-
Notifications
You must be signed in to change notification settings - Fork 5
Add migrate command #7
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
Open
jameshalsall
wants to merge
6
commits into
master
Choose a base branch
from
add-migrate-command
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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 hidden or 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,17 @@ | ||
| # Changelog | ||
|
|
||
| Notable changes to this project will be documented in this file. | ||
| The format is based on [keep a changelog](http://keepachangelog.com/en/1.0.0/) principles | ||
| and adheres to [semantic versioning](http://semver.org/spec/v2.0.0.html). | ||
|
|
||
| ## 1.1.0 - Unreleased | ||
|
|
||
| * Add new `bin/magento mx:db:migrate` command that handles migrations seamlessly. | ||
|
|
||
| ## 1.0.1 - 2018-07-05 | ||
|
|
||
| * Remove the auto-generation of the `phinx.php` configuration file. | ||
|
|
||
| ## 1.0.0 - 2018-07-04 | ||
|
|
||
| * Initial release. |
This file contains hidden or 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 hidden or 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,13 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||
| <type name="Magento\Framework\Console\CommandList"> | ||
| <arguments> | ||
| <argument name="commands" xsi:type="array"> | ||
| <item name="mx_db_migrate" xsi:type="object"> | ||
| MX\PhinxMigrations\Console\Command\MigrateCommand | ||
| </item> | ||
| </argument> | ||
| </arguments> | ||
| </type> | ||
| </config> |
This file contains hidden or 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,69 @@ | ||
| <?php | ||
|
|
||
| namespace MX\PhinxMigrations\Console\Command; | ||
|
|
||
| use Magento\Framework\Console\Cli; | ||
| use Magento\Setup\Console\Command\DbStatusCommand; | ||
| use Symfony\Component\Console\Command\Command; | ||
| use Symfony\Component\Console\Input\ArrayInput; | ||
| use Symfony\Component\Console\Input\InputInterface; | ||
| use Symfony\Component\Console\Input\InputOption; | ||
| use Symfony\Component\Console\Output\NullOutput; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
| use Symfony\Component\Process\Process; | ||
|
|
||
| /** | ||
| * @package MX\PhinxMigrations | ||
| * @author James Halsall <james.halsall@inviqa.com> | ||
| */ | ||
| class MigrateCommand extends Command | ||
| { | ||
| private const MIGRATE_TIMEOUT = 900; | ||
| private const PHINX_BIN = 'vendor/robmorgan/phinx/bin/phinx'; | ||
|
|
||
| protected function configure() | ||
| { | ||
| $this | ||
| ->setName('mx:db:migrate') | ||
| ->setDescription('Migrates the database so it is ready for use with the current application code.'); | ||
|
|
||
| $this->addOption( | ||
| 'keep-generated', | ||
| null, | ||
| InputOption::VALUE_NONE, | ||
| 'Prevents generated files from being deleted during migration.' | ||
| ); | ||
| } | ||
|
|
||
| protected function execute(InputInterface $input, OutputInterface $output) | ||
| { | ||
| $dbStatusCommand = $this->getApplication()->get('setup:db:status'); | ||
|
|
||
| $output->writeln('<info>Checking if setup:upgrade is required...</info>'); | ||
| $exit = $dbStatusCommand->run(new ArrayInput([]), new NullOutput()); | ||
|
|
||
| if ($exit === DbStatusCommand::EXIT_CODE_UPGRADE_REQUIRED) { | ||
| $output->writeln('<info>A setup:upgrade is required, running...</info>'); | ||
| $setupUpgradeCommand = $this->getApplication()->get('setup:upgrade'); | ||
|
|
||
| $setupUpgradeArgs = $input->getOption('keep-generated') ? ['--keep-generated' => true] : []; | ||
| $setupUpgradeCommand->run(new ArrayInput($setupUpgradeArgs), $output); | ||
| } else { | ||
| $output->writeln('<info>No setup:upgrade required.</info>'); | ||
| } | ||
|
|
||
| return $this->runPhinxMigrate(); | ||
| } | ||
|
|
||
| protected function runPhinxMigrate(): int | ||
| { | ||
| $phinx = new Process(sprintf('%s migrate', self::PHINX_BIN)); | ||
|
|
||
| $phinx->setTimeout(self::MIGRATE_TIMEOUT); | ||
| $phinx->run(function ($type, $buffer) { | ||
| echo $buffer; | ||
| }); | ||
|
|
||
| return $phinx->isSuccessful() ? Cli::RETURN_SUCCESS : Cli::RETURN_FAILURE; | ||
| } | ||
| } | ||
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.
what happens when we add a completely new module does this return the "upgrade required" status?
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.
Yeah it does, but that’s OK
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 just tested it by creating a new module in the src folder and the
setup:db:statussays everything is up to date, while the new module is obviously not registered in the database yet. 🤔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 will investigate this further
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.
From testing you are right, the
setup:db:statuscommand only tells us if the DB has out-of-date modules and not if it is completely missing a module. Will have to look at something bespoke for this I think.