diff --git a/composer.json b/composer.json index d0077bc..d88513f 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,8 @@ "minimum-stability": "stable", "require": { "illuminate/database": "^8.44", - "illuminate/events": "^8.44" + "illuminate/events": "^8.44", + "illuminate/filesystem": "^8.49" }, "require-dev": { "nette/utils": "^3.2", diff --git a/src/Migration.php b/src/Migration.php new file mode 100644 index 0000000..48b71ea --- /dev/null +++ b/src/Migration.php @@ -0,0 +1,63 @@ + $connection]); + $resolver->setDefaultConnection('default'); + + $repository = new DatabaseMigrationRepository($resolver, 'migrations'); + + if (!$repository->repositoryExists()) { + $repository->createRepository(); + } + + $this->migrator = new Migrator($repository, $resolver, new Filesystem()); + } + + protected function getMigrationPath() + { + return getcwd().DIRECTORY_SEPARATOR.'migrations'; + } + + public function create(string $name, ?string $table, bool $create = false) + { + $creator = new MigrationCreator(new Filesystem(), __DIR__.DIRECTORY_SEPARATOR.'stub'); + + $path = $creator->create($name, $this->getMigrationPath(), $table, $create); + + return pathinfo($path, PATHINFO_FILENAME); + } + + public function setMigratorOutput(OutputInterface $output): void + { + $this->migrator->setOutput($output); + } + + public function run(array $options = []): array + { + return $this->migrator->run($this->getMigrationPath(), $options); + } + + public function rollback(array $options = []): array + { + return $this->migrator->rollback($this->getMigrationPath(), $options); + } +} diff --git a/src/command/MakeMigrationCommand.php b/src/command/MakeMigrationCommand.php new file mode 100644 index 0000000..8fcade5 --- /dev/null +++ b/src/command/MakeMigrationCommand.php @@ -0,0 +1,48 @@ +setDescription('Creates a new migration') + ->setHelp('This command allows you to create a migration.') + ->addArgument('name', InputArgument::REQUIRED, 'Migration name.') + ->addOption('table', 't', InputOption::VALUE_OPTIONAL, 'The name of the table that will be specified in the migration code.') + ->addOption('create', null, InputOption::VALUE_NONE, 'Migration to create a table.'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $style = new SymfonyStyle($input, $output); + + try { + $migration = new Migration(); + $migration_name = $migration->create( + $input->getArgument('name'), + $input->getOption('table'), + $input->getOption('create'), + ); + + $style->success('Migration was created: '.$migration_name); + } catch (Exception $exception) { + $style->error($exception->getMessage()); + } + + return Command::SUCCESS; + } +} diff --git a/src/command/MigrateCommand.php b/src/command/MigrateCommand.php new file mode 100644 index 0000000..bdc02cd --- /dev/null +++ b/src/command/MigrateCommand.php @@ -0,0 +1,46 @@ +setDescription('Run or rollback migrations') + ->setHelp('The command allows you to run and rollback migrations.') + ->addOption('pretend', null, InputOption::VALUE_NONE, 'Show SQL queries without performing migration.') + ->addOption('step', null, InputOption::VALUE_OPTIONAL, 'Force the migrations to be run so they can be rolled back individually. The number of migrations to be reverted when running rollback command.') + ->addOption('rollback', null, InputOption::VALUE_NONE, 'Rollback.'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $migrations = new Migration(); + $migrations->setMigratorOutput($output); + + $options = [ + 'pretend' => $input->getOption('pretend'), + 'step' => $input->getOption('step'), + ]; + + if ($input->getOption('rollback')) { + $migrations->rollback($options); + + return Command::SUCCESS; + } + + $migrations->run($options); + + return Command::SUCCESS; + } +} diff --git a/src/stub/migration.create.stub b/src/stub/migration.create.stub new file mode 100644 index 0000000..7ae79d1 --- /dev/null +++ b/src/stub/migration.create.stub @@ -0,0 +1,31 @@ +create('{{ table }}', function (Blueprint $table) { + $table->id(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Capsule::schema()->dropIfExists('{{ table }}'); + } +} diff --git a/src/stub/migration.stub b/src/stub/migration.stub new file mode 100644 index 0000000..2b93e84 --- /dev/null +++ b/src/stub/migration.stub @@ -0,0 +1,28 @@ +table('{{ table }}', function (Blueprint $table) { + // + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Capsule::schema()->table('{{ table }}', function (Blueprint $table) { + // + }); + } +}