yii2
Migrazioni del database
Ricerca…
Creazione di migrazioni
yii migrate/create <name>
L'argomento del nome richiesto fornisce una breve descrizione della nuova migrazione. Ad esempio, se la migrazione riguarda la creazione di una nuova tabella denominata news, è possibile utilizzare il nome create_news_table ed eseguire il seguente comando
yii migrate/create create_news_table
Esempio di file di migrazione
<?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()
{
}
*/
}
Drop Table
public function up()
{
$this->dropTable('post');
}
Crea campi tabella immediatamente
yii migrate/create create_post_table --fields="title:string,body:text"
genera:
/**
* 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');
}
}
Crea tabella
public function up()
{
$this->createTable('post', [
'id' => $this->primaryKey()
]);
}
Rilascia / Rinomina / Modifica colonna
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'));
}
Aggiungi colonna
public function up()
{
$this->addColumn('post', 'position', $this->integer());
}
Ripristino delle migrazioni
yii migrate/down # revert the most recently applied migration
yii migrate/down 3 # revert the most 3 recently applied migrations
Migrazioni transazionali
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 modo ancora più semplice di implementare le migrazioni transazionali è inserire il codice di migrazione nei safeUp()
e safeDown()
. Questi due metodi differiscono da up()
e down()
in quanto sono racchiusi implicitamente in una transazione. Di conseguenza, se qualsiasi operazione in questi metodi fallisce, tutte le operazioni precedenti verranno automaticamente ripristinate.
Migrazione di più database
Per impostazione predefinita, le migrazioni vengono applicate allo stesso database specificato dal componente dell'applicazione db. Se si desidera che vengano applicati a un altro database, è possibile specificare l'opzione della riga di comando db come mostrato di seguito:
yii migrate --db=db2
Ripristino delle migrazioni
yii migrate/redo # redo the last applied migration
yii migrate/redo 3 # redo the last 3 applied migrations
Elenco delle migrazioni
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
Modifica della cronologia delle migrazioni
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
Applicazione delle migrazioni
yii migrate
Questo comando elencherà tutte le migrazioni che non sono state applicate finora. Se confermi di voler applicare queste migrazioni, eseguirà il metodo up () o safeUp () in ogni nuova classe di migrazione, una dopo l'altra, nell'ordine dei loro valori di timestamp. Se una qualsiasi delle migrazioni fallisce, il comando si chiude senza applicare il resto delle migrazioni.
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