From 56285bfb2a2c4cc5e69a8c3bb765bddc2a050a62 Mon Sep 17 00:00:00 2001 From: James Halsall Date: Fri, 7 Sep 2018 17:05:09 +0100 Subject: [PATCH 1/6] Add new bin/magento mx:migrate command --- README.md | 12 +++++-- etc/di.xml | 13 +++++++ src/Console/Command/MigrateCommand.php | 48 ++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 etc/di.xml create mode 100644 src/Console/Command/MigrateCommand.php diff --git a/README.md b/README.md index 1511098..3dca80a 100644 --- a/README.md +++ b/README.md @@ -117,9 +117,17 @@ We have access to 3 different setup helpers provided by Magento in each migratio ### Execute migrations -When you are happy with your migration you can execute it as follows +This module provides a new command that wraps up the whole DB migration process - bin/phinx migrate + bin/magento mx:migrate + +The new command does the following: + +1. Checks if a `setup:upgrade` is required (e.g. if a 3rd party module was updated or Magento was upgraded) +2. Runs `setup:upgrade` if it is required +3. Runs `bin/phinx migrate` + +This should replace the `bin/magento setup:upgrade` command in your deployment and development workflow. ### Rolling back diff --git a/etc/di.xml b/etc/di.xml new file mode 100644 index 0000000..fb9a127 --- /dev/null +++ b/etc/di.xml @@ -0,0 +1,13 @@ + + + + + + + MX\PhinxMigrations\Console\Command\MigrateCommand + + + + + diff --git a/src/Console/Command/MigrateCommand.php b/src/Console/Command/MigrateCommand.php new file mode 100644 index 0000000..12b57c3 --- /dev/null +++ b/src/Console/Command/MigrateCommand.php @@ -0,0 +1,48 @@ + + */ +class MigrateCommand extends Command +{ + protected function configure() + { + $this + ->setName('mx:migrate') + ->setDescription('Migrates the database so it is ready for use with the current application code.'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $dbStatusCommand = $this->getApplication()->get('setup:db:status'); + + $output->writeln('Checking if setup:upgrade is required...'); + $exit = $dbStatusCommand->run(new ArrayInput([]), new NullOutput()); + + if ($exit === DbStatusCommand::EXIT_CODE_UPGRADE_REQUIRED) { + $output->writeln('A setup:upgrade is required, running...'); + $setupUpgradeCommand = $this->getApplication()->get('setup:upgrade'); + $setupUpgradeCommand->run(new ArrayInput([]), $output); + } else { + $output->writeln('No setup:upgrade required.'); + } + + $phinx = new Process('bin/phinx migrate'); + $phinx->run(function ($type, $buffer) { + echo $buffer; + }); + + return $phinx->isSuccessful() ? 0 : 1; + } +} From ac4c75b709621abf8ff8913556299d9cc17f41fa Mon Sep 17 00:00:00 2001 From: James Halsall Date: Fri, 7 Sep 2018 17:07:32 +0100 Subject: [PATCH 2/6] Add changelog --- CHANGELOG.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..7cd74db --- /dev/null +++ b/CHANGELOG.md @@ -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 + +* Add new `bin/magento mx:migrate` command that handles migrations seamlessly. + +## 1.0.1 + +* Remove the auto-generation of the `phinx.php` configuration file. + +## 1.0.0 + +* Initial release. From 9aaaf8beab9f97acbef0a00a7a8f473f42649d0d Mon Sep 17 00:00:00 2001 From: James Halsall Date: Fri, 7 Sep 2018 17:11:21 +0100 Subject: [PATCH 3/6] Add dates to changelog --- CHANGELOG.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7cd74db..8b30d8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,14 +4,14 @@ 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 +## 1.1.0 - Unreleased * Add new `bin/magento mx:migrate` command that handles migrations seamlessly. -## 1.0.1 +## 1.0.1 - 2018-07-05 * Remove the auto-generation of the `phinx.php` configuration file. -## 1.0.0 +## 1.0.0 - 2018-07-04 * Initial release. From 9ca6471245ec32acd38905071d6026417ff7d560 Mon Sep 17 00:00:00 2001 From: James Halsall Date: Sun, 9 Sep 2018 16:25:43 +0100 Subject: [PATCH 4/6] Rename command --- CHANGELOG.md | 2 +- README.md | 2 +- src/Console/Command/MigrateCommand.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b30d8a..516e1bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ and adheres to [semantic versioning](http://semver.org/spec/v2.0.0.html). ## 1.1.0 - Unreleased -* Add new `bin/magento mx:migrate` command that handles migrations seamlessly. +* Add new `bin/magento mx:db:migrate` command that handles migrations seamlessly. ## 1.0.1 - 2018-07-05 diff --git a/README.md b/README.md index 3dca80a..0c791e2 100644 --- a/README.md +++ b/README.md @@ -119,7 +119,7 @@ We have access to 3 different setup helpers provided by Magento in each migratio This module provides a new command that wraps up the whole DB migration process - bin/magento mx:migrate + bin/magento mx:db:migrate The new command does the following: diff --git a/src/Console/Command/MigrateCommand.php b/src/Console/Command/MigrateCommand.php index 12b57c3..f648105 100644 --- a/src/Console/Command/MigrateCommand.php +++ b/src/Console/Command/MigrateCommand.php @@ -19,7 +19,7 @@ class MigrateCommand extends Command protected function configure() { $this - ->setName('mx:migrate') + ->setName('mx:db:migrate') ->setDescription('Migrates the database so it is ready for use with the current application code.'); } From fcdb0c010ddabf31c4d93286b836e2d34628d9d5 Mon Sep 17 00:00:00 2001 From: James Halsall Date: Sun, 9 Sep 2018 16:26:21 +0100 Subject: [PATCH 5/6] Use constants for exit codes --- src/Console/Command/MigrateCommand.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Console/Command/MigrateCommand.php b/src/Console/Command/MigrateCommand.php index f648105..2b449cf 100644 --- a/src/Console/Command/MigrateCommand.php +++ b/src/Console/Command/MigrateCommand.php @@ -2,6 +2,7 @@ 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; @@ -43,6 +44,6 @@ protected function execute(InputInterface $input, OutputInterface $output) echo $buffer; }); - return $phinx->isSuccessful() ? 0 : 1; + return $phinx->isSuccessful() ? Cli::RETURN_SUCCESS : Cli::RETURN_FAILURE; } } From bff15a8ad83100b193b60200b8c2d6deef8d9fea Mon Sep 17 00:00:00 2001 From: James Halsall Date: Sun, 9 Sep 2018 17:22:25 +0100 Subject: [PATCH 6/6] Improve migrate command --- src/Console/Command/MigrateCommand.php | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/Console/Command/MigrateCommand.php b/src/Console/Command/MigrateCommand.php index 2b449cf..fd7a28e 100644 --- a/src/Console/Command/MigrateCommand.php +++ b/src/Console/Command/MigrateCommand.php @@ -7,6 +7,7 @@ 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; @@ -17,11 +18,21 @@ */ 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) @@ -34,12 +45,21 @@ protected function execute(InputInterface $input, OutputInterface $output) if ($exit === DbStatusCommand::EXIT_CODE_UPGRADE_REQUIRED) { $output->writeln('A setup:upgrade is required, running...'); $setupUpgradeCommand = $this->getApplication()->get('setup:upgrade'); - $setupUpgradeCommand->run(new ArrayInput([]), $output); + + $setupUpgradeArgs = $input->getOption('keep-generated') ? ['--keep-generated' => true] : []; + $setupUpgradeCommand->run(new ArrayInput($setupUpgradeArgs), $output); } else { $output->writeln('No setup:upgrade required.'); } - $phinx = new Process('bin/phinx migrate'); + 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; });