2
0
Fork 0
forked from flashii/eeprom

Added EEPROM database migrations.

This commit is contained in:
flash 2023-11-07 17:12:47 +00:00
parent 1422ddb526
commit d6800f9b6f
5 changed files with 62 additions and 8 deletions

View file

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

30
src/DatabaseContext.php Normal file
View file

@ -0,0 +1,30 @@
<?php
namespace EEPROM;
use Index\Data\IDbConnection;
use Index\Data\Migration\IDbMigrationRepo;
use Index\Data\Migration\DbMigrationManager;
use Index\Data\Migration\FsDbMigrationRepo;
class DatabaseContext {
public function __construct(
private IDbConnection $connection
) {}
public function getConnection(): IDbConnection {
return $this->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);
}
}

23
src/EEPROMContext.php Normal file
View file

@ -0,0 +1,23 @@
<?php
namespace EEPROM;
use Index\Data\IDbConnection;
use Syokuhou\IConfig;
class EEPROMContext {
private IConfig $config;
private DatabaseContext $dbCtx;
public function __construct(IConfig $config, IDbConnection $dbConn) {
$this->config = $config;
$this->dbCtx = new DatabaseContext($dbConn);
}
public function getConfig(): IConfig {
return $this->config;
}
public function getDatabase(): DatabaseContext {
return $this->dbCtx;
}
}

View file

@ -1,22 +1,21 @@
#!/usr/bin/env php
<?php
use Index\Data\Migration\DbMigrationManager;
use Index\Data\Migration\FsDbMigrationRepo;
require_once __DIR__ . '/../eeprom.php';
try {
touch(PRM_ROOT . '/.migrating');
chmod(PRM_ROOT . '/.migrating', 0777);
$db = $eeprom->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);

View file

@ -1,18 +1,18 @@
#!/usr/bin/env php
<?php
use Index\Data\Migration\DbMigrationManager;
use Index\Data\Migration\FsDbMigrationRepo;
require_once __DIR__ . '/../eeprom.php';
$repo = new FsDbMigrationRepo(PRM_MIGRATIONS);
$db = $eeprom->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);