Skip to content

Commit

Permalink
Added isDatabaseVersioned issue #27
Browse files Browse the repository at this point in the history
  • Loading branch information
byjg committed May 6, 2020
1 parent 2f614ed commit 3d74841
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Database/AbstractDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,19 @@ public function updateVersionTable()
$this->createVersion();
$this->setVersion($currentVersion, Migration::VERSION_STATUS_UNKNOWN);
}

public function isDatabaseVersioned()
{
$count = $this->getDbDriver()->getScalar(
'SELECT count(*) FROM information_schema.tables ' .
' WHERE table_schema = [[schema]] ' .
' AND table_name = [[table]] ',
[
"schema" => ltrim($this->getDbDriver()->getUri()->getPath(), "/"),
"table" => $this->getMigrationTable()
]
);

return ($count !== 0);
}
}
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
12 changes: 12 additions & 0 deletions src/Database/SqliteDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,16 @@ protected function executeSqlInternal($sql)
{
$this->getDbDriver()->execute($sql);
}

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

return (intval($count) !== 0);
}
}
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 3d74841

Please sign in to comment.