yii2
Migrations de base de données
Recherche…
Créer des migrations
yii migrate/create <name>
L'argument de nom requis donne une brève description de la nouvelle migration. Par exemple, si la migration concerne la création d'une nouvelle table nommée news, vous pouvez utiliser le nom create_news_table et exécuter la commande suivante
yii migrate/create create_news_table
Exemple de fichier de migration
<?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()
{
}
*/
}
Table de chute
public function up()
{
$this->dropTable('post');
}
Créer des champs de table tout de suite
yii migrate/create create_post_table --fields="title:string,body:text"
Génère:
/**
* 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');
}
}
Créer une table
public function up()
{
$this->createTable('post', [
'id' => $this->primaryKey()
]);
}
Drop / Rename / Alter Column
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'));
}
Ajouter une colonne
public function up()
{
$this->addColumn('post', 'position', $this->integer());
}
Rétablir les migrations
yii migrate/down # revert the most recently applied migration
yii migrate/down 3 # revert the most 3 recently applied migrations
Migrations Transactionnelles
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');
}
Un moyen encore plus simple d'implémenter les migrations transactionnelles consiste à placer le code de migration dans les safeUp()
et safeDown()
. Ces deux méthodes diffèrent de up()
et down()
en ce sens qu'elles sont implicitement incluses dans une transaction. En conséquence, si une opération dans ces méthodes échoue, toutes les opérations antérieures seront annulées automatiquement.
Migration de plusieurs bases de données
Par défaut, les migrations sont appliquées à la même base de données spécifiée par le composant db application. Si vous souhaitez qu'ils soient appliqués à une autre base de données, vous pouvez spécifier l'option de ligne de commande db comme indiqué ci-dessous:
yii migrate --db=db2
Refaire les migrations
yii migrate/redo # redo the last applied migration
yii migrate/redo 3 # redo the last 3 applied migrations
Liste des 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
Modification de l'historique de migration
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
Application des migrations
yii migrate
Cette commande répertorie toutes les migrations qui n'ont pas encore été appliquées. Si vous confirmez que vous souhaitez appliquer ces migrations, il exécutera la méthode up () ou safeUp () dans chaque nouvelle classe de migration, l'une après l'autre, dans l'ordre de leurs valeurs d'horodatage. Si l'une des migrations échoue, la commande se ferme sans appliquer le reste des migrations.
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