/** * Task to run pending migrations * * @return null */ protected function _execute(array $params) { $migrations = new MigrationManager(); Database::$default = $params['db']; $this->db = Database::instance(); $db_config = Kohana::$config->load('database')->{$params['db']}; if (!ORM::factory('Migration')->is_installed()) { /** * Get platform from database config */ $platform = strtolower($db_config['type']); if ('mysqli' == $platform) { $platform = 'mysql'; } /** * Get SQL from file for selected platform */ $file = realpath(substr(__DIR__, 0, strlen(__DIR__) - strlen('classes/Task/Db')) . 'sql/' . $platform . '.sql'); $handle = fopen($file, 'rb'); $sql_create = fread($handle, filesize($file)); $this->db->query(0, $sql_create); $msg = Minion_CLI::color("-----------------------------\n", 'green'); $msg .= Minion_CLI::color("| Migration table create!!! |\n", 'green'); $msg .= Minion_CLI::color("-----------------------------\n", 'green'); Minion_CLI::write($msg); } $migrations->migrate($params['db'], $params['step']); }
/** * Task to rollback last executed migration * * @return null */ protected function _execute(array $params) { $migrations = new MigrationManager(); Database::$default = $params['db']; if (!ORM::factory('Migration')->is_installed()) { Minion_CLI::write('Migrations is not installed. Please Run the migrations.sql script in your mysql server'); exit; } $migrations->rollback($params['db'], $params['step']); }
/** * Task to build a new migration file * * @return null */ protected function _execute(array $params) { $migrations = new MigrationManager(); $status = $migrations->generate_migration($params['name']); if ($status == 0) { Minion_CLI::write('Migration ' . $params['name'] . ' was succefully created'); Minion_CLI::write('Please check migrations folder'); } else { Minion_CLI::write('There was an error while creating migration ' . $params['name']); } }
public static function migratePages() { $pages = Symphony::Database()->fetch("SELECT * FROM `tbl_pages`"); // Creates DOMDocument object $xml = new DOMDocument('1.0', 'UTF-8'); $xml->preserveWhiteSpace = false; $xml->formatOutput = true; // Root node $root = $xml->createElement('pages'); // Page entries $entries = $xml->createElement('entries'); if (is_array($pages) && !empty($pages)) { foreach ($pages as $page) { // Ensures that pages has a guid if (!$page['guid']) { $page['guid'] = uniqid(); Symphony::Database()->update(array('guid' => $page['guid']), 'tbl_pages', "id = {$page['id']}"); } $entry = $xml->createElement('entry'); foreach ($page as $column => $value) { if ($column == 'id') { continue; } $data = $xml->createElement($column, $value); $entry->appendChild($data); } $entries->appendChild($entry); } } // Page types $types = MigrationManager::getPagesTypes($page['guid']); $pages_types = $xml->createElement('types'); if (is_array($types) && !empty($types)) { foreach ($types as $page_type) { $type = $xml->createElement('type'); foreach ($page_type as $column => $value) { if ($column == 'type') { $value = implode(', ', $value); } $data = $xml->createElement($column, $value); $type->appendChild($data); } $pages_types->appendChild($type); } } $root->appendChild($entries); $root->appendChild($pages_types); $xml->appendChild($root); $location = WORKSPACE . "/pages/_pages.xml"; $output = $xml->saveXML(); General::writeFile($location, $output, Symphony::Configuration()->get('write_mode', 'file')); }
/** * Takes a query object of CreateTable or IQuery types and tries to execute it, * returning a QueryInfo object. * * @param $query An object that represents the query object. Either Query or CreateTable * @return QueryInfo A QueryInfo object that gives access to the query results, including any errors * @throws Exception */ public static function Query($query) { $ret = ""; $stmt = ""; try { $instance = self::instance(); // Determine which kind of object we're dealing with if ($query instanceof CreateTable) { $stmt = $instance->conn->prepare($query->Query()); } else { if ($query instanceof IQuery) { $stmt = $instance->parseIntoPrepared($query->Query()); } } // If neither of the above condtions is met, $stmt will be "" but needs to be // a PDOStatement so throw a new exception stating this if (!$stmt instanceof PDOStatement) { throw new Exception("Fatal Error: no statement found"); } $errors = ""; // Try to execute the given query 3 times for ($i = 0; !$stmt->execute() && $i < self::$tries; $i++) { // Gather the error information $err = $stmt->errorInfo()[2]; // Concatenate the error info together $errors .= "Failed to process the query: " . $err . "\n"; // If time out occurs for this file, uncomment the following line //printr($err); // If the error is data related, not database related, break out as // there is nothing that can be done to solve that problem if (is_numeric(strpos(strtolower($err), "duplicate"))) { break; } // Toss the query and the error info off to the MigrationManager to see if it can't // find a fix to the problem MigrationManager::instance()->HandleError($query, $stmt->errorInfo()); } // Set the return value to a new Query Info object $ret = new QueryInfo($stmt, $errors); } catch (PDOException $e) { echo "Error occured executing query '{$query->Query}()': " . $e->getMessage(); } return $ret; }
public function migratePages($context) { MigrationManager::migratePages(); }
/** * モデルクラスを自動生成して返す * 2.0よりオートマイグレーションが付きました! */ public static function getAutoGenerateModel($argDBO, $argModelName, $argExtractionCondition = NULL, $argBinds = NULL, $argSeqQuery = NULL) { // モデルクラス名とテーブル名を特定する $tableName = $argModelName; $modelName = self::getGeneratedModelName($tableName); // テーブル名末尾の数値は、ナンバリングテーブル名だと過程して、外す $matches = NULL; $unNumberingModelName = NULL; preg_match('/^([^0-9]+)[0-9]+$/', $modelName, $matches); if (is_array($matches) && isset($matches[1]) && strlen($matches[1]) > 0) { $unNumberingModelName = $matches[1]; } // モデルクラス名と、テーブル名の最終調整 if (strlen($modelName) - 5 === strpos(strtolower($modelName), "model")) { $tableName = substr($tableName, 0, strlen($tableName) - 5); } else { $modelName = $modelName . "Model"; } $tableName = ucfirst($tableName); // オートマイグレートその1 $lastMigrationHash = NULL; if (function_exists('getAutoMigrationEnabled') && TRUE === getAutoMigrationEnabled()) { // 未適用のmigrationがあれば、実行する $lastMigrationHash = MigrationManager::dispatchAll($argDBO, $tableName); if (TRUE === $lastMigrationHash) { $lastMigrationHash = NULL; } } // モデルがまだ未生成ならモデルをテーブル定義から生成する if (!isset(self::$_models[$tableName])) { // 親クラスを決める $superModelName = "ModelBase"; if (class_exists($modelName . "Extension")) { $superModelName = $modelName . "Extension"; } elseif (NULL !== $unNumberingModelName && class_exists($unNumberingModelName . "Extension")) { $superModelName = $unNumberingModelName . "Extension"; } // 上で見つからなければdefault.modelmainも探してみる if ("ModelBase" === $superModelName) { loadModule("default.modelmain." . $modelName . "Extension", TRUE); if (class_exists($modelName . "Extension")) { $superModelName = $modelName . "Extension"; } elseif (NULL !== $unNumberingModelName) { loadModule("default.modelmain." . $unNumberingModelName . "Extension", TRUE); if (class_exists($unNumberingModelName . "Extension")) { $superModelName = $unNumberingModelName . "Extension"; } } } // テーブル定義を取得 $tableDefs = self::getModelPropertyDefs($argDBO, $tableName); $varDef = $tableDefs['varDef']; $describeDef = $tableDefs['describeDef']; // モデルクラスの自動生成 $varDef .= "public \$sequenceSelectQuery = \"" . $argSeqQuery . "\"; "; // InterfaceはフレームワークのmodelクラスでI/Oの実装を強制する $baseModelClassDefine = "class " . $modelName . " extends " . $superModelName . " implements Model { %vars% public function __construct(\$argDBO, \$argExtractionCondition=NULL, \$argBinds=NULL){ %describes% parent::__construct(\$argDBO, \$argExtractionCondition, \$argBinds); } }"; $baseModelClassDefine = str_replace("%vars%", $varDef, $baseModelClassDefine); $baseModelClassDefine = str_replace("%describes%", $describeDef, $baseModelClassDefine); // モデルクラス定義からクラス生成 eval($baseModelClassDefine); // 生成したクラスを取っておく self::$_models[$tableName] = $modelName; // オートマイグレーションが有効だった場合、定義の更新が無いか確認 if (function_exists('getAutoMigrationEnabled') && TRUE === getAutoMigrationEnabled()) { // あれば新しいマイグレーションファイルを生成 MigrationManager::resolve($argDBO, $tableName, $lastMigrationHash); } } $model = new self::$_models[$tableName]($argDBO, $argExtractionCondition, $argBinds); $model->className = $modelName; // テーブル定義のハッシュ値を取っておく self::$modelHashs[$tableName] = sha1(serialize($model->describes)); return $model; }