This is a pre-alpha version of a Database Migrations system for CakePHP 3.0. It is currently under development and should be considered experimental.
The plugin consists of a wrapper for the phinx migrations library.
You can install this plugin into your CakePHP application using
composer. For existing applications you can add the
following to your composer.json
file:
"require": {
"cakephp/migrations": "dev-master"
}
And run php composer.phar update
You will need to add the following line to your application's bootstrap.php file:
Plugin::load('Migrations');
Additionally, you will need to configure the default
database configuration in your config/app.php
file.
This plugins provides the Migrations shell that you can invoke from your application's root folder:
$ bin/cake migrations
The command above will display a list of available options. Make sure you read the official phinx documentation to understand how migrations are created and executed in your database.
To create your first migration file, execute:
$ bin/cake bake migration Initial [-p PluginName] [-c connection]
This will create a phinx file with tables found in your database. By default,
this will just add tables that have model files, but you can create a file with
all tables by adding the option --checkModel false
.
To create an empty migration file, execute:
$ bin/cake migrations create Name
This will create a file under config/Migrations
that you can edit to complete
the migration steps as documented in phinx's manual.
Please note that you will need to learn how to write your own migrations, you need to fill in the up() and down() or change() methods if you want automatically reversible migrations.
Once again, please make sure you read the official phinx documentation to understand how migrations are created and executed in your database.
After modifying the migration file, you can run your changes in the database by executing:
$ bin/cake migrations migrate
If you added any steps to revert a migration in the down()
callback, you can execute this command and have that function executed:
$ bin/cake migrations rollback
By executing this command you will have an overview of the migrations that have been executed and those still pending to be run:
$ bin/cake migrations status
All the commands from above support the --plugin
or -p
option:
$ bin/cake migrations status -p PluginName
All the commands from above support the --connection
or -c
option:
$ bin/cake migrations migrate -c my_datasource
To create a table called statuses
and use a CHAR (36) for the id
field, this requires you to turn off the id.
See:
$table = $this->table('statuses',
[
'id' => false,
'primary_key' => ['id']
]);
$table->addColumn('id', 'char', ['limit' => 36])
->addColumn('name', 'char', ['limit' => 255])
->addColumn('model', 'string', ['limit' => 128])
->create();