From f1144d4ccc63f9fdd10139271739b2f69545b6c7 Mon Sep 17 00:00:00 2001 From: Alexis Peter Date: Fri, 19 Sep 2014 14:39:10 +0200 Subject: [PATCH] only run setup-scripts if mysqldb and mongodb are empty --- library/CM/App.php | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/library/CM/App.php b/library/CM/App.php index 1112e0557..ef12193f6 100644 --- a/library/CM/App.php +++ b/library/CM/App.php @@ -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); @@ -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(); + } } } @@ -205,6 +207,7 @@ private function _getUpdateScriptPath($version, $moduleName = null) { /** * @param boolean $forceReload + * @return boolean * @throws CM_Exception_Invalid */ private function _setupDbMongo($forceReload) { @@ -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) { @@ -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); @@ -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; } }