Migrations
Migrations are a convenient way for you to alter your database in a structured and organized manner. You could edit fragments of SQL by hand but you would then be responsible for telling other developers that they need to go and run them. You’d also have to keep track of which changes need to be run against the production machines next time you deploy.
The database table `migration` tracks which migrations have already been run so all you have to do is update your application files and call Migrate::current() to work out which migrations should be run. The current version is found in core/config/migration.php so like any other config file you should copy this to app/config to make changes.
Creating a Migration
Create a file in the app/migrations folder. The prefix should be an incremental count starting at 001, do not skip numbers and do not have two numbers that match. The first would be something like app/migrations/001_example.php.
namespace Fuel\Migrations;
class Example {
function up()
{
\DBUtil::create_table('posts', array(
'id' => array('type' => 'int', 'constraint' => 5),
'title' => array('type' => 'varchar', 'constraint' => 100),
'body' => array('type' => 'text'),
), array('id'));
}
function down()
{
\DBUtil::drop_table('posts');
}
}
Run a Migration
A migration can be run in two ways:
The oil command there uses the Refine command to call the migrate task.
$ php oil refine migrate $ php oil refine migrate:current $ php oil refine migrate:up $ php oil refine migrate:down $ php oil refine migrate --version=10
Note: The migrate:current task is to match your schema to the version listed in fuel/[app|core]/config/migrate.php as if you have just got a copy of the application, the very latest migration not be the one considered stable. Using oil for migrations will modify this migration config number so the current command will not be relevant in many situations.