diff --git a/system/src/Commands/MigrateToCommand.php b/system/src/Commands/MigrateToCommand.php new file mode 100644 index 00000000..f17c6437 --- /dev/null +++ b/system/src/Commands/MigrateToCommand.php @@ -0,0 +1,108 @@ +setName('migrate:to') + ->setDescription('This command migrates to specific version of database') + ->addArgument('version', + InputArgument::OPTIONAL, + 'Version number' + ); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $io = new SymfonyStyle($input, $output); + + $versionDest = $input->getArgument('version'); + + if (!$versionDest) { + $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; + global $db; + require_once SYSTEM . 'database.php'; + } +}