Skip to content

Commit

Permalink
Fix containers not being added to groups during the migration
Browse files Browse the repository at this point in the history
Make sure only one migration can run at a time
Add some extra logging to the migrations
  • Loading branch information
austinwbest committed Sep 16, 2024
1 parent 6f88300 commit b64bd2a
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 6 deletions.
37 changes: 35 additions & 2 deletions root/app/www/public/classes/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,55 @@ public function migrations()
$database = $this;
$db = $this->db;

//-- DONT RUN MIGRATIONS IF IT IS ALREADY RUNNING
if (file_exists(MIGRATION_FILE)) {
return;
}

setFile(MIGRATION_FILE, ['started' => date('c')]);

if (filesize(DATABASE_PATH . 'dockwatch.db') == 0) { //-- INITIAL SETUP
logger(SYSTEM_LOG, 'Creating database and applying migration 001_initial_setup');
logger(MIGRATION_LOG, '====================|');
logger(MIGRATION_LOG, '====================| migrations');
logger(MIGRATION_LOG, '====================|');
logger(MIGRATION_LOG, 'migration 001 ->');
require MIGRATIONS_PATH . '001_initial_setup.php';
logger(MIGRATION_LOG, 'migration 001 <-');
} else { //-- GET CURRENT MIGRATION & CHECK FOR NEEDED MIGRATIONS

$dir = opendir(MIGRATIONS_PATH);
while ($migration = readdir($dir)) {
if (substr($migration, 0, 3) > $this->getSetting('migration') && str_contains($migration, '.php')) {
logger(SYSTEM_LOG, 'Applying migration ' . $migration);
logger(MIGRATION_LOG, 'migration ' . substr($migration, 0, 3) . ' ->');
require MIGRATIONS_PATH . $migration;
logger(MIGRATION_LOG, 'migration ' . substr($migration, 0, 3) . ' <-');
}
}
closedir($dir);
} 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;
}
}
closedir($dir);

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

foreach ($neededMigrations as $neededMigration) {
logger(MIGRATION_LOG, 'migration ' . substr($neededMigration, 0, 3) . ' ->');
require MIGRATIONS_PATH . $neededMigration;
logger(MIGRATION_LOG, 'migration ' . substr($neededMigration, 0, 3) . ' <-');
}
}
}

deleteFile(MIGRATION_FILE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function getContainerGroups()
return $this->containerGroupsTable;
}

$containerGroups = [];
$q = "SELECT *
FROM " . CONTAINER_GROUPS_TABLE;
$r = $this->query($q);
Expand All @@ -23,12 +24,13 @@ public function getContainerGroups()
}

$this->containerGroupsTable = $containerGroups;
return $containerGroups ?? [];
return $containerGroups;
}

public function getContainerGroupFromHash($hash, $groups)
{
if (!$groups) {
$this->containerGroupsTable = '';
$groups = $this->getContainerGroups();
}

Expand Down
4 changes: 2 additions & 2 deletions root/app/www/public/functions/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ function apiRequestLocal($endpoint, $parameters = [], $payload = [])
if (!$parameters['hash']) {
apiResponse(400, ['error' => 'Missing hash parameter']);
}
$containersTable = $database->getContainers();
$groupsTable = $database->getContainerGroups();

return $database->getContainerGroupFromHash($parameters['hash'], $containersTable);
return $database->getContainerGroupFromHash($parameters['hash'], $groupsTable);
case 'database-getContainers':
return $database->getContainers();
case 'database-getContainerGroups':
Expand Down
7 changes: 7 additions & 0 deletions root/app/www/public/functions/files.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,10 @@ function setFile($file, $contents)
$contents = is_array($contents) ? json_encode($contents) : $contents;
file_put_contents($file, $contents);
}

function deleteFile($file)
{
logger(SYSTEM_LOG, 'deleteFile() ' . $file);

unlink($file);
}
1 change: 1 addition & 0 deletions root/app/www/public/includes/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
define('STATS_FILE', APP_DATA_PATH . 'stats.json');
define('DEPENDENCY_FILE', APP_DATA_PATH . 'dependencies.json');
define('SSE_FILE', APP_DATA_PATH . 'sse.json');
define('MIGRATION_FILE', APP_DATA_PATH . 'migration-in-progress.txt');

//-- LOG FILES
define('SYSTEM_LOG', LOGS_PATH . 'system/app.log');
Expand Down
8 changes: 7 additions & 1 deletion root/app/www/public/migrations/001_initial_setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@
$db->query($query);

if ($database->error() != 'not an error') {
logger(MIGRATION_LOG, '<span class="text-info">[R]</span> ' .$database->error(), 'error');
logger(MIGRATION_LOG, '<span class="text-info">[R]</span> ' . $database->error(), 'error');
} else {
logger(MIGRATION_LOG, '<span class="text-info">[R]</span> query applied!');
}
Expand All @@ -264,6 +264,10 @@

if ($group['id'] && $container['id']) {
$containerLinkRows[] = "('" . $group['id'] . "', '" . $container['id'] . "')";
} else {
logger(MIGRATION_LOG, 'Could not match container hash (' . $groupContainerHash . ') with group hash (' . $groupHash. ') to an existing group', 'error');
logger(MIGRATION_LOG, '$container=' . json_encode($container));
logger(MIGRATION_LOG, '$group=' . json_encode($group));
}
}
}
Expand All @@ -274,6 +278,8 @@
(`group_id`, `container_id`)
VALUES " . implode(', ', $containerLinkRows);
}
} else {
logger(MIGRATION_LOG, 'No \'containerGroups\' found in the settings file');
}

foreach ($q as $query) {
Expand Down

0 comments on commit b64bd2a

Please sign in to comment.