From 1c38976b954aec3cae59481ac4d69ae8f0ba7cfe Mon Sep 17 00:00:00 2001 From: flashwave Date: Sat, 7 Jan 2023 21:16:49 +0000 Subject: [PATCH] Added database migration tools. --- awaki.php | 2 +- .../2023_01_07_191402_initial_structure.php | 18 ++++++++++ src/AwakiContext.php | 11 ++++++ tools/migrate | 34 +++++++++++++++++++ tools/new-migration | 25 ++++++++++++++ 5 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 database/2023_01_07_191402_initial_structure.php create mode 100755 tools/migrate create mode 100755 tools/new-migration diff --git a/awaki.php b/awaki.php index a8cb9b0..eff3393 100644 --- a/awaki.php +++ b/awaki.php @@ -13,7 +13,7 @@ define('AWK_DIR_SRC', AWK_ROOT . '/src'); define('AWK_DIR_LIB', AWK_ROOT . '/lib'); define('AWK_DIR_PUB', AWK_ROOT . '/public'); define('AWK_DIR_CFG', AWK_ROOT . '/config'); -define('HAU_DIR_DBM', AWK_ROOT . '/database'); +define('AWK_DIR_DBM', AWK_ROOT . '/database'); require_once AWK_DIR_LIB . '/index/index.php'; diff --git a/database/2023_01_07_191402_initial_structure.php b/database/2023_01_07_191402_initial_structure.php new file mode 100644 index 0000000..a238bb9 --- /dev/null +++ b/database/2023_01_07_191402_initial_structure.php @@ -0,0 +1,18 @@ +execute(' + CREATE TABLE awk_redirects ( + redir_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, + redir_vanity VARCHAR(255) DEFAULT NULL, + redir_url VARCHAR(255) NOT NULL, + redir_created TIMESTAMP NOT NULL DEFAULT current_timestamp(), + PRIMARY KEY (redir_id), + UNIQUE KEY awk_redir_vanity_unique (redir_vanity) + ) ENGINE=InnoDB COLLATE=utf8mb4_bin; + '); + } +} diff --git a/src/AwakiContext.php b/src/AwakiContext.php index dab4b84..bcb1f1b 100644 --- a/src/AwakiContext.php +++ b/src/AwakiContext.php @@ -2,6 +2,9 @@ namespace Awaki; use Index\Data\IDbConnection; +use Index\Data\Migration\IDbMigrationRepo; +use Index\Data\Migration\DbMigrationManager; +use Index\Data\Migration\FsDbMigrationRepo; class AwakiContext { private const DB_INIT = '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\';'; @@ -17,4 +20,12 @@ class AwakiContext { $result = $this->dbConn->query('SHOW SESSION STATUS LIKE "Questions"'); return $result->next() ? $result->getInteger(0) : 0; } + + public function createMigrationManager(): DbMigrationManager { + return new DbMigrationManager($this->dbConn, 'awk_' . DbMigrationManager::DEFAULT_TABLE); + } + + public function createMigrationRepo(): IDbMigrationRepo { + return new FsDbMigrationRepo(AWK_DIR_DBM); + } } diff --git a/tools/migrate b/tools/migrate new file mode 100755 index 0000000..6c8ff22 --- /dev/null +++ b/tools/migrate @@ -0,0 +1,34 @@ +#!/usr/bin/env php +createMigrationManager(); + + echo 'Preparing to run migrations...' . PHP_EOL; + $manager->init(); + + echo 'Creating migration repository...' . PHP_EOL; + $repo = $awk->createMigrationRepo(); + + echo 'Running migrations...' . PHP_EOL; + $completed = $manager->processMigrations($repo); + + if(empty($completed)) { + echo 'There were no migrations to run!' . PHP_EOL; + } else { + echo 'The following migrations have been completed:' . PHP_EOL; + foreach($completed as $migration) + echo ' - ' . $migration . PHP_EOL; + } + + echo PHP_EOL; +} finally { + unlink(AWK_ROOT . '/.migrating'); +} diff --git a/tools/new-migration b/tools/new-migration new file mode 100755 index 0000000..1c2a5a4 --- /dev/null +++ b/tools/new-migration @@ -0,0 +1,25 @@ +#!/usr/bin/env php +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 = $awk->createMigrationManager(); + +try { + $names = $manager->createNames($baseName); +} catch(InvalidArgumentException $ex) { + echo $ex->getMessage() . PHP_EOL; + return; +} + +$repo->saveMigrationTemplate($names->name, $manager->template($names->className)); + +echo "Template for '{$names->className}' has been saved to {$names->name}.php." . PHP_EOL;