/** * Execute the action. * @param array command line parameters specific for this command */ public function actionIndex() { if (in_array(Yii::app()->params['env.code'], $this->testEnvironments)) { $this->_isTestEnv = true; } $dbName = Yii::app()->params['db.name']; //connect to the DB server $DSNparts = explode(';', Yii::app()->params['db.connectionString']); $serverOnlyDSN = $DSNparts[0]; //because standard dsn is : mysql:host=127.0.0.1;dbname=... $db = new CDbConnection($serverOnlyDSN, Yii::app()->params['db.username'], Yii::app()->params['db.password']); $db->active = true; //drop the, existing database, if it exists indeed $dbs = $db->createCommand("show databases")->queryAll(); if (in_array(array('Database' => $dbName), $dbs)) { if (!$this->noQuestions) { echo "\nStop!\n\nThis will completely destroy the existing database " . $dbName . "\n\nAre you sure? (Yes/No) "; if (strcasecmp(trim(fgets(STDIN)), 'Yes') != 0) { echo "Cancelled by the user\n"; Yii::app()->end(); } } echo "Destroying existing database " . $dbName . "..."; $db->createCommand("drop database " . $dbName)->execute(); if ($this->_isTestEnv) { if (in_array(array('Database' => $dbName . '_test'), $dbs)) { echo "Destroying existing test database " . $dbName . '_test' . " ..."; $db->createCommand("drop database " . $dbName . '_test')->execute(); } } echo " Done\n"; } //create the database echo "Creating new database " . $dbName . "..."; $db->createCommand("create database " . $dbName)->execute(); echo " Done\n"; if ($this->_isTestEnv) { echo "Creating new database " . $dbName . '_test' . " ..."; $db->createCommand("create database " . $dbName . '_test')->execute(); echo " Done\n"; } //reset the connection $db->active = false; $db = new CDbConnection(Yii::app()->params['db.connectionString'], Yii::app()->params['db.username'], Yii::app()->params['db.password']); $db->active = true; if ($this->_isTestEnv) { $testdb = new CDbConnection(Yii::app()->params['testdb.connectionString'], Yii::app()->params['testdb.username'], Yii::app()->params['testdb.password']); $testdb->active = true; } //import the base sql file, uppon which migrations are built if ($this->useBaseSql) { $baseSqlFile = Yii::getPathOfAlias('common.migrations.base') . '.sql'; echo "Importing base.sql..."; $statements = explode(";", file_get_contents($baseSqlFile)); //dani: are you absolutely sure, no *data* in this file contains ';' ? Even if this is the case, generally it doesn't seem a safe assumption when importing an sql file foreach ($statements as $statement) { if (empty($statement)) { continue; } $db->pdoInstance->exec($statement); if ($this->_isTestEnv) { $testdb->pdoInstance->exec($statement); } } echo " Done\n"; echo "\n"; } //running migrations echo "Running migrations...\n"; runCommand(getPhpPath() . " " . Yii::getPathOfAlias('root.yiic') . " migrate --interactive=0 "); if ($this->_isTestEnv) { echo "Running migrations for test db...\n"; runCommand(getPhpPath() . " " . Yii::getPathOfAlias('root.yiic') . " migrate --interactive=0 --connectionID=testdb"); echo "Done\n"; } }
foreach ($files as $srcFile) { $pattern = "/-" . $envType . ".php\$/"; $dstFile = preg_replace($pattern, "-env.php", $srcFile); // remove "env/" or "environments\" from the path $dstFile = preg_replace('/environments\\\\|environments\\//', "", $dstFile); $res = copy($srcFile, $dstFile); if (!$res) { echo "Error. Failed to copy {$srcFile} to {$dstFile}\n"; } // for some reason after copying the permissions are wrong chmod($dstFile, 0644); } } // create empty local configs if these do no exist yet if ($envType == 'private') { foreach ($directories as $dir) { if (!file_exists($dir . 'params-local.php')) { file_put_contents($dir . 'params-local.php', "<?php\nreturn array(\n\t'env.code' => 'private',\n);"); chmod($dir . 'params-local.php', 0644); } } } // applying migrations (for local machines is preferred to be done manually but...) if ($envType != 'private' && !in_array('no-migrate', $argv) || in_array('migrate', $argv)) { runCommand(getPhpPath() . ' \'' . $root . "yiic' migrate --interactive=0"); if (in_array($envType, array('prod'))) { // include any of environment types here at will runCommand(getPhpPath() . ' \'' . $root . "yiic' migrate --interactive=0 --connectionID=db"); } } echo "Done!\n";