Table of Contents
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.
1 |
php bin/cake.php bake migration xxxx |
Command is defined in MigrationTask.php
which inherits SimpleMigrationTask.php
. Let’s look at options and predefined format.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
/** * Gets the option parser instance and configures it. * * @return \Cake\Console\ConsoleOptionParser */ public function getOptionParser() { $name = ($this->plugin ? $this->plugin . '.' : '') . $this->name; $parser = new ConsoleOptionParser($name); $bakeThemes = []; foreach (Plugin::loaded() as $plugin) { $path = Plugin::classPath($plugin); if (is_dir($path . 'Template' . DS . 'Bake')) { $bakeThemes[] = $plugin; } } $parser->description( 'Bake migration class.' )->addOption('plugin', [ 'short' => 'p', 'help' => 'Plugin to bake into.' ])->addOption('force', [ 'short' => 'f', 'boolean' => true, 'help' => 'Force overwriting existing files without prompting.' ])->addOption('connection', [ 'short' => 'c', 'default' => 'default', 'help' => 'The datasource connection to get data from.' ])->addOption('theme', [ 'short' => 't', 'help' => 'The theme to use when baking code.', 'choices' => $bakeThemes ]); return $parser; } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
/** * Detects the action and table from the name of a migration * * @param string $name Name of migration * @return array **/ public function detectAction($name) { if (preg_match('/^(Create|Drop)(.*)/', $name, $matches)) { $action = strtolower($matches[1]) . '_table'; $table = Inflector::tableize(Inflector::pluralize($matches[2])); } elseif (preg_match('/^(Add).*(?:To)(.*)/', $name, $matches)) { $action = 'add_field'; $table = Inflector::tableize(Inflector::pluralize($matches[2])); } elseif (preg_match('/^(Remove).*(?:From)(.*)/', $name, $matches)) { $action = 'drop_field'; $table = Inflector::tableize(Inflector::pluralize($matches[2])); } elseif (preg_match('/^(Alter)(.*)/', $name, $matches)) { $action = 'alter_table'; $table = Inflector::tableize(Inflector::pluralize($matches[2])); } else { return null; } return [$action, $table]; } |
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.
1 |
php bin/cake.php migrations COMMAND |
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.
1 |
php bin/cake.php migrations mark_migrated VERSION |
Migration of CakePHP 3 is wrapper of phinx, so we should look into phinx to understand detail. It is really difficult.