yii2
데이터베이스 마이그레이션
수색…
마이그레이션 생성
yii migrate/create <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()
입니다. 이 두 메소드는 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
이 명령은 지금까지 적용되지 않은 모든 이전을 나열합니다. 이러한 마이그레이션을 적용한다는 것을 확인하면 새로운 마이그레이션 클래스마다 timestamp 값의 순서대로 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