yii2
डेटाबेस माइग्रेशन
खोज…
माइग्रेशन बनाना
yii migrate/create <name>
आवश्यक नाम तर्क नए प्रवास के बारे में एक संक्षिप्त विवरण देता है। उदाहरण के लिए, यदि माइग्रेशन समाचार नाम की एक नई तालिका बनाने के बारे में है, तो आप 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
यह आदेश उन सभी पलायनों को सूचीबद्ध करेगा जो अब तक लागू नहीं किए गए हैं। यदि आप यह पुष्टि करते हैं कि आप इन माइग्रेशनों को लागू करना चाहते हैं, तो यह उनके टाइमस्टैम्प मूल्यों के क्रम में, एक के बाद एक हर नए माइग्रेशन क्लास में अप () या सेफअप () विधि चलाएगा। यदि कोई भी माइग्रेशन विफल हो जाता है, तो कमांड बाकी के माइग्रेशनों को लागू किए बिना छोड़ देगा।
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