From d6800f9b6f91cde8b0470fa3ccac6ef892316267 Mon Sep 17 00:00:00 2001 From: flashwave Date: Tue, 7 Nov 2023 17:12:47 +0000 Subject: [PATCH] Added EEPROM database migrations. --- eeprom.php | 2 ++ src/DatabaseContext.php | 30 ++++++++++++++++++++++++++++++ src/EEPROMContext.php | 23 +++++++++++++++++++++++ tools/migrate | 9 ++++----- tools/new-migration | 6 +++--- 5 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 src/DatabaseContext.php create mode 100644 src/EEPROMContext.php diff --git a/eeprom.php b/eeprom.php index f9a6fcb..93ad56d 100644 --- a/eeprom.php +++ b/eeprom.php @@ -43,3 +43,5 @@ if(!is_dir(PRM_THUMBS)) $db = DbTools::create($cfg->getString('database:dsn', 'null:')); $db->execute('SET SESSION time_zone = \'+00:00\', sql_mode = \'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION\';'); + +$eeprom = new EEPROMContext($cfg, $db); diff --git a/src/DatabaseContext.php b/src/DatabaseContext.php new file mode 100644 index 0000000..3ae2f79 --- /dev/null +++ b/src/DatabaseContext.php @@ -0,0 +1,30 @@ +connection; + } + + public function getQueryCount(): int { + $result = $this->connection->query('SHOW SESSION STATUS LIKE "Questions"'); + return $result->next() ? $result->getInteger(1) : 0; + } + + public function createMigrationManager(): DbMigrationManager { + return new DbMigrationManager($this->connection, 'prm_' . DbMigrationManager::DEFAULT_TABLE); + } + + public function createMigrationRepo(): IDbMigrationRepo { + return new FsDbMigrationRepo(PRM_MIGRATIONS); + } +} diff --git a/src/EEPROMContext.php b/src/EEPROMContext.php new file mode 100644 index 0000000..8911e14 --- /dev/null +++ b/src/EEPROMContext.php @@ -0,0 +1,23 @@ +config = $config; + $this->dbCtx = new DatabaseContext($dbConn); + } + + public function getConfig(): IConfig { + return $this->config; + } + + public function getDatabase(): DatabaseContext { + return $this->dbCtx; + } +} diff --git a/tools/migrate b/tools/migrate index 85c8914..5c3d295 100755 --- a/tools/migrate +++ b/tools/migrate @@ -1,22 +1,21 @@ #!/usr/bin/env php getDatabase(); + echo 'Creating migration manager...' . PHP_EOL; - $manager = new DbMigrationManager($db, 'prm_' . DbMigrationManager::DEFAULT_TABLE); + $manager = $db->createMigrationManager(); echo 'Preparing to run migrations...' . PHP_EOL; $manager->init(); echo 'Creating migration repository...' . PHP_EOL; - $repo = new FsDbMigrationRepo(PRM_MIGRATIONS); + $repo = $db->createMigrationRepo(); echo 'Running migrations...' . PHP_EOL; $completed = $manager->processMigrations($repo); diff --git a/tools/new-migration b/tools/new-migration index ca34377..b42a6d2 100755 --- a/tools/new-migration +++ b/tools/new-migration @@ -1,18 +1,18 @@ #!/usr/bin/env php getDatabase(); +$repo = $db->createMigrationRepo(); if(!($repo instanceof FsDbMigrationRepo)) { echo 'Migration repository type does not support creation of templates.' . PHP_EOL; return; } $baseName = implode(' ', array_slice($argv, 1)); -$manager = new DbMigrationManager($db, 'prm_' . DbMigrationManager::DEFAULT_TABLE); +$manager = $db->createMigrationManager(); try { $names = $manager->createNames($baseName);