Skip to content

Commit 5cf0ed2

Browse files
committed
Migration support
1 parent fbc62b1 commit 5cf0ed2

File tree

7 files changed

+250
-1
lines changed

7 files changed

+250
-1
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"minimum-stability": "stable",
1313
"require": {
1414
"illuminate/database": "^8.44",
15-
"illuminate/events": "^8.44"
15+
"illuminate/events": "^8.44",
16+
"illuminate/filesystem": "^8.49"
1617
},
1718
"require-dev": {
1819
"nette/utils": "^3.2",

src/Migration.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Gingdev\NetteExtension;
6+
7+
use Illuminate\Database\Capsule\Manager;
8+
use Illuminate\Database\ConnectionResolver;
9+
use Illuminate\Database\Migrations\DatabaseMigrationRepository;
10+
use Illuminate\Database\Migrations\MigrationCreator;
11+
use Illuminate\Database\Migrations\Migrator;
12+
use Illuminate\Filesystem\Filesystem;
13+
use Symfony\Component\Console\Output\OutputInterface;
14+
15+
class Migration
16+
{
17+
protected Migrator $migrator;
18+
19+
public function __construct()
20+
{
21+
$connection = Manager::connection();
22+
23+
$resolver = new ConnectionResolver(['default' => $connection]);
24+
$resolver->setDefaultConnection('default');
25+
26+
$repository = new DatabaseMigrationRepository($resolver, 'migrations');
27+
28+
if (!$repository->repositoryExists()) {
29+
$repository->createRepository();
30+
}
31+
32+
$this->migrator = new Migrator($repository, $resolver, new Filesystem());
33+
}
34+
35+
protected function getMigrationPath()
36+
{
37+
return getcwd().DIRECTORY_SEPARATOR.'migrations';
38+
}
39+
40+
public function create(string $name, ?string $table, bool $create = false)
41+
{
42+
$creator = new MigrationCreator(new Filesystem(), __DIR__.DIRECTORY_SEPARATOR.'stub');
43+
44+
$path = $creator->create($name, $this->getMigrationPath(), $table, $create);
45+
46+
return pathinfo($path, PATHINFO_FILENAME);
47+
}
48+
49+
public function setMigratorOutput(OutputInterface $output): void
50+
{
51+
$this->migrator->setOutput($output);
52+
}
53+
54+
public function run(array $options = []): array
55+
{
56+
return $this->migrator->run($this->getMigrationPath(), $options);
57+
}
58+
59+
public function rollback(array $options = []): array
60+
{
61+
return $this->migrator->rollback($this->getMigrationPath(), $options);
62+
}
63+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Gingdev\NetteExtension\Command;
6+
7+
use Exception;
8+
use Gingdev\NetteExtension\Migration;
9+
use Symfony\Component\Console\Command\Command;
10+
use Symfony\Component\Console\Input\InputArgument;
11+
use Symfony\Component\Console\Input\InputInterface;
12+
use Symfony\Component\Console\Input\InputOption;
13+
use Symfony\Component\Console\Output\OutputInterface;
14+
use Symfony\Component\Console\Style\SymfonyStyle;
15+
16+
class MakeMigrationCommand extends Command
17+
{
18+
protected static $defaultName = 'make:migration';
19+
20+
protected function configure(): void
21+
{
22+
$this->setDescription('Creates a new migration')
23+
->setHelp('This command allows you to create a migration.')
24+
->addArgument('name', InputArgument::REQUIRED, 'Migration name.')
25+
->addOption('table', 't', InputOption::VALUE_OPTIONAL, 'The name of the table that will be specified in the migration code.')
26+
->addOption('create', null, InputOption::VALUE_NONE, 'Migration to create a table.');
27+
}
28+
29+
protected function execute(InputInterface $input, OutputInterface $output): int
30+
{
31+
$style = new SymfonyStyle($input, $output);
32+
33+
try {
34+
$migration = new Migration();
35+
$migration_name = $migration->create(
36+
$input->getArgument('name'),
37+
$input->getOption('table'),
38+
$input->getOption('create'),
39+
);
40+
41+
$style->success('Migration was created: '.$migration_name);
42+
} catch (Exception $exception) {
43+
$style->error($exception->getMessage());
44+
}
45+
46+
return Command::SUCCESS;
47+
}
48+
}

src/command/MigrateCommand.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Gingdev\NetteExtension\Command;
6+
7+
use Gingdev\NetteExtension\Migration;
8+
use Symfony\Component\Console\Command\Command;
9+
use Symfony\Component\Console\Input\InputInterface;
10+
use Symfony\Component\Console\Input\InputOption;
11+
use Symfony\Component\Console\Output\OutputInterface;
12+
13+
class MigrateCommand extends Command
14+
{
15+
protected static $defaultName = 'migrate';
16+
17+
protected function configure(): void
18+
{
19+
$this->setDescription('Run or rollback migrations')
20+
->setHelp('The command allows you to run and rollback migrations.')
21+
->addOption('pretend', null, InputOption::VALUE_NONE, 'Show SQL queries without performing migration.')
22+
->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.')
23+
->addOption('rollback', null, InputOption::VALUE_NONE, 'Rollback.');
24+
}
25+
26+
protected function execute(InputInterface $input, OutputInterface $output): int
27+
{
28+
$migrations = new Migration();
29+
$migrations->setMigratorOutput($output);
30+
31+
$options = [
32+
'pretend' => $input->getOption('pretend'),
33+
'step' => $input->getOption('step'),
34+
];
35+
36+
if ($input->getOption('rollback')) {
37+
$migrations->rollback($options);
38+
39+
return Command::SUCCESS;
40+
}
41+
42+
$migrations->run($options);
43+
44+
return Command::SUCCESS;
45+
}
46+
}

src/stub/migration.create.stub

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Capsule\Manager as Capsule;
6+
7+
class {{ class }} extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Capsule::schema()->create('{{ table }}', function (Blueprint $table) {
17+
$table->id();
18+
$table->timestamps();
19+
});
20+
}
21+
22+
/**
23+
* Reverse the migrations.
24+
*
25+
* @return void
26+
*/
27+
public function down()
28+
{
29+
Capsule::schema()->dropIfExists('{{ table }}');
30+
}
31+
}

src/stub/migration.stub

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Capsule\Manager as Capsule;
6+
7+
class {{ class }} extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
//
17+
}
18+
19+
/**
20+
* Reverse the migrations.
21+
*
22+
* @return void
23+
*/
24+
public function down()
25+
{
26+
//
27+
}
28+
}

src/stub/migration.update.stub

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Capsule\Manager as Capsule;
6+
7+
class {{ class }} extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Capsule::schema()->table('{{ table }}', function (Blueprint $table) {
17+
//
18+
});
19+
}
20+
21+
/**
22+
* Reverse the migrations.
23+
*
24+
* @return void
25+
*/
26+
public function down()
27+
{
28+
Capsule::schema()->table('{{ table }}', function (Blueprint $table) {
29+
//
30+
});
31+
}
32+
}

0 commit comments

Comments
 (0)