mirror of
https://github.com/grocy/grocy.git
synced 2025-08-24 05:56:55 +00:00
Replace the single user (defined in /data/config.php) with a multi user management thing
This commit is contained in:
@@ -8,21 +8,38 @@ class DatabaseMigrationService extends BaseService
|
||||
{
|
||||
$this->DatabaseService->ExecuteDbStatement("CREATE TABLE IF NOT EXISTS migrations (migration INTEGER NOT NULL PRIMARY KEY UNIQUE, execution_time_timestamp DATETIME DEFAULT (datetime('now', 'localtime')))");
|
||||
|
||||
$migrationFiles = array();
|
||||
$sqlMigrationFiles = array();
|
||||
foreach (new \FilesystemIterator(__DIR__ . '/../migrations') as $file)
|
||||
{
|
||||
$migrationFiles[$file->getBasename('.sql')] = $file->getPathname();
|
||||
if ($file->getExtension() === 'sql')
|
||||
{
|
||||
$sqlMigrationFiles[$file->getBasename('.sql')] = $file->getPathname();
|
||||
}
|
||||
}
|
||||
ksort($migrationFiles);
|
||||
|
||||
foreach($migrationFiles as $migrationNumber => $migrationFile)
|
||||
ksort($sqlMigrationFiles);
|
||||
foreach($sqlMigrationFiles as $migrationNumber => $migrationFile)
|
||||
{
|
||||
$migrationNumber = ltrim($migrationNumber, '0');
|
||||
$this->ExecuteMigrationWhenNeeded($migrationNumber, file_get_contents($migrationFile));
|
||||
$this->ExecuteSqlMigrationWhenNeeded($migrationNumber, file_get_contents($migrationFile));
|
||||
}
|
||||
|
||||
$phpMigrationFiles = array();
|
||||
foreach (new \FilesystemIterator(__DIR__ . '/../migrations') as $file)
|
||||
{
|
||||
if ($file->getExtension() === 'php')
|
||||
{
|
||||
$phpMigrationFiles[$file->getBasename('.php')] = $file->getPathname();
|
||||
}
|
||||
}
|
||||
ksort($phpMigrationFiles);
|
||||
foreach($phpMigrationFiles as $migrationNumber => $migrationFile)
|
||||
{
|
||||
$migrationNumber = ltrim($migrationNumber, '0');
|
||||
$this->ExecutePhpMigrationWhenNeeded($migrationNumber, $migrationFile);
|
||||
}
|
||||
}
|
||||
|
||||
private function ExecuteMigrationWhenNeeded(int $migrationId, string $sql)
|
||||
private function ExecuteSqlMigrationWhenNeeded(int $migrationId, string $sql)
|
||||
{
|
||||
$rowCount = $this->DatabaseService->ExecuteDbQuery('SELECT COUNT(*) FROM migrations WHERE migration = ' . $migrationId)->fetchColumn();
|
||||
if (intval($rowCount) === 0)
|
||||
@@ -31,4 +48,14 @@ class DatabaseMigrationService extends BaseService
|
||||
$this->DatabaseService->ExecuteDbStatement('INSERT INTO migrations (migration) VALUES (' . $migrationId . ')');
|
||||
}
|
||||
}
|
||||
|
||||
private function ExecutePhpMigrationWhenNeeded(int $migrationId, string $phpFile)
|
||||
{
|
||||
$rowCount = $this->DatabaseService->ExecuteDbQuery('SELECT COUNT(*) FROM migrations WHERE migration = ' . $migrationId)->fetchColumn();
|
||||
if (intval($rowCount) === 0)
|
||||
{
|
||||
include $phpFile;
|
||||
$this->DatabaseService->ExecuteDbStatement('INSERT INTO migrations (migration) VALUES (' . $migrationId . ')');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user