mirror of
https://github.com/slawkens/myaac.git
synced 2025-10-26 14:04:17 +01:00
Feature migrations up/down (#270)
* Migrations up down * Add forum model * Syntactic sugar for db structure changes * Refactor migrations with $up & $down * Fix migrations upgrade and downgrade + Add option to disable auto migrate * Add migrate:to command Usage: php aac migrate:to x (x - database version) * Show error when mail is not enabled * Fixes regarding to init.php * Add migrate command to manually upgrade db, incase auto migrate is disabled * Fixed rest of the migrations * Limit max version of database * Don't allow minus number * Option to clear specified plugin settings by name * Version is required * Fix PHPStan errors * Unset $up after migration, to prevent executing same migration twice * Add database version to output * This is not needed * Update 5.php * Set database_auto_migrate on install * Set blank & color only if current db version supports it * Fix duplicate function declaration
This commit is contained in:
108
system/src/Commands/MigrateToCommand.php
Normal file
108
system/src/Commands/MigrateToCommand.php
Normal file
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
namespace MyAAC\Commands;
|
||||
|
||||
use MyAAC\Models\Config;
|
||||
use POT;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
class MigrateToCommand extends Command
|
||||
{
|
||||
protected function configure(): void
|
||||
{
|
||||
$this->setName('migrate:to')
|
||||
->setDescription('This command migrates to specific version of database')
|
||||
->addArgument('version',
|
||||
InputArgument::REQUIRED,
|
||||
'Version number'
|
||||
);
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
|
||||
$versionDest = $input->getArgument('version');
|
||||
|
||||
if (!$versionDest || $versionDest > DATABASE_VERSION || $versionDest < 1) {
|
||||
$io->error('Please enter a valid version number');
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$this->initEnv();
|
||||
|
||||
$currentVersion = Config::where('name', 'database_version')->first()->value;
|
||||
if ($currentVersion > $versionDest) {
|
||||
// downgrade
|
||||
for ($i = $currentVersion; $i > $versionDest; $i--) {
|
||||
echo $i . ' ';
|
||||
$this->executeMigration($i, false);
|
||||
}
|
||||
}
|
||||
else if ($currentVersion < $versionDest) {
|
||||
// upgrade
|
||||
for ($i = $currentVersion + 1; $i <= $versionDest; $i++) {
|
||||
echo $i . ' ';
|
||||
$this->executeMigration($i, true);
|
||||
}
|
||||
}
|
||||
else {
|
||||
$io->success('Nothing to be done');
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
$upgrade = ($currentVersion < $versionDest ? 'Upgrade' : 'Downgrade');
|
||||
$io->success("Migration ({$upgrade}) to version {$versionDest} has been completed");
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
private function executeMigration($id, $_up): void
|
||||
{
|
||||
global $db;
|
||||
|
||||
$db->revalidateCache();
|
||||
|
||||
require SYSTEM . 'migrations/' . $id . '.php';
|
||||
if ($_up) {
|
||||
if (isset($up)) {
|
||||
$up();
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (isset($down)) {
|
||||
$down();
|
||||
}
|
||||
}
|
||||
|
||||
updateDatabaseConfig('database_version', ($_up ? $id : $id - 1));
|
||||
}
|
||||
|
||||
private function initEnv()
|
||||
{
|
||||
global $config;
|
||||
if (!isset($config['installed']) || !$config['installed']) {
|
||||
throw new \RuntimeException('MyAAC has not been installed yet or there was error during installation. Please install again.');
|
||||
}
|
||||
|
||||
if(empty($config['server_path'])) {
|
||||
throw new \RuntimeException('Server Path has been not set. Go to config.php and set it.');
|
||||
}
|
||||
|
||||
// take care of trailing slash at the end
|
||||
if($config['server_path'][strlen($config['server_path']) - 1] !== '/')
|
||||
$config['server_path'] .= '/';
|
||||
|
||||
$config['lua'] = load_config_lua($config['server_path'] . 'config.lua');
|
||||
|
||||
// POT
|
||||
require_once SYSTEM . 'libs/pot/OTS.php';
|
||||
$ots = POT::getInstance();
|
||||
$eloquentConnection = null;
|
||||
|
||||
require_once SYSTEM . 'database.php';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user