Skip to content

Commit

Permalink
Make sure migrations run in order
Browse files Browse the repository at this point in the history
  • Loading branch information
austinwbest committed Oct 6, 2024
1 parent a50c8a9 commit 772a6d7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/root/app/www/public/logs/*
/root/app/www/public/api/container-alias.json
/.vscode/*
29 changes: 20 additions & 9 deletions root/app/www/public/classes/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,37 +125,48 @@ public function migrations()
require MIGRATIONS_PATH . '001_initial_setup.php';
logger(MIGRATION_LOG, 'migration 001 <-');

$neededMigrations = [];
$dir = opendir(MIGRATIONS_PATH);
while ($migration = readdir($dir)) {
if (substr($migration, 0, 3) > $this->getSetting('migration') && str_contains($migration, '.php')) {
logger(MIGRATION_LOG, 'migration ' . substr($migration, 0, 3) . ' ->');
$q = [];
require MIGRATIONS_PATH . $migration;
logger(MIGRATION_LOG, 'migration ' . substr($migration, 0, 3) . ' <-');
$neededMigrations[substr($migration, 0, 3)] = $migration;
}
}
closedir($dir);

if ($neededMigrations) {
ksort($neededMigrations);

foreach ($neededMigrations as $migrationNumber => $neededMigration) {
logger(MIGRATION_LOG, 'migration ' . $migrationNumber . ' ->');
$q = [];
require MIGRATIONS_PATH . $neededMigration;
logger(MIGRATION_LOG, 'migration ' . $migrationNumber . ' <-');
}
}
} else { //-- GET CURRENT MIGRATION & CHECK FOR NEEDED MIGRATIONS
$neededMigrations = [];
$dir = opendir(MIGRATIONS_PATH);
while ($migration = readdir($dir)) {
if (substr($migration, 0, 3) > $this->getSetting('migration') && str_contains($migration, '.php')) {
$neededMigrations[] = $migration;
$neededMigrations[substr($migration, 0, 3)] = $migration;
}
}
closedir($dir);

if ($neededMigrations) {
logger(SYSTEM_LOG, 'Applying migrations: ' . implode(', ', $neededMigrations));
ksort($neededMigrations);

logger(SYSTEM_LOG, 'Applying migrations: ' . implode(', ', array_keys($neededMigrations)));
logger(MIGRATION_LOG, '====================|');
logger(MIGRATION_LOG, '====================| migrations');
logger(MIGRATION_LOG, '====================|');

foreach ($neededMigrations as $neededMigration) {
logger(MIGRATION_LOG, 'migration ' . substr($neededMigration, 0, 3) . ' ->');
foreach ($neededMigrations as $migrationNumber => $neededMigration) {
logger(MIGRATION_LOG, 'migration ' . $migrationNumber . ' ->');
$q = [];
require MIGRATIONS_PATH . $neededMigration;
logger(MIGRATION_LOG, 'migration ' . substr($neededMigration, 0, 3) . ' <-');
logger(MIGRATION_LOG, 'migration ' . $migrationNumber . ' <-');
}
}
}
Expand Down

0 comments on commit 772a6d7

Please sign in to comment.