mirror of
https://github.com/grocy/grocy.git
synced 2025-04-29 17:45:39 +00:00
Optimize SQLite database file after migrations have run
This commit is contained in:
parent
24c9247663
commit
68aad90a59
@ -15,30 +15,34 @@ class DatabaseMigrationService extends BaseService
|
|||||||
$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 = [];
|
||||||
|
|
||||||
foreach (new \FilesystemIterator(__DIR__ . '/../migrations') as $file)
|
foreach (new \FilesystemIterator(__DIR__ . '/../migrations') as $file)
|
||||||
{
|
{
|
||||||
$migrationFiles[$file->getBasename()] = $file;
|
$migrationFiles[$file->getBasename()] = $file;
|
||||||
}
|
}
|
||||||
|
|
||||||
ksort($migrationFiles);
|
ksort($migrationFiles);
|
||||||
|
|
||||||
|
$migrationCounter = 0;
|
||||||
foreach ($migrationFiles as $migrationKey => $migrationFile)
|
foreach ($migrationFiles as $migrationKey => $migrationFile)
|
||||||
{
|
{
|
||||||
if ($migrationFile->getExtension() === 'php')
|
if ($migrationFile->getExtension() === 'php')
|
||||||
{
|
{
|
||||||
$migrationNumber = ltrim($migrationFile->getBasename('.php'), '0');
|
$migrationNumber = ltrim($migrationFile->getBasename('.php'), '0');
|
||||||
$this->ExecutePhpMigrationWhenNeeded($migrationNumber, $migrationFile->getPathname());
|
$this->ExecutePhpMigrationWhenNeeded($migrationNumber, $migrationFile->getPathname(), $migrationCounter);
|
||||||
}
|
}
|
||||||
elseif ($migrationFile->getExtension() === 'sql')
|
elseif ($migrationFile->getExtension() === 'sql')
|
||||||
{
|
{
|
||||||
$migrationNumber = ltrim($migrationFile->getBasename('.sql'), '0');
|
$migrationNumber = ltrim($migrationFile->getBasename('.sql'), '0');
|
||||||
$this->ExecuteSqlMigrationWhenNeeded($migrationNumber, file_get_contents($migrationFile->getPathname()));
|
$this->ExecuteSqlMigrationWhenNeeded($migrationNumber, file_get_contents($migrationFile->getPathname()), $migrationCounter);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function ExecutePhpMigrationWhenNeeded(int $migrationId, string $phpFile)
|
if ($migrationCounter > 0)
|
||||||
|
{
|
||||||
|
$this->getDatabaseService()->ExecuteDbStatement('VACUUM');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function ExecutePhpMigrationWhenNeeded(int $migrationId, string $phpFile, int &$migrationCounter)
|
||||||
{
|
{
|
||||||
$rowCount = $this->getDatabaseService()->ExecuteDbQuery('SELECT COUNT(*) FROM migrations WHERE migration = ' . $migrationId)->fetchColumn();
|
$rowCount = $this->getDatabaseService()->ExecuteDbQuery('SELECT COUNT(*) FROM migrations WHERE migration = ' . $migrationId)->fetchColumn();
|
||||||
|
|
||||||
@ -49,11 +53,12 @@ class DatabaseMigrationService extends BaseService
|
|||||||
if ($migrationId != self::EMERGENCY_MIGRATION_ID && $migrationId != self::DOALWAYS_MIGRATION_ID)
|
if ($migrationId != self::EMERGENCY_MIGRATION_ID && $migrationId != self::DOALWAYS_MIGRATION_ID)
|
||||||
{
|
{
|
||||||
$this->getDatabaseService()->ExecuteDbStatement('INSERT INTO migrations (migration) VALUES (' . $migrationId . ')');
|
$this->getDatabaseService()->ExecuteDbStatement('INSERT INTO migrations (migration) VALUES (' . $migrationId . ')');
|
||||||
|
$migrationCounter++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function ExecuteSqlMigrationWhenNeeded(int $migrationId, string $sql)
|
private function ExecuteSqlMigrationWhenNeeded(int $migrationId, string $sql, int &$migrationCounter)
|
||||||
{
|
{
|
||||||
$rowCount = $this->getDatabaseService()->ExecuteDbQuery('SELECT COUNT(*) FROM migrations WHERE migration = ' . $migrationId)->fetchColumn();
|
$rowCount = $this->getDatabaseService()->ExecuteDbQuery('SELECT COUNT(*) FROM migrations WHERE migration = ' . $migrationId)->fetchColumn();
|
||||||
|
|
||||||
@ -68,6 +73,7 @@ class DatabaseMigrationService extends BaseService
|
|||||||
if ($migrationId != self::EMERGENCY_MIGRATION_ID && $migrationId != self::DOALWAYS_MIGRATION_ID)
|
if ($migrationId != self::EMERGENCY_MIGRATION_ID && $migrationId != self::DOALWAYS_MIGRATION_ID)
|
||||||
{
|
{
|
||||||
$this->getDatabaseService()->ExecuteDbStatement('INSERT INTO migrations (migration) VALUES (' . $migrationId . ')');
|
$this->getDatabaseService()->ExecuteDbStatement('INSERT INTO migrations (migration) VALUES (' . $migrationId . ')');
|
||||||
|
$migrationCounter++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception $ex)
|
catch (Exception $ex)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user