Skip to content

Commit

Permalink
Migration support
Browse files Browse the repository at this point in the history
  • Loading branch information
ging-dev committed Jul 6, 2021
1 parent fbc62b1 commit 5cf0ed2
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
63 changes: 63 additions & 0 deletions src/Migration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace Gingdev\NetteExtension;

use Illuminate\Database\Capsule\Manager;
use Illuminate\Database\ConnectionResolver;
use Illuminate\Database\Migrations\DatabaseMigrationRepository;
use Illuminate\Database\Migrations\MigrationCreator;
use Illuminate\Database\Migrations\Migrator;
use Illuminate\Filesystem\Filesystem;
use Symfony\Component\Console\Output\OutputInterface;

class Migration
{
protected Migrator $migrator;

public function __construct()
{
$connection = Manager::connection();

$resolver = new ConnectionResolver(['default' => $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);
}
}
48 changes: 48 additions & 0 deletions src/command/MakeMigrationCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Gingdev\NetteExtension\Command;

use Exception;
use Gingdev\NetteExtension\Migration;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class MakeMigrationCommand extends Command
{
protected static $defaultName = 'make:migration';

protected function configure(): void
{
$this->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;
}
}
46 changes: 46 additions & 0 deletions src/command/MigrateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Gingdev\NetteExtension\Command;

use Gingdev\NetteExtension\Migration;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class MigrateCommand extends Command
{
protected static $defaultName = 'migrate';

protected function configure(): void
{
$this->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;
}
}
31 changes: 31 additions & 0 deletions src/stub/migration.create.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Capsule\Manager as Capsule;

class {{ class }} extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Capsule::schema()->create('{{ table }}', function (Blueprint $table) {
$table->id();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Capsule::schema()->dropIfExists('{{ table }}');
}
}
28 changes: 28 additions & 0 deletions src/stub/migration.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Capsule\Manager as Capsule;

class {{ class }} extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
32 changes: 32 additions & 0 deletions src/stub/migration.update.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Capsule\Manager as Capsule;

class {{ class }} extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Capsule::schema()->table('{{ table }}', function (Blueprint $table) {
//
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Capsule::schema()->table('{{ table }}', function (Blueprint $table) {
//
});
}
}

0 comments on commit 5cf0ed2

Please sign in to comment.