Monday, January 13, 2014

Yii migration safeUp() is not working

I tried to create a table using the safeUp() function. Here's the content of the migration file
class m140113_223038_create_test_tables extends CDbMigration
{
 public function up()
 {
 }

 public function down()
 {
 }

 public function safeUp()
 {
  $this->createTable('tbl_test',array(
   'id'=>'pk',
   'name'=>'string NOT NULL',
   ),'ENGINE=InnoDB');
 }

 public function safeDown()
 {
  $this->dropTable('tbl_test');
 }
}
However, it won't create a new table. The solution is actually very simple. If we want to use safeUp() function, we have to make sure that there's no up() function in the code. So, we have to remove both up() and down() function.
class m140113_223038_create_test_tables extends CDbMigration
{
 public function safeUp()
 {
  $this->createTable('tbl_test',array(
   'id'=>'pk',
   'name'=>'string NOT NULL',
   ),'ENGINE=InnoDB');
 }

 public function safeDown()
 {
  $this->dropTable('tbl_test');
 }
}
Reference:
http://stackoverflow.com/questions/18126773/yii-framework-yic-migrate-command-doesnt-work

No comments: