CakePHP 3 migration command


I researched migration command in CakePHP 3.

Environment

  • PHP 5.5.9
  • cakephp/cakephp 3.0.9
  • cakephp/migrations 1.1.4

Task executed by bake

migration

Migration code is generated by the command like the following.

Command is defined in MigrationTask.php which inherits SimpleMigrationTask.php. Let’s look at options and predefined format.

The same options were also defined SimpleBakeTask, parent class. Well, --plugin and -p is the same meaning, which specify plugin name.

Now, let’s look at predefined migration format.

From the above, it is clear that CreateTable, DropTable, AddColumnToTable, RemoveColumnFromTable and AlterTable patterns are predefined. If command doesn’t match any predefined format, blank migration file will be created, so you should add necessary procedure.

migration_snapshot

php bin/cake.php bake migration_snapshot snapshot_name generate a snapshot of table schemes as migration code.

Adding option require-table, generate snapshot for only tables with Table class. As default, generate snapshot for tables in the database.

It is clear if you try. Or, reading Template/Bake/config/snapshot.php helps your understanding.

Command available in migrations

Let’s see command used in the following.

It is defined by files in Command directory.

migrate

Execute migration. Specify migration version with target option, and only the specified version will be migrated.

rollback

Rollback the last executed migration or specified migration with target option.

status

List migrations with status which reveals being already migrated or not.

mark_migrated

Make migration with given version as executed.

Migration of CakePHP 3 is wrapper of phinx, so we should look into phinx to understand detail. It is really difficult.