Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1416 from alexispeter/app-setup-fix
Browse files Browse the repository at this point in the history
only run setup-scripts if mysqldb and mongodb are empty
  • Loading branch information
tomaszdurka committed Sep 22, 2014
2 parents e8760a7 + f1144d4 commit 041d6a2
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions library/CM/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public function setupFilesystem() {
* @param boolean|null $forceReload
*/
public function setupDatabase($forceReload = null) {
$this->_setupDbSql($forceReload);
$this->_setupDbMongo($forceReload);
$isEmptyMysql = $this->_setupDbMysql($forceReload);
$isEmptyMongo = $this->_setupDbMongo($forceReload);
$app = CM_App::getInstance();
foreach ($this->_getUpdateScriptPaths() as $namespace => $path) {
$updateFiles = CM_Util::rglob('*.php', $path);
Expand All @@ -42,8 +42,10 @@ public function setupDatabase($forceReload = null) {
}, $app->getVersion());
$app->setVersion($version, $namespace);
}
foreach (CM_Util::getResourceFiles('db/setup.php') as $setupScript) {
require $setupScript->getPath();
if ($isEmptyMysql && $isEmptyMongo) {
foreach (CM_Util::getResourceFiles('db/setup.php') as $setupScript) {
require $setupScript->getPath();
}
}
}

Expand Down Expand Up @@ -205,6 +207,7 @@ private function _getUpdateScriptPath($version, $moduleName = null) {

/**
* @param boolean $forceReload
* @return boolean
* @throws CM_Exception_Invalid
*/
private function _setupDbMongo($forceReload) {
Expand All @@ -213,7 +216,8 @@ private function _setupDbMongo($forceReload) {
$mongoClient->dropDatabase();
}
$collections = $mongoClient->listCollectionNames();
if (0 === count($collections)) {
$isEmpty = 0 === count($collections);
if ($isEmpty) {
foreach (CM_Util::getResourceFiles('mongo/collections.json') as $dump) {
$collectionInfo = CM_Params::jsonDecode($dump->read());
foreach ($collectionInfo as $collection => $indexes) {
Expand All @@ -224,13 +228,15 @@ private function _setupDbMongo($forceReload) {
}
}
}
return $isEmpty;
}

/**
* @param boolean $forceReload
* @return boolean
* @throws CM_Db_Exception
*/
private function _setupDbSql($forceReload) {
private function _setupDbMysql($forceReload) {
$mysqlClient = CM_Service_Manager::getInstance()->getDatabases()->getMaster();
$db = $mysqlClient->getDb();
$mysqlClient->setDb(null);
Expand All @@ -243,10 +249,12 @@ private function _setupDbSql($forceReload) {
}
$mysqlClient->setDb($db);
$tables = $mysqlClient->createStatement('SHOW TABLES')->execute()->fetchAll();
if (0 === count($tables)) {
$isEmpty = 0 === count($tables);
if ($isEmpty) {
foreach (CM_Util::getResourceFiles('db/structure.sql') as $dump) {
CM_Db_Db::runDump($db, $dump);
}
}
return $isEmpty;
}
}

0 comments on commit 041d6a2

Please sign in to comment.