Added database migration tools.

This commit is contained in:
flash 2023-01-07 04:36:22 +00:00
parent 2c61a3b7f5
commit 07ad8bd721
6 changed files with 72 additions and 1 deletions

0
database/.gitkeep Normal file
View file

View file

@ -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');

@ -1 +1 @@
Subproject commit f8c6602ab953491a3e540d1ab9c77f2d885ee120
Subproject commit fbe4fe18decd502a0ca15ffe8a7c3b2d847349d5

View file

@ -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) {

34
tools/migrate Executable file
View file

@ -0,0 +1,34 @@
#!/usr/bin/env php
<?php
use Index\Data\Migration\FsDbMigrationRepo;
require_once __DIR__ . '/../hanyuu.php';
try {
touch(HAU_ROOT . '/.migrating');
chmod(HAU_ROOT . '/.migrating', 0777);
echo 'Creating migration manager...' . PHP_EOL;
$manager = $hau->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');
}

25
tools/new-migration Executable file
View file

@ -0,0 +1,25 @@
#!/usr/bin/env php
<?php
use Index\Data\Migration\FsDbMigrationRepo;
require_once __DIR__ . '/../hanyuu.php';
$repo = $hau->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;