The MX_PhinxMigrations module integrates Phinx database migrations into Magento 2

Overview

MX Phinx Migrations

About

The MX_PhinxMigrations module integrates Phinx database migrations into Magento 2 as a replacement for the built-in setup:upgrade method of triggering schema and data changes, therefore enabling zero-downtime deployments.

Why?

The idea of this module is to avoid having to change module versions (in module.xml files) in order to trigger database changes in Magento. When Magento bootstraps itself it verifies that all module versions in the setup_module database table match the versions on disk. If a module version in the database does not match the version in our code then Magento will error and show something like:

1 exception(s):
Exception #0 (Magento\Framework\Exception\LocalizedException): Please upgrade your database: Run "bin/magento setup:upgrade" from the Magento root directory.
The following modules are outdated:
Acme_Foo schema: current version - 0.2.0, required version - 0.3.0
Acme_Foo data: current version - 0.2.0, required version - 0.3.0

If we consider the following high level deployment process, this makes zero-downtime deployments impossible due to the fact that at some point, even if just for a very short time, the code and database versions will be out of sync between steps 2 and 3:

  1. Build Magento container
  2. Before deploying the new container(s), run setup:upgrade process on the target environment database
  3. Deploy the new container(s)

Requirements

The module is currently supported on Magento >= 2.1.

Installing

Add the module to the require section of the composer file:

$ composer require mx/module-phinx-migrations

Enabling the module

$ bin/magento setup:upgrade
$ bin/magento c:c

We can verify that the module is enabled by running the status command:

$ bin/magento module:status MX_PhinxMigrations

The phinx configuration file

After installing the module you will need to create a phinx.php file in your project root folder.

An example can be found here. There should be no further changes required to this file but it can be edited as necessary for your project setup.

Using

Migrations are custom classes that extend phinx migrations and execute some code to change the DB. Migration class files can sit in any of your Magento modules (phinx will scan for all migration folders at run time) in the following locations:

  • src/*/*/etc/migrations
  • app/code/*/*/etc/migrations

Creating a migration

phinx can create a migration for us. For example

bin/phinx create AddProductPimIdAttribute

It will then ask us which module we want to add the migration file to:

Which migrations path would you like to use?
  [0] src/Foo/Catalog/etc/migrations
  [1] src/Foo/Sales/etc/migrations
 >

When the migration file is created in the selected destination we should see a class similar to the following:

When writing our migration we should always include rollback code so that we can reverse deployments easily.

Magento setup helpers

We have access to 3 different setup helpers provided by Magento in each migration:

  1. $this->schemaSetup (instance of Magento\Framework\Setup\SchemaSetupInterface)
  2. $this->dataSetup (instance of Magento\Framework\Setup\ModuleDataSetupInterface)
  3. $this->eavSetup (instance of Magento\Eav\Setup\EavSetup)

Execute migrations

When you are happy with your migration you can execute it as follows

bin/phinx migrate

Rolling back

If you want to undo your migration then simply execute

bin/phinx rollback

Limitations

  1. Upgrading Magento itself will still result in version changes in the Magento module.xml files, therefore zero-downtime deployments are not possible in this situation.

FAQ / troubleshooting

Q. When I run bin/phinx create ... it cannot find any migration paths to create the migration file, why?

A. In order for a migration to be created phinx will scan your project for migration folders in the paths mentioned in the section above. Make sure you create at least one migration folder inside one of your Magento module's etc folders and then it should work as expected.

You might also like...
A Symfony2 bundle that integrates Select2 as a drop-in replacement for a standard entity field on a Symfony form.
A Symfony2 bundle that integrates Select2 as a drop-in replacement for a standard entity field on a Symfony form.

select2entity-bundle Introduction This is a Symfony bundle which enables the popular Select2 component to be used as a drop-in replacement for a stand

Integrates the ClassicPress Plugin Directory and any plugin stored in GitHub (tagged with classicpress-plugin) in the ClassicPress Admin

