Skip to content

Commit

Permalink
Merge pull request #28 from byjg/4.1.0
Browse files Browse the repository at this point in the history
4.1.0
  • Loading branch information
byjg authored Jul 13, 2020
2 parents a79949d + edb688c commit 86c63ee
Show file tree
Hide file tree
Showing 12 changed files with 192 additions and 23 deletions.
38 changes: 38 additions & 0 deletions .idea/runConfigurations/Start_MySql.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions .idea/runConfigurations/Start_Postgres.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions .idea/runConfigurations/Start_SQL_Server.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 2 additions & 22 deletions .idea/runConfigurations/Test_Postgres.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/runConfigurations/Test_SqlSever.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"byjg/anydataset-db": "4.0.*",
"byjg/anydataset-db": "4.1.*",
"ext-pdo": "*"
},
"require-dev": {
Expand Down
20 changes: 20 additions & 0 deletions src/Database/AbstractDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,24 @@ public function updateVersionTable()
$this->createVersion();
$this->setVersion($currentVersion, Migration::VERSION_STATUS_UNKNOWN);
}

protected function isTableExists($schema, $table)
{
$count = $this->getDbDriver()->getScalar(
'SELECT count(*) FROM information_schema.tables ' .
' WHERE table_schema = [[schema]] ' .
' AND table_name = [[table]] ',
[
"schema" => $schema,
"table" => $table
]
);

return (intval($count) !== 0);
}

public function isDatabaseVersioned()
{
return $this->isTableExists(ltrim($this->getDbDriver()->getUri()->getPath(), "/"), $this->getMigrationTable());
}
}
2 changes: 2 additions & 0 deletions src/Database/DatabaseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public function setVersion($version, $status);

public function createVersion();

public function isDatabaseVersioned();

public function getDbDriver();

public function getMigrationTable();
Expand Down
5 changes: 5 additions & 0 deletions src/Database/PgsqlDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,9 @@ protected function executeSqlInternal($sql)
}
$this->getDbDriver()->execute($sql);
}

public function isDatabaseVersioned()
{
return $this->isTableExists('public', $this->getMigrationTable());
}
}
17 changes: 17 additions & 0 deletions src/Database/SqliteDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,21 @@ protected function executeSqlInternal($sql)
{
$this->getDbDriver()->execute($sql);
}

protected function isTableExists($schema, $table)
{
$count = $this->getDbDriver()->getScalar(
"SELECT count(*) FROM sqlite_master WHERE type='table' AND name=[[table]]",
[
"table" => $table
]
);

return (intval($count) !== 0);
}

public function isDatabaseVersioned()
{
return $this->isTableExists(null, $this->getMigrationTable());
}
}
10 changes: 10 additions & 0 deletions src/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ public function getDbCommand()
return $this->dbCommand;
}

public function getMigrationTable()
{
return $this->migrationTable;
}

/**
* @return mixed
* @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
Expand Down Expand Up @@ -346,4 +351,9 @@ public function addCallbackProgress(callable $callable)
{
$this->callableProgress = $callable;
}

public function isDatabaseVersioned()
{
return $this->getDbCommand()->isDatabaseVersioned();
}
}
7 changes: 7 additions & 0 deletions tests/BaseDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ public function testVersion0()
$this->assertVersion0();
}

public function testIsDatabaseVersioned()
{
$this->assertFalse($this->migrate->isDatabaseVersioned());
$this->migrate->reset();
$this->assertTrue($this->migrate->isDatabaseVersioned());
}

/**
* @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered
* @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException
Expand Down

0 comments on commit 86c63ee

Please sign in to comment.