warden
(Radek Antoniuk)
July 25, 2012, 9:19am
1
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?
mdomba
(Maurizio Domba Cerin)
July 27, 2012, 12:34pm
3
warden
(Radek Antoniuk)
July 27, 2012, 1:10pm
4
Thanks, I know about this one, but then how can I run all migrations up afterwards using CConsoleApplication ?
warden
(Radek Antoniuk)
July 27, 2012, 2:01pm
5
Done it!
I will write a Bamboo integration howto soon, but this for reference how I did that:
<?php
class DbPrepare extends CConsoleCommand
{
public function getHelp()
{
return <<<EOD
USAGE
dbprepare
DESCRIPTION
This command drops and recreates the database and runs all migrations.
EOD;
}
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";
Yii::app()->getCommandRunner()->createCommand('migrate')->run(array('--interactive=0'));
echo "Done.\n\n";
}
}
mbi
(mbi)
July 27, 2012, 3:10pm
6
sure, you have to drop the database?
$tables = Yii::app()->db->schema->getTableNames();
foreach ($tables as $table) {
Yii::app()->db->createCommand()->dropTable($table);
}
ok, if you are using more database objects like views, stored procedures etc then it is better to drop the database
1 Like