Added database migration tools.

This commit is contained in:
flash 2023-01-07 21:16:49 +00:00
parent f66001a4cf
commit 1c38976b95
5 changed files with 89 additions and 1 deletions

View file

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

View file

@ -0,0 +1,18 @@
<?php
use Index\Data\IDbConnection;
use Index\Data\Migration\IDbMigration;
final class InitialStructure_20230107_191402 implements IDbMigration {
public function migrate(IDbConnection $conn): void {
$conn->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;
');
}
}

View file

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

34
tools/migrate Executable file
View file

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

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__ . '/../awaki.php';
$repo = $awk->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;