Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions CHANGELOG.md
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.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:db: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

Expand Down
13 changes: 13 additions & 0 deletions etc/di.xml
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>
69 changes: 69 additions & 0 deletions src/Console/Command/MigrateCommand.php
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());
Copy link
Contributor

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?

Copy link
Contributor Author

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

Copy link
Contributor

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:status says everything is up to date, while the new module is obviously not registered in the database yet. 🤔

Copy link
Contributor Author

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

Copy link
Contributor Author

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:status command 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.


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;
}
}