mirror of
https://github.com/grocy/grocy.git
synced 2025-10-12 16:44:55 +00:00
Applied PHP formatting rules
This commit is contained in:
@@ -6,47 +6,58 @@ class DatabaseMigrationService extends BaseService
|
||||
{
|
||||
public function MigrateDatabase()
|
||||
{
|
||||
$this->getDatabaseService()->ExecuteDbStatement("CREATE TABLE IF NOT EXISTS migrations (migration INTEGER NOT NULL PRIMARY KEY UNIQUE, execution_time_timestamp DATETIME DEFAULT (datetime('now', 'localtime')))");
|
||||
$this->getDatabaseService()->ExecuteDbStatement("CREATE TABLE IF NOT EXISTS migrations (migration INTEGER NOT NULL PRIMARY KEY UNIQUE, execution_time_timestamp DATETIME DEFAULT (datetime('now', 'localtime')))");
|
||||
|
||||
$migrationFiles = [];
|
||||
|
||||
$migrationFiles = array();
|
||||
foreach (new \FilesystemIterator(__DIR__ . '/../migrations') as $file)
|
||||
{
|
||||
$migrationFiles[$file->getBasename()] = $file;
|
||||
$migrationFiles[$file->getBasename()] = $file;
|
||||
}
|
||||
|
||||
ksort($migrationFiles);
|
||||
foreach($migrationFiles as $migrationKey => $migrationFile)
|
||||
{
|
||||
if($migrationFile->getExtension() === 'php')
|
||||
{
|
||||
$migrationNumber = ltrim($migrationFile->getBasename('.php'), '0');
|
||||
$this->ExecutePhpMigrationWhenNeeded($migrationNumber, $migrationFile->getPathname());
|
||||
}
|
||||
else if($migrationFile->getExtension() === 'sql')
|
||||
{
|
||||
$migrationNumber = ltrim($migrationFile->getBasename('.sql'), '0');
|
||||
$this->ExecuteSqlMigrationWhenNeeded($migrationNumber, file_get_contents($migrationFile->getPathname()));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private function ExecuteSqlMigrationWhenNeeded(int $migrationId, string $sql)
|
||||
{
|
||||
$rowCount = $this->getDatabaseService()->ExecuteDbQuery('SELECT COUNT(*) FROM migrations WHERE migration = ' . $migrationId)->fetchColumn();
|
||||
if (intval($rowCount) === 0)
|
||||
foreach ($migrationFiles as $migrationKey => $migrationFile)
|
||||
{
|
||||
$this->getDatabaseService()->ExecuteDbStatement($sql);
|
||||
$this->getDatabaseService()->ExecuteDbStatement('INSERT INTO migrations (migration) VALUES (' . $migrationId . ')');
|
||||
if ($migrationFile->getExtension() === 'php')
|
||||
{
|
||||
$migrationNumber = ltrim($migrationFile->getBasename('.php'), '0');
|
||||
$this->ExecutePhpMigrationWhenNeeded($migrationNumber, $migrationFile->getPathname());
|
||||
}
|
||||
else
|
||||
|
||||
if ($migrationFile->getExtension() === 'sql')
|
||||
{
|
||||
$migrationNumber = ltrim($migrationFile->getBasename('.sql'), '0');
|
||||
$this->ExecuteSqlMigrationWhenNeeded($migrationNumber, file_get_contents($migrationFile->getPathname()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private function ExecutePhpMigrationWhenNeeded(int $migrationId, string $phpFile)
|
||||
{
|
||||
$rowCount = $this->getDatabaseService()->ExecuteDbQuery('SELECT COUNT(*) FROM migrations WHERE migration = ' . $migrationId)->fetchColumn();
|
||||
|
||||
if (intval($rowCount) === 0)
|
||||
{
|
||||
include $phpFile;
|
||||
$this->getDatabaseService()->ExecuteDbStatement('INSERT INTO migrations (migration) VALUES (' . $migrationId . ')');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private function ExecuteSqlMigrationWhenNeeded(int $migrationId, string $sql)
|
||||
{
|
||||
$rowCount = $this->getDatabaseService()->ExecuteDbQuery('SELECT COUNT(*) FROM migrations WHERE migration = ' . $migrationId)->fetchColumn();
|
||||
|
||||
if (intval($rowCount) === 0)
|
||||
{
|
||||
$this->getDatabaseService()->ExecuteDbStatement($sql);
|
||||
$this->getDatabaseService()->ExecuteDbStatement('INSERT INTO migrations (migration) VALUES (' . $migrationId . ')');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user