Database Migration

Salve a tutti. Ho provato ad usare lo strumento migration ma non funziona.

Ho creato il nuovo file usando il comando:

yiic migrate create name_table

Ho modificato il file seguendo l’esempio della guida:




class m101129_185401_name_table extends CDbMigration

{

    public function up()

    {

        $this->createTable('tbl_news', array(

            'id' => 'pk',

            'title' => 'string NOT NULL',

            'content' => 'text',

        ));

    }

 

    public function down()

    {

        $this->dropTable('tbl_news');

    }

}

A questo punto ho usato il comando yiic migrate ma ottengo l’errore riportato di seguito:

CConsoleApplication.db is read only

La procedura ovviamente si interrompe.

OK RISOLTO!!.

Ho configurato adeguatamente il file console.php (protected/config/console.php) aggiungendo:


'components'=>array(

			'db'=>array(

					'connectionString' => 'mysql:host=host;dbname=dbase',

					'username' => 'user',

					'password' => 'password',

					'charset' => 'utf8',

				),

		),

La migrazione funziona ma vorrei sapere quali sono attualmente i limiti dello strumento migration (PRYMARY KEY,UNIQUE KEY, INDEX KEY) e come posso impostare direttamente le relazioni tra piu’ tabelle.

Ciao!!

Io personalmente uso migrate in una maniera molto sporca.

Ogni volta che faccio qualche modifica al database copio l’sql e lo incollo in un file per la migration all’interno di

Yii::app()->db->createCommand("sql qua")->query();

Cosi’ funziona perfettamente l’up, il down non lo uso.

(funziona proprio tutto, foreign key, insomma TUTTO)

Grazie Zaccaria.

Ad ogni modo ho dato un’occhiata al sorgente (CDbMigration.php) e ho visto che sono presenti tutte le funzioni che mi servono, riporto quindi di seguito un piccolo esempio giusto per dare un’idea.

Nell’esempio creo la migrazione di un database con solo due tabelle (parent e child) e creo una semplice relazione tra loro.

Uso la funzione function up() per creare le tabelle, aggiungere indici, e relazioni oppure per inserire i dati.


public function up()

    {

        /* CREO LA TABELLA PARENT */


	$this->createTable('parent', array(

			'id'=>'int(10) NOT NULL AUTO_INCREMENT',

			'id'=>'pk', // Imposto chiave primaria

	),'ENGINE=InnoDB');

	

        /* CREO LA TABELLA CHILD */


	$this->createTable('child', array(

			'id'=>'int(10) NOT NULL',

			'parent_id'=>'int(10)',

			'INDEX par_ind (parent_id)',// Imposto l'indice che uso per la relazione con la tabella parent

	),'ENGINE=InnoDB');


        /* INSERISCO DATI NELLA TABELLA PARENT */


        $this->insert('parent', array(

			'id'=>1,

	));	


        /* INSERISCO DATI NELLA TABELLA CHILD */


        $this->insert('child', array(

			'id'=>1,

                        'parent_id'=>1,

	));	

	/**

	 * Builds a SQL statement for adding a foreign key constraint to an existing table.

	 * The method will properly quote the table and column names.

	 * @param string $name the name of the foreign key constraint.

	 * @param string $table the table that the foreign key constraint will be added to.

	 * @param string $columns the name of the column to that the constraint will be added on. If there are multiple columns, separate them with commas.

	 * @param string $refTable the table that the foreign key references to.

	 * @param string $refColumns the name of the column that the foreign key references to. If there are multiple columns, separate them with commas.

	 * @param string $delete the ON DELETE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL

	 * @param string $update the ON UPDATE option. Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL

	 */


	$this->addForeignKey('par_ind','child','parent_id','parent','id','CASCADE','CASCADE');

}

In questo caso con la funzione down (che posso usare con il comando Yiic migrate down) mi limito ad eliminare le tabelle:




    public function down()

    {

	 $this->dropTable('parent');

	 $this->dropTable('child');

    }



Io uso una tecnica un pochino più bovina.

  • apro phpmyadmin

  • esporto tutto il database dati compresi

  • invio per email al mio compare sistemista il file .sql

  • lui mi avverte ed io verifico che tutto sia ok