/** * * @param string $module * @param string $action */ public static function update($module, $action = 'create') { $targetDbName = 'centreon'; ini_set('memory_limit', '-1'); $di = Di::getDefault(); $config = $di->get('config'); $targetDb = 'db_centreon'; $db = $di->get($targetDb); // Configuration for Propel $configParams = array('propel.project' => 'centreon', 'propel.database' => 'mysql', 'propel.database.url' => $config->get($targetDb, 'dsn'), 'propel.database.user' => $config->get($targetDb, 'username'), 'propel.database.password' => $config->get($targetDb, 'password')); // Set the Current Platform and DB Connection $platform = new CentreonMysqlPlatform($db); // Initilize Schema Parser $propelDb = new \MysqlSchemaParser($db); $propelDb->setGeneratorConfig(new \GeneratorConfig($configParams)); $propelDb->setPlatform($platform); // get Current Db State $currentDbAppData = new \AppData($platform); $currentDbAppData->setGeneratorConfig(new \GeneratorConfig($configParams)); $currentDb = $currentDbAppData->addDatabase(array('name' => $targetDbName)); $propelDb->parse($currentDb); // Retreive target DB State $updatedAppData = new \AppData($platform); self::getDbFromXml($updatedAppData, 'centreon'); // Get diff between current db state and target db state $diff = \PropelDatabaseComparator::computeDiff($currentDb, $updatedAppData->getDatabase('centreon'), false); if ($diff !== false) { $strDiff = $platform->getModifyDatabaseDDL($diff); $sqlToBeExecuted = \PropelSQLParser::parseString($strDiff); $finalSql = "\nSET FOREIGN_KEY_CHECKS = 0;\n\n"; if ($action == 'create') { $finalSql .= implode(";\n\n", static::keepCreateStatement($sqlToBeExecuted, $module)); } elseif ($action == 'delete') { $finalSql .= implode(";\n\n", static::keepDeleteStatement($sqlToBeExecuted, $module)); } $finalSql .= ";\n\nSET FOREIGN_KEY_CHECKS = 1;\n\n"; \PropelSQLParser::executeString($finalSql, $db); } // Empty Target DB self::deleteTargetDbSchema($targetDbName); }
public function createMigrationTable($datasource) { $platform = $this->getPlatform($datasource); // modelize the table $database = new Database($datasource); $database->setPlatform($platform); $table = new Table($this->getMigrationTable()); $database->addTable($table); $column = new Column('version'); $column->getDomain()->copy($platform->getDomainForType('INTEGER')); $column->setDefaultValue(0); $table->addColumn($column); // insert the table into the database $statements = $platform->getAddTableDDL($table); $pdo = $this->getPdoConnection($datasource); $res = PropelSQLParser::executeString($statements, $pdo); if (!$res) { throw new Exception(sprintf('Unable to create migration table in datasource "%s"', $datasource)); } }
protected function dropTables() { $db = Di::getDefault()->get('db_centreon'); $platform = new CentreonMysqlPlatform($db); // Get current DB State $currentDb = self::initializeCurrentSchema($platform); // Retreive target DB State $updatedAppData = new \AppData($platform); $appDataObject = new \XmlToAppData(new CentreonMysqlPlatform($db), null, 'utf-8'); $updatedAppData->joinAppDatas(array($appDataObject->parseFile(__DIR__ . '/data/empty.xml'))); unset($appDataObject); /* @todo Fatorize */ $diff = \PropelDatabaseComparator::computeDiff($currentDb, $updatedAppData->getDatabase('centreon'), false); if (false !== $diff) { $strDiff = $platform->getModifyDatabaseDDL($diff); $sqlToBeExecuted = \PropelSQLParser::parseString($strDiff); \PropelSQLParser::executeString($strDiff, $db); } }