feat(install): Make upgrade script also work for eventdb

This commit is contained in:
celestora 2025-02-22 20:45:17 +02:00
parent b5b4853891
commit 22bae8a2e1
2 changed files with 69 additions and 20 deletions

View file

@ -18,6 +18,7 @@ class UpgradeCommand extends Command {
protected static $defaultName = "upgrade"; protected static $defaultName = "upgrade";
private Connection $db; private Connection $db;
private Connection $eventDb;
private array $chandlerTables = [ private array $chandlerTables = [
"CHANDLERACLPERMISSIONALIASES", "CHANDLERACLPERMISSIONALIASES",
@ -32,6 +33,7 @@ class UpgradeCommand extends Command {
public function __construct() public function __construct()
{ {
$this->db = DatabaseConnection::i()->getConnection(); $this->db = DatabaseConnection::i()->getConnection();
$this->eventDb = eventdb()->getConnection();
parent::__construct(); parent::__construct();
} }
@ -50,7 +52,7 @@ class UpgradeCommand extends Command {
"Location of Chandler installation"); "Location of Chandler installation");
} }
protected function checkDatabaseReadiness(bool &$chandlerOk, bool &$ovkOk, bool &$migrationsOk): void protected function checkDatabaseReadiness(bool &$chandlerOk, bool &$ovkOk, bool &$eventOk, bool &$migrationsOk): void
{ {
$tables = $this->db->query("SHOW TABLES")->fetchAll(); $tables = $this->db->query("SHOW TABLES")->fetchAll();
$tables = array_map(fn ($x) => strtoupper($x->offsetGet(0)), $tables); $tables = array_map(fn ($x) => strtoupper($x->offsetGet(0)), $tables);
@ -63,13 +65,22 @@ class UpgradeCommand extends Command {
else else
$chandlerOk = false; $chandlerOk = false;
if (is_null($this->eventDb)) {
$eventOk = false;
} else if (is_null($this->eventDb->query("SHOW TABLES LIKE \"notifications\"")->fetch())) {
$eventOk = null;
} else {
$eventOk = true;
}
$ovkOk = in_array("PROFILES", $tables); $ovkOk = in_array("PROFILES", $tables);
$migrationsOk = in_array("OVK_UPGRADE_HISTORY", $tables); $migrationsOk = in_array("OVK_UPGRADE_HISTORY", $tables);
} }
protected function executeSqlScript(int $errCode, string $script, SymfonyStyle $io, bool $transaction = false): int protected function executeSqlScript(int $errCode, string $script, SymfonyStyle $io, bool $transaction = false,
bool $eventDb = false): int
{ {
$pdo = $this->db->getPdo(); $pdo = ($eventDb ? $this->eventDb : $this->db)->getPdo();
$res = false; $res = false;
try { try {
@ -96,21 +107,23 @@ class UpgradeCommand extends Command {
return $errCode; return $errCode;
} }
protected function getNextLevel(): int protected function getNextLevel(bool $eventDb = false): int
{ {
$record = $this->db->query("SELECT level FROM ovk_upgrade_history ORDER BY level DESC LIMIT 1"); $tbl = $eventDb ? "ovk_events_upgrade_history" : "ovk_upgrade_history";
$record = $this->db->query("SELECT level FROM $tbl ORDER BY level DESC LIMIT 1");
if (!$record->getRowCount()) if (!$record->getRowCount())
return 0; return 0;
return $record->fetchField() + 1; return $record->fetchField() + 1;
} }
protected function getMigrationFiles(): array protected function getMigrationFiles(bool $eventDb = false): array
{ {
$files = []; $files = [];
$root = dirname(__DIR__ . "/../install/init-static-db.sql"); $root = dirname(__DIR__ . "/../install/init-static-db.sql");
$dir = $eventDb ? "sqls/eventdb" : "sqls";
foreach (glob("$root/sqls/*.sql") as $file) foreach (glob("$root/$dir/*.sql") as $file)
$files[(int) basename($file)] = basename($file); $files[(int) basename($file)] = basename($file);
ksort($files); ksort($files);
@ -155,31 +168,38 @@ class UpgradeCommand extends Command {
return $this->executeSqlScript(31, $installFile, $io); return $this->executeSqlScript(31, $installFile, $io);
} }
protected function initEventSchema(SymfonyStyle $io): int
{
$installFile = file_get_contents(__DIR__ . "/../install/init-event-db.sql");
return $this->executeSqlScript(31, $installFile, $io, true, true);
}
protected function initUpgradeLog(SymfonyStyle $io): int protected function initUpgradeLog(SymfonyStyle $io): int
{ {
$installFile = file_get_contents(__DIR__ . "/../install/init-migration-table.sql"); $installFile = file_get_contents(__DIR__ . "/../install/init-migration-table.sql");
return $this->executeSqlScript(31, $installFile, $io); return $this->executeSqlScript(31, $installFile, $io, true);
} }
protected function runMigrations(SymfonyStyle $io, bool $oneshot): int protected function runMigrations(SymfonyStyle $io, bool $eventDb, bool $oneshot): int
{ {
$nextLevel = $this->getNextLevel(); $dir = $eventDb ? "sqls/eventdb" : "sqls";
$migrations = array_filter($this->getMigrationFiles(), fn ($id) => $id >= $nextLevel, ARRAY_FILTER_USE_KEY); $tbl = $eventDb ? "ovk_events_upgrade_history" : "ovk_upgrade_history";
$nextLevel = $this->getNextLevel($eventDb);
$migrations = array_filter($this->getMigrationFiles($eventDb), fn ($id) => $id >= $nextLevel,
ARRAY_FILTER_USE_KEY);
if (!sizeof($migrations)) { if (!sizeof($migrations))
$io->writeln("Database up to date. Nothing left to do.");
return 24; return 24;
}
$uname = addslashes(`whoami`); $uname = addslashes(`whoami`);
$bar = new ProgressBar($io, sizeof($migrations)); $bar = new ProgressBar($io, sizeof($migrations));
$bar->setFormat("very_verbose"); $bar->setFormat("very_verbose");
foreach ($bar->iterate($migrations) as $num => $migration) { foreach ($bar->iterate($migrations) as $num => $migration) {
$script = file_get_contents(__DIR__ . "/../install/sqls/$migration"); $script = file_get_contents(__DIR__ . "/../install/$dir/$migration");
$res = $this->executeSqlScript(100 + $num, $script, $io, true); $res = $this->executeSqlScript(100 + $num, $script, $io, true, $eventDb);
if ($res != 0) { if ($res != 0) {
$io->getErrorStyle()->error("Error while executing migration №$num"); $io->getErrorStyle()->error("Error while executing migration №$num");
@ -187,12 +207,14 @@ class UpgradeCommand extends Command {
} }
$t = time(); $t = time();
$this->db->query("INSERT INTO ovk_upgrade_history VALUES ($num, $t, \"$uname\");"); $this->db->query("INSERT INTO $tbl VALUES ($num, $t, \"$uname\");");
if ($oneshot) if ($oneshot)
return 5; return 5;
} }
$io->newLine();
return 0; return 0;
} }
@ -210,9 +232,10 @@ class UpgradeCommand extends Command {
$migrationsOk = false; $migrationsOk = false;
$chandlerOk = false; $chandlerOk = false;
$eventOk = false;
$ovkOk = false; $ovkOk = false;
$this->checkDatabaseReadiness($chandlerOk, $ovkOk, $migrationsOk); $this->checkDatabaseReadiness($chandlerOk, $ovkOk, $eventOk, $migrationsOk);
$res = -1; $res = -1;
if ($chandlerOk === null) { if ($chandlerOk === null) {
@ -255,8 +278,26 @@ class UpgradeCommand extends Command {
return 5; return 5;
} }
if ($eventOk !== false) {
if ($eventOk === null) {
$io->writeln("Initializing event database...");
$res = $this->initEventSchema($io);
if ($res > 0)
return $res;
else if ($oneShotMode)
return 5;
}
$io->writeln("Upgrading event database...");
$res = $this->runMigrations($io, true, $oneShotMode);
if ($res == 24)
$output->writeln("Event database already up to date.");
else if ($res > 0)
return $res;
}
$io->writeln("Upgrading database..."); $io->writeln("Upgrading database...");
$res = $this->runMigrations($io, $oneShotMode); $res = $this->runMigrations($io, false, $oneShotMode);
if (!$res) { if (!$res) {
$io->success("Database has been upgraded!"); $io->success("Database has been upgraded!");
@ -266,6 +307,8 @@ class UpgradeCommand extends Command {
return $res; return $res;
} }
$io->writeln("Database up to date. Nothing left to do.");
return 0; return 0;
} }
} }

View file

@ -6,4 +6,10 @@ CREATE TABLE `ovk_upgrade_history` (
`operator` varchar(256) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT "Maintenance Script" `operator` varchar(256) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT "Maintenance Script"
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci; ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
CREATE TABLE `ovk_events_upgrade_history` (
`level` smallint UNSIGNED NOT NULL,
`timestamp` bigint(20) UNSIGNED NOT NULL,
`operator` varchar(256) COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT "Maintenance Script"
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_520_ci;
COMMIT; COMMIT;