サーチ…


移行の作成

yii migrate/create <name>

必要なname引数には、新しい移行について簡単に説明します。たとえば、移行でnewsという名前の新しいテーブルを作成する場合、create_news_tableという名前を使用して次のコマンドを実行できます

yii migrate/create create_news_table

移行ファイルの例

<?php

use yii\db\Migration;

class m150101_185401_create_news_table extends Migration
{
public function up()
{

}

public function down()
{
    echo "m101129_185401_create_news_table cannot be reverted.\n";

    return false;
}

/*
// Use safeUp/safeDown to run migration code within a transaction
public function safeUp()
{
}

public function safeDown()
{
}
*/
}

ドロップテーブル

public function up()
{
    $this->dropTable('post');
}

すぐにテーブルフィールドを作成する

yii migrate/create create_post_table --fields="title:string,body:text"

生成する:

/**
 * Handles the creation for table `post`.
 */
class m150811_220037_create_post_table extends Migration
{
/**
 * @inheritdoc
 */
public function up()
{
    $this->createTable('post', [
        'id' => $this->primaryKey(),
        'title' => $this->string(),
        'body' => $this->text(),
    ]);
}

/**
 * @inheritdoc
 */
public function down()
{
    $this->dropTable('post');
}
}

テーブルの作成

public function up()
{
    $this->createTable('post', [
        'id' => $this->primaryKey()
    ]);
}

ドロップ/名前の変更/列の変更

public function up()
{
    $this->dropColumn('post', 'position');

    $this->renameColumn('post', 'owner_id', 'user_id');

    $this->alterColumn('post', 'updated', $this->timestamp()->notNull()->defaultValue('0000-00-00 00:00:00'));
}

列を追加

public function up()
{
    $this->addColumn('post', 'position', $this->integer());
}

移行の復帰

yii migrate/down     # revert the most recently applied migration
yii migrate/down 3   # revert the most 3 recently applied migrations

トランザクションマイグレーション

public function safeUp()
{
    $this->createTable('news', [
        'id' => $this->primaryKey(),
        'title' => $this->string()->notNull(),
        'content' => $this->text(),
    ]);

    $this->insert('news', [
        'title' => 'test 1',
        'content' => 'content 1',
    ]);
}

public function safeDown()
{
    $this->delete('news', ['id' => 1]);
    $this->dropTable('news');
}

トランザクションの移行を実装するさらに簡単な方法は、移行コードをsafeUp()およびsafeDown()メソッドにsafeDown()ことです。これら2つのメソッドは、トランザクション内で暗黙的に囲まれている点でup()およびdown()と異なります。結果として、これらのメソッドでの操作が失敗した場合、すべての以前の操作は自動的にロールバックされます。

複数のデータベースの移行

デフォルトでは、移行はdbアプリケーションコンポーネントによって指定された同じデータベースに適用されます。それらを別のデータベースに適用したい場合は、以下に示すようなdbコマンドラインオプションを指定することができます:

yii migrate --db=db2

移行のやり直し

yii migrate/redo        # redo the last applied migration
yii migrate/redo 3      # redo the last 3 applied migrations

移行の一覧表示

yii migrate/history     # showing the last 10 applied migrations
yii migrate/history 5   # showing the last 5 applied migrations
yii migrate/history all # showing all applied migrations

yii migrate/new         # showing the first 10 new migrations
yii migrate/new 5       # showing the first 5 new migrations
yii migrate/new all     # showing all new migrations

移行履歴の変更

yii migrate/mark 150101_185401                      # using timestamp to specify the migration
yii migrate/mark "2015-01-01 18:54:01"              # using a string that can be parsed by strtotime()
yii migrate/mark m150101_185401_create_news_table   # using full name
yii migrate/mark 1392853618                         # using UNIX timestamp

移行の適用

yii migrate

このコマンドはこれまでに適用されていないすべての移行を一覧表示します。これらの移行を適用することを確認した場合、新しい移行クラスごとにタイムスタンプ値の順にup()またはsafeUp()メソッドが順次実行されます。いずれかの移行が失敗した場合、コマンドは残りの移行を適用せずに終了します。

yii migrate 3
yii migrate/to 150101_185401                      # using timestamp to specify the migration
yii migrate/to "2015-01-01 18:54:01"              # using a string that can be parsed by strtotime()
yii migrate/to m150101_185401_create_news_table   # using full name
yii migrate/to 1392853618                         # using UNIX timestamp


Modified text is an extract of the original Stack Overflow Documentation
ライセンスを受けた CC BY-SA 3.0
所属していない Stack Overflow