-
-
Notifications
You must be signed in to change notification settings - Fork 133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cycle orm migrations #25
Closed
Closed
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
67c26f2
Added consoled command `migrate/generate`
roxblnfk bc7eb85
Added consoled command `migrate/up`
roxblnfk 8491f2b
Added consoled command `migrate/down`
roxblnfk fc48834
Added consoled command `migrate/list`
roxblnfk 533c3e3
Cleanup;
roxblnfk e0ec67a
Style fix
roxblnfk 0147b88
Decided two todos in common.php - added EntityFinderHelper class;
roxblnfk 1b9f6ed
Refactoring:
roxblnfk 0d036f8
Code style hotfix
roxblnfk 7690e6a
Fix formatting [skip ci]
samdark a82a41b
Fix formatting [skip ci]
samdark 80817b4
Fix formatting
samdark c2563c7
Remove unneeded imports
samdark f8a7b79
Add missing return types
samdark e84e78d
Drop cached schema when migration up/down
roxblnfk e1e929f
added checking unapplied migrations
roxblnfk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -1,8 +1,29 @@ | ||
<?php | ||
|
||
|
||
use App\ConsoleCommand\CreateUser; | ||
use App\Console\Command\CreateUser; | ||
use App\Factory\CycleMigratorFactory; | ||
use Psr\Container\ContainerInterface; | ||
use Spiral\Migrations\Config\MigrationConfig; | ||
use Spiral\Migrations\Migrator; | ||
use Yiisoft\Aliases\Aliases; | ||
|
||
$params = $params ?? []; | ||
|
||
return [ | ||
|
||
CreateUser::class => CreateUser::class, | ||
|
||
// Cycle Migrations | ||
Migrator::class => new CycleMigratorFactory(), | ||
// Migration Config | ||
MigrationConfig::class => function (ContainerInterface $container) { | ||
$aliases = $container->get(Aliases::class); | ||
return new MigrationConfig([ | ||
'directory' => $aliases->get('@src/Console/Migration'), | ||
'namespace' => 'App\Console\Migration', | ||
'table' => 'migration', | ||
'safe' => false, | ||
]); | ||
} | ||
]; |
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
3 changes: 1 addition & 2 deletions
3
src/ConsoleCommand/CreateUser.php → src/Console/Command/CreateUser.php
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,77 @@ | ||
<?php | ||
namespace App\Console\Command; | ||
|
||
use App\Helper\CycleOrmHelper; | ||
use Spiral\Migrations\Config\MigrationConfig; | ||
use Spiral\Migrations\MigrationInterface; | ||
use Spiral\Migrations\Migrator; | ||
use Spiral\Migrations\State; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class MigrateDown extends Command | ||
{ | ||
protected static $defaultName = 'migrate/down'; | ||
|
||
/** @var MigrationConfig */ | ||
private $config; | ||
|
||
/** @var Migrator */ | ||
private $migrator; | ||
|
||
/** @var CycleOrmHelper */ | ||
private $cycleOrmHelper; | ||
|
||
/** | ||
* MigrateGenerateCommand constructor. | ||
* @param Migrator $migrator | ||
* @param MigrationConfig $conf | ||
* @param CycleOrmHelper $cycleOrmHelper | ||
*/ | ||
public function __construct(Migrator $migrator, MigrationConfig $conf, CycleOrmHelper $cycleOrmHelper) | ||
{ | ||
parent::__construct(); | ||
$this->config = $conf; | ||
$this->migrator = $migrator; | ||
$this->cycleOrmHelper = $cycleOrmHelper; | ||
} | ||
|
||
public function configure(): void | ||
{ | ||
$this | ||
->setDescription('Rollback last migration'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
// drop cached schema | ||
$this->cycleOrmHelper->dropCurrentSchemaCache(); | ||
|
||
$list = $this->migrator->getMigrations(); | ||
$output->writeln('<fg=green>' . count($list) . ' migrations found in ' . $this->config->getDirectory() . '</fg=green>'); | ||
|
||
$statuses = [ | ||
State::STATUS_UNDEFINED => 'undefined', | ||
State::STATUS_PENDING => 'pending', | ||
State::STATUS_EXECUTED => 'executed', | ||
]; | ||
try { | ||
$migration = $this->migrator->rollback(); | ||
if (!$migration instanceof MigrationInterface) { | ||
throw new \Exception('Migration not found'); | ||
} | ||
|
||
$state = $migration->getState(); | ||
$status = $state->getStatus(); | ||
$output->writeln($state->getName() . ': ' . ($statuses[$status] ?? $status)); | ||
} catch (\Throwable $e) { | ||
$output->writeln([ | ||
'===================', | ||
'Error!', | ||
$e->getMessage(), | ||
]); | ||
return; | ||
} | ||
} | ||
} |
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,54 @@ | ||
<?php | ||
namespace App\Console\Command; | ||
|
||
use App\Helper\CycleOrmHelper; | ||
use Spiral\Migrations\Migrator; | ||
use Spiral\Migrations\Config\MigrationConfig; | ||
use Spiral\Migrations\State; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class MigrateGenerate extends Command | ||
{ | ||
protected static $defaultName = 'migrate/generate'; | ||
|
||
/** @var Migrator */ | ||
private $migrator; | ||
|
||
/** @var CycleOrmHelper */ | ||
private $cycleHelper; | ||
|
||
/** @var MigrationConfig */ | ||
private $config; | ||
|
||
public function __construct( | ||
Migrator $migrator, | ||
MigrationConfig $conf, | ||
CycleOrmHelper $cycleHelper | ||
) { | ||
parent::__construct(); | ||
$this->migrator = $migrator; | ||
$this->config = $conf; | ||
$this->cycleHelper = $cycleHelper; | ||
} | ||
|
||
public function configure(): void | ||
{ | ||
$this->setDescription('Generates a migration'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): void | ||
{ | ||
// check existing unapplied migrations | ||
$list = $this->migrator->getMigrations(); | ||
foreach ($list as $migration) { | ||
if ($migration->getState()->getStatus() !== State::STATUS_EXECUTED) { | ||
$output->writeln('<fg=red>Outstanding migrations found, run `migrate/up` first.</fg=red>'); | ||
return; | ||
} | ||
} | ||
|
||
$this->cycleHelper->generateMigration($this->migrator, $this->config); | ||
} | ||
} |
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,54 @@ | ||
<?php | ||
namespace App\Console\Command; | ||
|
||
use Spiral\Migrations\Config\MigrationConfig; | ||
use Spiral\Migrations\Migrator; | ||
use Spiral\Migrations\State; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class MigrateList extends Command | ||
{ | ||
protected static $defaultName = 'migrate/list'; | ||
|
||
/** @var MigrationConfig */ | ||
private $config; | ||
/** @var Migrator */ | ||
private $migrator; | ||
/** | ||
* MigrateGenerateCommand constructor. | ||
* @param Migrator $migrator | ||
* @param MigrationConfig $conf | ||
*/ | ||
public function __construct(Migrator $migrator, MigrationConfig $conf) | ||
{ | ||
$this->config = $conf; | ||
$this->migrator = $migrator; | ||
parent::__construct(); | ||
} | ||
|
||
public function configure(): void | ||
{ | ||
$this | ||
->setDescription('Print list of all migrations'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$list = $this->migrator->getMigrations(); | ||
$output->writeln('<fg=green>' . count($list) . ' migrations found in ' . $this->config->getDirectory() . ':</fg=green>'); | ||
|
||
$statuses = [ | ||
State::STATUS_UNDEFINED => 'undefined', | ||
State::STATUS_PENDING => 'pending', | ||
State::STATUS_EXECUTED => 'executed', | ||
]; | ||
$list = $this->migrator->getMigrations(); | ||
|
||
foreach ($list as $migration) { | ||
$state = $migration->getState(); | ||
$output->writeln($state->getName() . ' <fg=yellow>[' . ($statuses[$state->getStatus()] ?? '?') . ']</fg=yellow>'); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wolfy-j suggested that such check is needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cycle will generate migrations based on diff with current DB schema if the schema is not consistent you will generate broken migrations. This is a small nasty issue, so such check is recommended.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand and support this recommendation. Also, it would be nice not to generate empty migrations
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm... Why would empty migrations be generated?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are checking if migration is required via Generator observer: https://github.com/spiral/framework/blob/master/src/Command/Cycle/MigrateCommand.php#L78
You are free to copy its code, it also gives you a nice description of what have changed.
https://github.com/spiral/framework/blob/master/src/Command/Cycle/Generator/ShowChanges.php
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ref cycle/migrations#4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 1.0.5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One question. How would it work if you write migration manually? Would that still create dummy migration classes for me?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ORM and manual migrations works using the same engine, you can combine them if you want. Basically ORM simply writes migrations for you, the rest is the same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/cycle/migrations/blob/master/src/GenerateMigrations.php#L87