Yii 2 Migration


(Bizley) #1

[size="6"]Yii 2 Migration v2[/size]

With this extension all your migrations can be generated automatically.

[size="5"]Typical use case scenario[/size]

This huge DB table on your local machine is finally polished and ready to be moved to production environment.

Now instead of writing the migration class down you can use this extension. Simply install it by adding


"bizley/migration": "*"

in the require section of your composer.json file and run composer update.

Alternatively for the same effect you can just run


composer require bizley/migration

Now add some basic configuration (preferably in the console config file):




'components' => [

    // ...

],

'controllerMap' => [

    'migration' => [

        'class' => 'bizley\migration\controllers\MigrationController',

    ],

],



And you are ready to go (fortunately it needs to be done only once).

In the console run


php yii migration/create table_name --fixHistory=1

and you can find the migration file for this table_name table in your @app/migrations folder. In addition your local migration history table has been updated with this migration entry (fixHistory parameter).

Now you can copy this migration file to your production environment and run there php yii migrate/up to apply it.

Few days later you add one additional column to the same table. And again Yii 2 Migration v2 can help.

In the console run


php yii migration/update table_name --fixHistory=1

and you can find another migration file for the table_name table in your @app/migrations folder but now it only contains methods that modifies table instead of creating it. (Local migration history table has been again updated with this migration entry).

To make both environments even again copy new migration file and run there php yii migrate/up to apply it.

[size="5"]Migration extension can generate creating and updating migrations[/size]

  • Supports databases that are supported by Yii 2 core: CUBRID, MS SQL Server, MySQL, Oracle, PostgreSQL and SQLite.

  • Scans migration history to gather all previously applied changes and create virtual table schema that can be compared with the real one.

  • Generates general schema migrations or currently-used-DB-specific ones.

  • Can generate migrations for many tables at once.

  • Can update migration history table when generating the file.

  • Can show differences between current table structure and the one saved in migration files instead of generating migration.

What is not supported (unfortunately)

  • DB indexes (except unique indexes),

  • whole table comments (column comments are supported),

  • ON UPDATE and ON DELETE actions for foreign keys.

[size="6"]For all available parameters go to the GitHub repo:

https://github.com/bizley/yii2-migration[/size]


(Bizley) #2

Migration v2.1

  • New actions list and create-all added (thanks to thyseus),

  • New action update-all added,

  • Namespaced migrations are properly extracted now,

  • Missing migration history file error is now properly handled.

Migration v2.1.1

New option added: skipMigrations

List of migrations from the history table that should be skipped during the update process. (default [])

Here you can place migrations containing actions that can not be covered by extractor i.e. when there is migration setting the RBAC hierarchy with authManager component. Such actions should be kept in separated migration and placed on this list to prevent them from being run during the extraction process.


(Jacob Moen) #3

Great work!

Just want to mention, though, that if you’ve developed a huge database without thinking about migrations, you are definitely doing it wrong!

But it’s great that this extension exist so that you can correct your erroneous ways in an elegant fashion!


(Bizley) #4

Cheers! :)

I guess it happens quite often, especially when you start with database model to generate tables ;)


(Bizley) #5

Migration 2.1.2

  • fixed bug with argument order in one of the methods,

  • proper handling of composite primary keys,

Note about renaming

When you rename table or column remember to generate appropriate migration manually otherwise this extension will not generate updating migration (in case of the table) or will generate migration with command to drop original column and add renamed one (in case of the column). This is happening because yii2-migration can only compare two states of the table without the knowledge of how one state turned into another. And while the very result of migration renaming the column and the one dropping it and adding another is the same in terms of structure, the latter makes you lose data.

Once you add renaming migration to the history it’s being tracked by the extension.


(Bizley) #6

[size="4"]Migration 2.3.0 has been released[/size]

Package has been rewritten for more object-oriented approach.

  • Minimum Yii version is raised up to 2.0.11.

  • Maximum Yii version is set to lower than 2.1.0.

  • Extractor class is gone.

  • Multiple table structure classes have been added.

  • PHPUnit tests (with MySQL specific tests) have been added.

  • Default value of generalSchema property is now 1.

New features:

  • TINYINT and JSON support added (works only for Yii 2.0.14 and newer).

  • Non-unique indexes tracking added (works only for Yii 2.0.13 and newer).

  • ON DELETE and ON UPDATE options tracking for foreign keys added (works only for Yii 2.0.13 and newer).

  • Migration history table is automatically skipped when running create-all and update-all actions.


(Bizley) #7

Migration 3.1.0 and 2.4.0 has been released.

  • Tests structure has been updated for easier DBMS switching
  • Added tests for PostgreSQL and SQLite
  • Added warning for unsupported SQLite migration commands
  • Fixed bug with schema type map missing when loading PostgreSQL schema
  • Added fallback for SQLite primary key detection
  • Schema type is now passed to TableColumn and TableChange
  • Changed the way column length is compared
  • Column length is now present only for DBMS supporting it
  • Updated Travis config for MySQL explicit_defaults_for_timestamp setting

I haven’t updated this topic for some time so here is quick summary what happened with the package since 2.3.0:

  • Extension has been split to 3.x and 2.x branches
  • 3.x requires PHP >=7.1.0 and Yii >=2.0.15.1
  • 2.x requires PHP <7.1.0
  • Several bugs have been fixed
  • Travis service has been added for automatic tests
  • Controller returns error codes now

(Bizley) #8

Migration version 3.6.0 and 2.9.0 has been released.

What happened since last entry in this topic?

  • Option excludeTables has been added to exclude selected tables from scanning while running create-all and update-all actions. Migration history table is added to excluded tables by default.
  • upsert() method has been added in dummy Migration file to properly silence it while scanning migrations.
  • Some code style has been updated.
  • Deprecated method removeMigrationTable() has been removed.
  • Extracting column from migration with column data provided in form different than ColumnSchemaBuilder instance throws exception from now on.
  • Default column value accepts empty strings now.
  • Default column value accepts arrays now.
  • Unique indexes are not triggering unnecessary updates from now on.
  • Creating migrations for more than one table now forces the order in which they are created based on the foreign key dependencies. When tables are cross-referenced additional special foreign key migration is added at the end of the whole process.
  • New config option templateFileForeignKey - template file for generating new foreign keys migrations (default @bizley/migration/views/create_fk_migration.php ).
    migrationPath and migrationNamespace properties now can also be arrays.
  • Column size different than DBMS’ default is now respected and rendered in case of generalSchema set to 1 .
  • after() and first() calls are now rendered.