From 07ad8bd7213b86cf431b5547f9d12835b45618bc Mon Sep 17 00:00:00 2001 From: flashwave Date: Sat, 7 Jan 2023 04:36:22 +0000 Subject: [PATCH] Added database migration tools. --- database/.gitkeep | 0 hanyuu.php | 1 + lib/index | 2 +- src/HanyuuContext.php | 11 +++++++++++ tools/migrate | 34 ++++++++++++++++++++++++++++++++++ tools/new-migration | 25 +++++++++++++++++++++++++ 6 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 database/.gitkeep create mode 100755 tools/migrate create mode 100755 tools/new-migration diff --git a/database/.gitkeep b/database/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/hanyuu.php b/hanyuu.php index 7bdf9b6..b73702c 100644 --- a/hanyuu.php +++ b/hanyuu.php @@ -13,6 +13,7 @@ define('HAU_DIR_PUBLIC', HAU_ROOT . '/public'); define('HAU_DIR_SOURCE', HAU_ROOT . '/src'); define('HAU_DIR_LIBRARIES', HAU_ROOT . '/lib'); define('HAU_DIR_CONFIG', HAU_ROOT . '/config'); +define('HAU_DIR_MIGRATIONS', HAU_ROOT . '/database'); define('HAU_NDX_PATH', HAU_DIR_LIBRARIES . '/index'); define('HAU_NDX_PATH_DEV', HAU_DIR_LIBRARIES . '/index-dev'); diff --git a/lib/index b/lib/index index f8c6602..fbe4fe1 160000 --- a/lib/index +++ b/lib/index @@ -1 +1 @@ -Subproject commit f8c6602ab953491a3e540d1ab9c77f2d885ee120 +Subproject commit fbe4fe18decd502a0ca15ffe8a7c3b2d847349d5 diff --git a/src/HanyuuContext.php b/src/HanyuuContext.php index bc0bf6e..18d1e1f 100644 --- a/src/HanyuuContext.php +++ b/src/HanyuuContext.php @@ -4,6 +4,9 @@ namespace Hanyuu; use RuntimeException; use Index\Data\IDbConnection; use Index\Data\DbTools; +use Index\Data\Migration\IDbMigrationRepo; +use Index\Data\Migration\DbMigrationManager; +use Index\Data\Migration\FsDbMigrationRepo; use Index\Http\HttpFx; use Index\Http\HttpRequest; use Index\Routing\IRouter; @@ -34,6 +37,14 @@ class HanyuuContext { return $result->next() ? $result->getInteger(0) : 0; } + public function createMigrationManager(): DbMigrationManager { + return new DbMigrationManager($this->dbConn, 'hau_' . DbMigrationManager::DEFAULT_TABLE); + } + + public function createMigrationRepo(): IDbMigrationRepo { + return new FsDbMigrationRepo(HAU_DIR_MIGRATIONS); + } + public function setUpHttp(): void { $this->router = new HttpFx; $this->router->use('/', function($response) { diff --git a/tools/migrate b/tools/migrate new file mode 100755 index 0000000..20cbee5 --- /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 = $hau->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(HAU_ROOT . '/.migrating'); +} diff --git a/tools/new-migration b/tools/new-migration new file mode 100755 index 0000000..918504b --- /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 = $hau->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;