From e13a0acce1fe2d80575c9dffcf90d55362328ae9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20P=C5=82aczek?= Date: Sun, 13 Oct 2024 20:33:30 +0200 Subject: [PATCH] Add parameter `recreate` to migration command. --- src/Command/Migrate.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Command/Migrate.php b/src/Command/Migrate.php index 558ceab..2ece48c 100644 --- a/src/Command/Migrate.php +++ b/src/Command/Migrate.php @@ -9,13 +9,25 @@ use Illuminate\Database\Schema\Blueprint; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; #[AsCommand(name: 'app:migrate', description: 'Create database and rum migrations')] class Migrate extends Command { + private const RECREATE_OPTION = 'recreate'; + protected function configure(): void { + $this->addOption(self::RECREATE_OPTION, null, InputOption::VALUE_OPTIONAL, 'Recreate database file event if exist and has data.'); + } + + public function execute(InputInterface $input, OutputInterface $output): int + { + if (true === $input->hasOption(self::RECREATE_OPTION)) { + unlink(__DIR__ . '/../../database.sqlite'); + //sleep(5); + } touch(__DIR__ . '/../../database.sqlite'); $capsule = new Capsule; $capsule->addConnection([ @@ -24,10 +36,6 @@ class Migrate extends Command ]); $capsule->setAsGlobal(); $capsule->bootEloquent(); - } - - public function execute(InputInterface $input, OutputInterface $output): int - { $this->createProductsTable(); $this->createPricesTable(); $this->createStocksTable();