マイグレーション

マイグレーションは構造化され整理された方法で、データベースを変更する便利な方法です。SQL の断片を手動で編集することもできますが、他の開発者にもそれを実行するように知らせる責任が生じます。次のデプロイ時に、本番のマシンに対して実行する変更点を記録しておく必要もあります。

データベーステーブル migration が、どのマイグレーションが実行されているかを記録しますので、アプリケーションファイルを更新し、どのマイグレーションが実行されるかを解決するために Migrate::current() を呼び出すだけで済みます。現在のバージョンは、core/config/migration.php の中にありますので、他の設定ファイルと同様に app/config にコピーして変更します。

Configuration

Key Type Default Description
folder string
								'migrations/'
							
The folder in which migration files will be found.
connection string
								null
							
Configuration name of a database connection to use to write migrations.
table string
								'migration'
							
The database table used to store migration data.

Note: After migrations have been run, the configuration file will be rewritten with version information included. These version numbers are used internally and should not be changed.

Creating a Migration

app/migrations フォルダにファイルを作成します。接頭辞は 001 から始まる増加する数字です。数字はスキップせずに、また同じ数字を使わないようにする必要があります。最初のファイルは、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');
    }
}

マイグレーションの実行

マイグレーションは、2 つの方法で実行できます:

  1. Migrate クラス
  2. oil refine コマンド

oil コマンドは refine コマンドを使用して、マイグレーション タスク を呼び出します。

$ 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

Migrations are supported for modules and packages too. You can specify on the oil commandline if you want to migrate all, or only specific modules and/or packages. If you do, you can use '--default' to indicate you want to include app migrations.

$ php oil refine migrate -all
$ php oil refine migrate --modules=module1,module2 --packages=package1
$ php oil refine migrate:up --packages=package1
$ php oil refine migrate:down --modules=module1,module2 --default
$ 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.

Skipping migrations

You can abort the migration process by having your up() or down() method return false. This can be useful if your migration has external dependencies, for example the existence of a table created in a different migration.

This will only abort the migration stack currently being processed, either the application migrations, or the migrations in a single module or package. All migrations in other stacks will be processed as normal.