From 649e37ab0f209b97e19565becc9896248aec41f4 Mon Sep 17 00:00:00 2001 From: slawkens Date: Sun, 4 Feb 2024 08:16:22 +0100 Subject: [PATCH] "php aac migrate:run {ids}" command --- system/src/Commands/MigrateRunCommand.php | 62 +++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 system/src/Commands/MigrateRunCommand.php diff --git a/system/src/Commands/MigrateRunCommand.php b/system/src/Commands/MigrateRunCommand.php new file mode 100644 index 00000000..ee0265b3 --- /dev/null +++ b/system/src/Commands/MigrateRunCommand.php @@ -0,0 +1,62 @@ +setName('migrate:run') + ->setDescription('This command runs the migration specified by id') + ->addArgument('id', + InputArgument::IS_ARRAY | InputArgument::REQUIRED, + 'Id or ids of migration(s)' + ); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $io = new SymfonyStyle($input, $output); + + $ids = $input->getArgument('id'); + + // pre-check + // in case one of the migrations doesn't exist - we won't execute any of them + foreach ($ids as $id) { + if (!$this->migrationExists($id)) { + $io->error([ + "One of the migrations specified doesnt exist: $id", + "Please check it and re-run the command", + "No migration has been executed", + ]); + + return Command::FAILURE; + } + } + + foreach ($ids as $id) { + $this->executeMigration($id, $io); + } + + return Command::SUCCESS; + } + + private function migrationExists($id): bool { + return file_exists(SYSTEM . 'migrations/' . $id . '.php'); + } + + private function executeMigration($id, $io): void + { + global $db; + + $db->revalidateCache(); + + require SYSTEM . 'migrations/' . $id . '.php'; + $io->success('Migration ' . $id . ' successfully executed'); + } +}