recreating database from scratch with migrations?

I would like to recreate the database structure with yiic commandline. Is that possible?

I’ve searched the docs but didn’t come up with any valid idea (besides doing manual mysql commands).

My intention is, that I, let’s say, want to run this every 5 minutes:

  • drop test database

  • create test database

  • run migrations

The last two steps are not a problem, but I don’t see a Yii method( i.e. using the connection string from my main.cfg) how to purge the database before migrating again…

Any ideas?

bump, no ideas? :slight_smile:

Check this thread -

Thanks, I know about this one, but then how can I run all migrations up afterwards using CConsoleApplication ?

Done it!

I will write a Bamboo integration howto soon, but this for reference how I did that:


class DbPrepare extends CConsoleCommand


  public function getHelp()


		return <<<EOD




  This command drops and recreates the database and runs all migrations.




   function actionIndex($tables = '*', $alias = 'application') {

       	$db = Yii::app()->db;

       	preg_match('/dbname=(\w+)/',$db->connectionString, $matches);

       	$dbname = $matches[1];

       	echo "Dropping old database.\n";

       	$db->createCommand(sprintf('DROP DATABASE `%s`',$dbname))->execute();

       	echo "Creating new database.\n";

       	$db->createCommand(sprintf('CREATE DATABASE `%s`',$dbname))->execute();

       	$db->createCommand(sprintf('USE `%s`',$dbname))->execute();

       	echo "Running migrations.\n";



        echo "Done.\n\n";



sure, you have to drop the database?

$tables = Yii::app()->db->schema->getTableNames();

foreach ($tables as $table) {



ok, if you are using more database objects like views, stored procedures etc then it is better to drop the database

1 Like