Applying migrations to test db

Hello all.

I’m trying to setup migrations for my test db.

In console.php, I’ve put in a second db array like so:




'components'=>array(

  'db'=>array(

    'connectionString' => 'mysql:host=localhost;dbname=aaa_dev',

    'emulatePrepare' => true,

    'username' => 'aaa_dev',

    'password' => 'aaa_dev',

    'charset' => 'utf8',

  ),

  'testdb'=>array(

    'class'=>'CDbConnection',

    'connectionString' => 'mysql:host=localhost;dbname=aaa_test',

    'emulatePrepare' => true,

    'username' => 'aaa_test',

    'password' => 'aaa_test',

    'charset' => 'utf8',

  ),		

),



I had to put in the ‘class’=>‘CDbConnection’, part because it was complaining as follows:




exception 'CException' with message 'Object configuration must be an array containing a "class" element.' in /home/dani/lib/yii-1.1.8.r3324/framework/YiiBase.php:198



…but I didn’t understand why the ‘db’ array didn’t need one.

Moving on, I then tried to do a migration:




./yiic migrate --connectionID=testdb



The result was not quite as desired. The migration tables were created in the dev db and tbl_migration was created in the test db.

Does anyone know what I’m doing wrong?

You could avoid need to add connectionID, if you added db settings to your console app(testdb).

So, in console.php you should have this setting for db:


'db'=>array(

    'connectionString' => 'mysql:host=localhost;dbname=aaa_test',

    'emulatePrepare' => true,

    'username' => 'aaa_test',

    'password' => 'aaa_test',

    'charset' => 'utf8',

  ),

Please note that I used db parameter for key, and test database in connection string.

Then you should just use:


yiic migrate up

Thanks, Ivica, but unless I’m missing something, that would require me to flip it back and forth between doing migrations on dev and test. I’m hoping to avoid that.

I’ve found what I was doing wrong, anyway.

I was doing this in my migrations:




Yii::app()->db->createCommand($sql)->execute();



I know, pretty stupid. Go easy on me. :P

So I changed it to the following and solved the issue:




$this->getDbConnection()->createCommand($sql)->execute();



But that still had me confused because this is what the CDbMigration.getDbConnection() looks like:




public function getDbConnection()

{

	if($this->_db===null)

	{

		$this->_db=Yii::app()->getComponent('db');

		if(!$this->_db instanceof CDbConnection)

			throw new CException(Yii::t('yii', 'The "db" application component must be configured to be a CDbConnection object.'));

	}

	return $this->_db;

}



It’s doing the same thing I was! (That is, assuming the connection component it needs is ‘db’.)

After looking a bit deeper I realized that, yeah, it is, but Yii is smarter than that. The migration class never actually gets the connection itself that way. The $_db property is assigned to it by MigrateCommand.instantiateMigration(). And MigrateCommand has the proper value because it was initialized with it off the --connectionID command line arg.

Whew! A bonehead mistake, sure. But I always like when I walk out of these things understanding more. It was almost worth it. :)

Just try :


$this->execute($sql);

:)

Ahhh, even better. (Which one’s the embarrassed smilie?)

No need to be embarrassed :) Convenience methods, by their very nature, aren’t obvious.