ClassicPress Plugin Directory Adds a new screen in a ClassicPress Install where you can browse, install, activate, deactivate, update, delete and pagi

Searchable DataObjects is a module that permit to include DataObjects into frontend search

Searchable DataObjects Searchable DataObjects is a module that permit to include DataObjects into frontend search. Introduction Pages are not always t

A SilverStripe module for conveniently injecting JSON-LD metadata into the header of each rendered page in SilverStripe

A SilverStripe module for conveniently injecting JSON-LD metadata into the header of each rendered page in Silver

Tables migrations seeded with data according to the Algerian education system structure.
Tables migrations seeded with data according to the Algerian education system structure.

Laravel algerian education system structure If you are building a Learning Management System or a School Management System and targeting the Algerian

Refresh artisan commands,table,migrations,models,controllers...

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

The package provides the ability to write logs into the database synchronously or asynchronously, along with other logging channels.

Laravel database log channel The package provides the ability to write logs into the database synchronously or asynchronously, along with other loggin

This project processes a small database with php all on a web server. This project uses XAMPP to run the web server and the database.

PHP-introduction This project processes a small database with php all on a web server. This project uses XAMPP to run the web server and the database.

A PHP MySQL database client class to simplify database access

This lightweight database class is written with PHP and uses the MySQLi extension, it uses prepared statements to properly secure your queries, no need to worry about SQL injection attacks.

Comments
  • Composer install from GitHub

    Composer install from GitHub

    The pull request provided with the ability

    composer installing directly from Github (independent on packagist.org)

    Allow installing directly from the github:

    $ composer config repositories.inviqa__MX_PhinxMigrations git https://github.com/inviqa/MX_PhinxMigrations
    

    Add the module to the require section of the composer file:

    $ composer require inviqa/MX_PhinxMigrations
    
    opened by coresh 0
Releases(1.1.1)
Owner
Inviqa
Inviqa
Magento 2 Module Experius Page Not Found 404. This module saves all 404 url to a database table

Magento 2 Module Experius Page Not Found 404 This module saves all 404 urls to a database table. Adds an admin grid with 404s It includes a count so y

Experius 28 Dec 9, 2022
A tool that allows to quickly export data from Magento 1 and Magento 2 store and import it back into Magento 2

Simple Import / Export tool A tool that allows to quickly export data from Magento 1 and Magento 2 store and import it back into Magento 2. Table data

EcomDev B.V. 51 Dec 5, 2022
This module integrates Silverstripe CMS with Google Translate API and then allows content editors to use automatic translation for every translatable field.

Autotranslate This module integrates Silverstripe CMS with Google Translate API and then allows content editors to use automatic translation for every

null 4 Jan 3, 2022
Migrations module for ProcessWire

ProcessDbMigrate Introduction This module is designed to ease the problem of migrating database changes from one PW environment to another.

Mark Evens 9 Nov 3, 2022
A Simple MVC PHP Framework, integrated with lot of features such as Session, Cookies, Migrations, Database, Factories, Seeding, Bootstrap and Tailwind CSS

Navite A Simple MVC PHP Framework, integrated with lot of features such as Session, Cookies, Migrations, Database, Factories, Seeding, Bootstrap and T

Celionatti 2 Aug 22, 2022
Search faster into Magento DevDocs and Magento StackExchange! 🔍⚡️

VS Code - Magento DevSearch Search faster into Magento DevDocs and Magento StackExchange! ?? ⚡️ Magento DevSearch is a VS Code extension that allows y

Rafael Corrêa Gomes 12 Oct 18, 2022
Automatically load the next page of products in Magento. Easy to install and configure, this module works 100% out of the box with vanilla Magento 1.9.x and earlier.

Automatically load the next page of products in Magento. Easy to install and configure, this module works 100% out of the box with vanilla Magento 1.9.x and earlier.

Strategery 123 Nov 20, 2021