diff --git a/.idea/runConfigurations/Start_MySql.xml b/.idea/runConfigurations/Start_MySql.xml new file mode 100644 index 0000000..6ec0abf --- /dev/null +++ b/.idea/runConfigurations/Start_MySql.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations/Start_Postgres.xml b/.idea/runConfigurations/Start_Postgres.xml new file mode 100644 index 0000000..95c3e67 --- /dev/null +++ b/.idea/runConfigurations/Start_Postgres.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations/Start_SQL_Server.xml b/.idea/runConfigurations/Start_SQL_Server.xml new file mode 100644 index 0000000..81de896 --- /dev/null +++ b/.idea/runConfigurations/Start_SQL_Server.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/runConfigurations/Test_Postgres.xml b/.idea/runConfigurations/Test_Postgres.xml index 1027c17..999f71b 100644 --- a/.idea/runConfigurations/Test_Postgres.xml +++ b/.idea/runConfigurations/Test_Postgres.xml @@ -1,26 +1,6 @@ - - - - - - - + + \ No newline at end of file diff --git a/.idea/runConfigurations/Test_SqlSever.xml b/.idea/runConfigurations/Test_SqlSever.xml new file mode 100644 index 0000000..cd28e03 --- /dev/null +++ b/.idea/runConfigurations/Test_SqlSever.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/composer.json b/composer.json index 9fc8816..26a4ff2 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/src/Database/AbstractDatabase.php b/src/Database/AbstractDatabase.php index 702a6ea..b46c172 100644 --- a/src/Database/AbstractDatabase.php +++ b/src/Database/AbstractDatabase.php @@ -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()); + } } diff --git a/src/Database/DatabaseInterface.php b/src/Database/DatabaseInterface.php index 7602ad5..5a8e144 100644 --- a/src/Database/DatabaseInterface.php +++ b/src/Database/DatabaseInterface.php @@ -27,6 +27,8 @@ public function setVersion($version, $status); public function createVersion(); + public function isDatabaseVersioned(); + public function getDbDriver(); public function getMigrationTable(); diff --git a/src/Database/PgsqlDatabase.php b/src/Database/PgsqlDatabase.php index e19676c..533e1ec 100644 --- a/src/Database/PgsqlDatabase.php +++ b/src/Database/PgsqlDatabase.php @@ -80,4 +80,9 @@ protected function executeSqlInternal($sql) } $this->getDbDriver()->execute($sql); } + + public function isDatabaseVersioned() + { + return $this->isTableExists('public', $this->getMigrationTable()); + } } diff --git a/src/Database/SqliteDatabase.php b/src/Database/SqliteDatabase.php index 4242249..080b5d9 100644 --- a/src/Database/SqliteDatabase.php +++ b/src/Database/SqliteDatabase.php @@ -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()); + } } diff --git a/src/Migration.php b/src/Migration.php index 8e71d19..fe2be0b 100644 --- a/src/Migration.php +++ b/src/Migration.php @@ -101,6 +101,11 @@ public function getDbCommand() return $this->dbCommand; } + public function getMigrationTable() + { + return $this->migrationTable; + } + /** * @return mixed * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered @@ -346,4 +351,9 @@ public function addCallbackProgress(callable $callable) { $this->callableProgress = $callable; } + + public function isDatabaseVersioned() + { + return $this->getDbCommand()->isDatabaseVersioned(); + } } diff --git a/tests/BaseDatabase.php b/tests/BaseDatabase.php index 4afcadd..4a1cf3f 100644 --- a/tests/BaseDatabase.php +++ b/tests/BaseDatabase.php @@ -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