protected function dropColumn() { $restore = $this->getRestoreFileName(); if (Sabel_Db_Migration_Manager::isUpgrade()) { if (is_file($restore)) { unlink($restore); } $columns = $this->getReader()->readDropColumn()->getColumns(); $schema = $this->getSchema()->getTable($this->tblName); $sColumns = $schema->getColumns(); $writer = new Sabel_Db_Migration_Writer($restore); $writer->writeColumns($schema, $columns); $writer->close(); foreach ($columns as $column) { if (isset($sColumns[$column])) { unset($sColumns[$column]); } else { $warning = "column '{$column}' does not exist. (SKIP)"; Sabel_Console::warning($warning); } } $this->dropColumnsAndRemakeTable($sColumns, $schema); } else { $columns = $this->getReader($restore)->readAddColumn()->getColumns(); $this->execAddColumn($columns); } }
public function execute() { if (Sabel_Db_Migration_Manager::isUpgrade()) { $queries = $this->upgradeQueries; } else { $queries = $this->downgradeQueries; } $stmt = Sabel_Db_Migration_Manager::getStatement(); foreach ($queries as $query) { $stmt->setQuery($query)->execute(); } }
protected function addColumn() { $columns = $this->getReader()->readAddColumn()->getColumns(); if (Sabel_Db_Migration_Manager::isUpgrade()) { $this->execAddColumn($columns); } else { $tblName = convert_to_tablename($this->mdlName); $quotedTblName = $this->quoteIdentifier($tblName); foreach ($columns as $column) { $this->dropDefaultConstraint($tblName, $column->name); $colName = $this->quoteIdentifier($column->name); $this->executeQuery("ALTER TABLE {$tblName} DROP COLUMN {$colName}"); } } }
protected function drop() { $restore = $this->getRestoreFileName(); if (Sabel_Db_Migration_Manager::isUpgrade()) { if (is_file($restore)) { unlink($restore); } $schema = $this->getSchema()->getTable($this->tblName); $writer = new Sabel_Db_Migration_Writer($restore); $writer->writeTable($schema)->close(); $this->executeQuery("DROP TABLE " . $this->quoteIdentifier($this->tblName)); $this->dropSequence($schema->getSequenceColumn()); } else { $this->createTable($restore); } }
public function drop() { if (Sabel_Db_Migration_Manager::isUpgrade()) { $restore = $this->getRestoreFileName(); if (is_file($restore)) { unlink($restore); } $schema = $this->getSchema(); $tblSchema = $schema->getTable($this->tblName); $engine = $schema->getTableEngine($this->tblName); $writer = new Sabel_Db_Migration_Writer($restore); $writer->writeTable($tblSchema); $writer->write('$create->options("engine", "' . $engine . '");'); $writer->write(PHP_EOL)->close(); $this->executeQuery("DROP TABLE " . $this->quoteIdentifier($this->tblName)); } else { $this->createTable($this->getRestoreFileName()); } }
protected function _execMigration() { $version = $this->currentVersion; $to = (int) $this->migrateTo; if ((int) $version === $to) { $this->message("NO CHANGES FROM {$to}"); exit; } $doNext = false; if ($version < $to) { $next = $version + 1; $num = $next; $mode = "upgrade"; $doNext = $next < $to; } else { $next = $version - 1; $num = $version; $mode = "downgrade"; $doNext = $next > $to; } Sabel_Db_Migration_Manager::setApplyMode($mode); $instance = Sabel_Db::createMigrator($this->connectionName); $directory = Sabel_Db_Migration_Manager::getDirectory(); $instance->execute($directory . DS . $this->files[$num]); $this->updateVersionNumber($next); return $doNext; }
public static function setDirectory($directory) { self::$directory = $directory; }
<?php define("RUN_BASE", getcwd()); require_once "/usr/local/lib/php/Sabel/Sabel.php"; require_once RUN_BASE . "/config/INIT.php"; require_once RUN_BASE . "/config/environment.php"; $configs = array("sqlite" => array("package" => "sabel.db.pdo.sqlite", "database" => "/home/ebine/test.sq3"), "mysql" => array("package" => "sabel.db.mysql", "host" => "127.0.0.1", "database" => "sdb_test", "port" => "3306", "user" => "root", "password" => ""), "pgsql" => array("package" => "sabel.db.pgsql", "host" => "127.0.0.1", "database" => "sdb_test", "user" => "pgsql", "password" => "pgsql"), "oci" => array("package" => "sabel.db.oci", "host" => "127.0.0.1", "database" => "XE", "schema" => "DEVELOP", "user" => "DEVELOP", "password" => "DEVELOP")); foreach ($configs as $key => $param) { Sabel_Db_Config::add($key, $param); } $args = $_SERVER["argv"]; $path = $args[1]; $conName = $args[2]; $type = $args[3]; $schema = Sabel_Db::createMetadata($conName); $stmt = Sabel_Db::createStatement($conName); Sabel_Db_Migration_Manager::setSchema($schema); Sabel_Db_Migration_Manager::setStatement($stmt); Sabel_Db_Migration_Manager::setDirectory(RUN_BASE . "/migration/tmp"); Sabel_Db_Migration_Manager::setApplyMode($type); $dirs = explode(".", Sabel_Db_Config::getPackage($conName)); $className = implode("_", array_map("ucfirst", $dirs)) . "_Migration"; $mig = new $className(); $mig->execute($path);
protected function getStatement() { return Sabel_Db_Migration_Manager::getStatement(); }