/** * Create the ShineISP Database */ public static function createDb($installsampledata = true) { try { $dbconfig = Shineisp_Main::databaseConfig(); $dsn = Shineisp_Main::getDSN(); $conn = Doctrine_Manager::connection($dsn, 'doctrine'); $conn->execute('SHOW TABLES'); # Lazy loading of the connection. If I execute a simple command the connection to the database starts. $conn->setAttribute(Doctrine::ATTR_USE_NATIVE_ENUM, true); $conn->setCharset('UTF8'); $dbh = $conn->getDbh(); $models = Doctrine::getLoadedModels(); // Set the current connection $manager = Doctrine_Manager::getInstance()->setCurrentConnection('doctrine'); if ($conn->isConnected()) { $migration = new Doctrine_Migration(APPLICATION_PATH . '/configs/migrations'); // Get the latest version set in the migrations directory $latestversion = $migration->getLatestVersion(); if (empty($latestversion)) { $latestversion = 0; } // Clean the database $conn->execute('SET FOREIGN_KEY_CHECKS = 0'); foreach ($models as $model) { $tablename = Doctrine::getTable($model)->getTableName(); $dbh->query("DROP TABLE IF EXISTS {$tablename}"); } // Create the migration_version table Doctrine_Manager::getInstance()->getCurrentConnection()->execute('DROP TABLE IF EXISTS `migration_version`;CREATE TABLE `migration_version` (`version` int(11) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1;INSERT INTO `migration_version` VALUES (' . $latestversion . ')'); // Create all the tables in the database Doctrine_Core::createTablesFromModels(APPLICATION_PATH . '/models'); // Common resources Doctrine_Core::loadData(APPLICATION_PATH . '/configs/data/fixtures/commons/', true); // Sample data if ($installsampledata) { $import = new Doctrine_Data_Import(APPLICATION_PATH . '/configs/data/fixtures/'); $import->setFormat('yml'); $import->setModels($models); $import->doImport(true); } $conn->execute('SET FOREIGN_KEY_CHECKS = 1'); // Update the version in the config.xml file previously created Settings::saveConfig($dbconfig, $latestversion); } else { echo "No Connection found"; } } catch (Exception $e) { die($e); } // return the latest version return $latestversion; }
/** * (non-PHPdoc) * @see Zend_Controller_Plugin_Abstract::preDispatch() */ public function preDispatch(Zend_Controller_Request_Abstract $request) { $dayssincefirstsetup = 0; $migration = new Doctrine_Migration(APPLICATION_PATH . '/configs/migrations'); $LatestVersion = $migration->getLatestVersion(); $CurrentVersion = $migration->getCurrentVersion(); try { // Check if the config file has been created $isReady = Shineisp_Main::isReady(); if ($isReady) { // Execute the migration if ($CurrentVersion < $LatestVersion) { // write a log message Shineisp_Commons_Utilities::log("Upgrade: Current Version is {$CurrentVersion} and the latest available version is {$LatestVersion}"); $dbconfig = Shineisp_Main::databaseConfig(); // Update the version in the config.xml file previously created Settings::saveConfig($dbconfig, $LatestVersion); if ($CurrentVersion > 0) { Shineisp_Commons_Utilities::log("Upgrade: Migrate ShineISP version from {$CurrentVersion} to {$LatestVersion}"); $migration->migrate(); } } $db = Doctrine_Manager::getInstance()->getCurrentConnection(); // Read and execute all the sql files saved in the /application/configs/data/sql directory $path = PROJECT_PATH . "/application/configs/data/sql"; if (is_dir($path)) { $directory_iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)); try { // get the first setup date $setupdate = Settings::getConfigSetupDate(); if (empty($setupdate)) { throw new Exception('Setup date is not set in the config.xml file'); } // for each sql file do ... foreach ($directory_iterator as $filename => $path_object) { // get the sql file information $info = pathinfo($filename); if (!empty($info['extension']) && $info['extension'] == "sql") { $name = $info['filename']; // get the first part of the name with the filename that contains the date: YYYYMMddHis-NAME.sql $arrName = explode("-", $name); // if the string is a valid date get the days betweeen the sql file name and the day of the setup of shineisp if (!empty($arrName[0]) && Zend_Date::isdate($arrName[0], 'YYYYMMddHis')) { $sqldate = new Zend_Date($arrName[0], 'YYYYMMddHis'); $mysetupdate = new Zend_Date($setupdate, 'YYYYMMddHis'); // get the difference of the two dates $diff = $sqldate->sub($mysetupdate)->toValue(); $dayssincefirstsetup = floor($diff / 60 / 60 / 24); unset($sqldate); unset($mysetupdate); } // SQL files post installation will be executed if ($dayssincefirstsetup >= 0) { // read the sql $sql = Shineisp_Commons_Utilities::readfile($info['dirname'] . "/" . $info['basename']); if (!empty($sql)) { // execute the sql strings $result = $db->execute($sql); // close the db connection $db->close(); if ($result) { // write a log message Shineisp_Commons_Utilities::log($info['filename'] . ".sql has been executed."); // rename the sql rename($info['dirname'] . "/" . $info['basename'], $info['dirname'] . "/" . $info['filename'] . ".sql.old"); } } } else { // rename the sql rename($info['dirname'] . "/" . $info['basename'], $info['dirname'] . "/" . $info['filename'] . ".sql.old"); // write a log message Shineisp_Commons_Utilities::log($info['filename'] . ".sql has been skipped because already set in the doctrine data setup."); } } } } catch (Exception $e) { die($e->getMessage()); } } } // Execute the migration if ($CurrentVersion < $LatestVersion) { $dbconfig = Shineisp_Main::databaseConfig(); // Update the version in the config.xml file previously created Settings::saveConfig($dbconfig, $LatestVersion); if ($CurrentVersion > 0) { $migration->migrate(); } } } catch (Exception $e) { Zend_Debug::dump($e->getMessage()); die; } }