-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
250 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }}'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
// | ||
}); | ||
} | ||
} |