Make DB migrations fully automatic

This commit is contained in:
Bernd Bestel
2017-04-21 11:52:24 +02:00
parent fe8a6d96e4
commit d628f9b3ca
5 changed files with 91 additions and 61 deletions

View File

@@ -75,14 +75,11 @@ class GrocyDbMigrator
private static function ExecuteMigrationWhenNeeded(PDO $pdo, int $migrationId, string $sql)
{
if ($pdo->query("SELECT COUNT(*) FROM migrations WHERE migration = $migrationId")->fetchColumn() == 0)
$rowCount = Grocy::ExecuteDbQuery($pdo, 'SELECT COUNT(*) FROM migrations WHERE migration = ' . $migrationId)->fetchColumn();
if (intval($rowCount) === 0)
{
if ($pdo->exec(utf8_encode($sql)) === false)
{
throw new Exception($pdo->errorInfo());
}
$pdo->exec('INSERT INTO migrations (migration) VALUES (' . $migrationId . ')');
Grocy::ExecuteDbStatement($pdo, $sql);
Grocy::ExecuteDbStatement($pdo, 'INSERT INTO migrations (migration) VALUES (' . $migrationId . ')');
}
}
}