public function testMigration() { $migration = new Doctrine_Migration('migration_classes'); $this->assertFalse($migration->hasMigrated()); $migration->setCurrentVersion(3); $migration->migrate(0); $this->assertEqual($migration->getCurrentVersion(), 0); $this->assertEqual($migration->getLatestVersion(), 4); $this->assertEqual($migration->getNextVersion(), 5); $current = $migration->getCurrentVersion(); $migration->setCurrentVersion(100); $this->assertEqual($migration->getCurrentVersion(), 100); $migration->setCurrentVersion($current); $migration->migrate(3); $this->assertTrue($migration->hasMigrated()); $this->assertEqual($migration->getCurrentVersion(), 3); $this->assertTrue($this->conn->import->tableExists('migration_phonenumber')); $this->assertTrue($this->conn->import->tableExists('migration_user')); $this->assertTrue($this->conn->import->tableExists('migration_profile')); $migration->migrate(4); $this->assertFalse($this->conn->import->tableExists('migration_profile')); $migration->migrate(0); $this->assertEqual($migration->getCurrentVersion(), 0); $this->assertTrue($migration->getMigrationClass(1) instanceof AddPhonenumber); $this->assertTrue($migration->getMigrationClass(2) instanceof AddUser); $this->assertTrue($migration->getMigrationClass(3) instanceof AddProfile); $this->assertTrue($migration->getMigrationClass(4) instanceof DropProfile); $this->assertFalse($this->conn->import->tableExists('migration_phonenumber')); $this->assertFalse($this->conn->import->tableExists('migration_user')); $this->assertFalse($this->conn->import->tableExists('migration_profile')); }
function migrate_db() { try { $path = makeMigrationsDir(); $migration = new Doctrine_Migration($path); $migration->migrate(); $account = db_get_account('alice'); if (!$account) { $account = array('login' => 'alice', 'crypted_password' => 'b6263bb14858294c08e4bdfceba90363e10d72b4', 'name' => 'Alice Yamada', 'name_ja_kana_jp' => 'ヤマダアリサ', 'name_ja_hani_jp' => '山田亜理紗', 'given_name' => 'Alice', 'given_name_ja_kana_jp' => 'アリサ', 'given_name_ja_hani_jp' => '亜理紗', 'family_name' => 'Yamada', 'family_name_ja_kana_jp' => 'ヤマダ', 'family_name_ja_hani_jp' => '山田', 'nickname' => 'Alice Nickname', 'preferred_username' => 'AlicePreferred', 'profile' => 'http://www.wonderland.com/alice', 'picture' => 'smiling_woman.jpg', 'website' => 'http://www.wonderland.com', 'email' => '*****@*****.**', 'email_verified' => 1, 'gender' => 'Female', 'birthdate' => '2000-01-01', 'zoneinfo' => 'america/Los Angeles', 'locale' => 'en', 'phone_number' => '123-456-7890', 'phone_number_verified' => 1, 'address' => '123 Wonderland Way', 'updated_at' => time()); db_save_account('alice', $account); } $account = db_get_account('bob'); if (!$account) { $account = array('login' => 'bob', 'crypted_password' => 'cc8684eed2b6544e89242558df73a7208c9391b4', 'name' => 'Bob Ikeda', 'name_ja_kana_jp' => 'イケダボブ', 'name_ja_hani_jp' => '池田保夫', 'given_name' => 'Bob', 'given_name_ja_kana_jp' => 'ボブ', 'given_name_ja_hani_jp' => '保夫', 'family_name' => 'Ikeda', 'family_name_ja_kana_jp' => 'イケダ', 'family_name_ja_hani_jp' => '池田', 'nickname' => 'Bob Nickname', 'preferred_username' => 'BobPreferred', 'profile' => 'http://www.underland.com/bob', 'picture' => 'smiling_man.jpg', 'website' => 'http://www.underland.com', 'email' => '*****@*****.**', 'email_verified' => 1, 'gender' => 'Male', 'birthdate' => '1980-02-09', 'zoneinfo' => 'France/Paris', 'locale' => 'fr', 'phone_number' => '987-234-1234', 'phone_number_verified' => 1, 'address' => '456 Underland Ct.', 'updated_at' => time()); db_save_account('bob', $account); } } catch (Doctrine_Migration_Exception $e) { if (strstr($e->getMessage(), "Already at version") === false) { throw $e; } } catch (Doctrine_Connection_Exception $e) { printf("migration exception %s\n", $e); die(2); } }
public function testMigration() { // New migration for the 'migration_classes' directory $migration = new Doctrine_Migration('migration_classes'); // Make sure the current version is 0 $this->assertEqual($migration->getCurrentVersion(), 0); // migrate to version latest version $migration->migrate($migration->getLatestVersion()); // Make sure the current version is latest version $this->assertEqual($migration->getCurrentVersion(), $migration->getLatestVersion()); // now migrate back to original version $migration->migrate(0); // Make sure the current version is 0 $this->assertEqual($migration->getCurrentVersion(), 0); }
public function testMigrateClearsErrors() { $migration = new Doctrine_Migration('migration_classes'); $migration->setCurrentVersion(3); try { $migration->migrate(3); } catch (Doctrine_Migration_Exception $e) { $this->assertTrue($migration->hasErrors()); $this->assertEqual(1, $migration->getNumErrors()); } try { $migration->migrate(3); } catch (Doctrine_Migration_Exception $e) { $this->assertTrue($migration->hasErrors()); $this->assertEqual(1, $migration->getNumErrors()); } $migration->clearErrors(); $this->assertFalse($migration->hasErrors()); $this->assertEqual(0, $migration->getNumErrors()); }
/** * Executes index action * * @param sfRequest $request A request object */ public function executeIndex(sfWebRequest $request) { $migration = new Doctrine_Migration(sfConfig::get('sf_lib_dir') . '/migration/doctrine'); if ($migration->getCurrentVersion() < $migration->getLatestVersion()) { try { $migration->migrate($migration->getLatestVersion()); } catch (Exception $e) { } $this->errors = array_merge(array_map(create_function('$e', 'return \' - \'.$e->getMessage();'), $migration->getErrors())); } }
/** * @see sfTask */ protected function execute($arguments = array(), $options = array()) { $databaseManager = new sfDatabaseManager($this->configuration); $config = $this->getCliConfig(); $migration = new Doctrine_Migration($config['migrations_path']); $from = $migration->getCurrentVersion(); if (is_numeric($arguments['version'])) { $version = $arguments['version']; } else { if ($options['up']) { $version = $from + 1; } else { if ($options['down']) { $version = $from - 1; } else { $version = $migration->getLatestVersion(); } } } if ($from == $version) { $this->logSection('doctrine', sprintf('Already at migration version %s', $version)); return; } $this->logSection('doctrine', sprintf('Migrating from version %s to %s%s', $from, $version, $options['dry-run'] ? ' (dry run)' : '')); try { $migration_classes = $migration->getMigrationClasses(); if ($version < $from) { for ($i = (int) $from - 1; $i >= (int) $version; $i--) { $this->logSection('doctrine', 'executing migration : ' . $i . ', class: ' . $migration_classes[$i]); $migration->migrate($i, $options['dry-run']); } } else { for ($i = (int) $from + 1; $i <= (int) $version; $i++) { $this->logSection('doctrine', 'executing migration : ' . $i . ', class: ' . $migration_classes[$i]); $migration->migrate($i, $options['dry-run']); } } } catch (Exception $e) { } // render errors if ($migration->hasErrors()) { if ($this->commandApplication && $this->commandApplication->withTrace()) { $this->logSection('doctrine', 'The following errors occurred:'); foreach ($migration->getErrors() as $error) { $this->commandApplication->renderException($error); } } else { $this->logBlock(array_merge(array('The following errors occurred:', ''), array_map(create_function('$e', 'return \' - \'.$e->getMessage();'), $migration->getErrors())), 'ERROR_LARGE'); } return 1; } $this->logSection('doctrine', 'Migration complete'); }
/** * Perform a migration process by specifying the migration number/version to * migrate to. It will automatically know whether you are migrating up or down * based on the current version of the database. * * @param integer $to Version to migrate to * @param boolean $dryRun Whether or not to run the migrate process as a dry run * @return integer $to Version number migrated to * @throws Doctrine_Exception */ public function migrate($to = null, $dryRun = false) { if (empty($to)) { $to = $this->_migration->getLatestVersion(); } $result = $this->_migration->migrate($to, $dryRun); // TODO: This is a dirty fix for problems... if we get here migration was successful if (!$dryRun && $result) { $this->_migration->setCurrentVersion($to); } return $result; }
/** * Migrate database to specified $to version. Migrates from current to latest if you do not specify. * * @param string $migrationsPath Path to migrations directory which contains your migration classes * @param string $to Version you wish to migrate to. * @return bool true * @throws new Doctrine_Migration_Exception */ public static function migrate($migrationsPath, $to = null) { $migration = new Doctrine_Migration($migrationsPath); return $migration->migrate($to); }
<?php require_once 'bootstrap.php'; echo "Executing migration scripts..."; $migration = new Doctrine_Migration('migrations'); $migration->migrate();
public function migrate($version = null) { $migration = new Doctrine_Migration('application/migrations'); $migration->migrate($version); }
/** * migrate * * @see Doctrine_Migration */ public function migrate($to = null) { if (is_null($to)) { if (!is_null($this->revision)) { $to = $this->revision; } } parent::migrate($to); $file = $this->getMigrationFixtureFile(); if ($file) { Doctrine::loadData($file, true); } }
/** * (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; } }
} $migration = new Doctrine_Migration(__DIR__ . '/classes', $conn); $migration->setTableName('doctrine_migration_version'); if (isset($_REQUEST['ver']) && (!isset($google_user) || $google_user->getNickname() == '*****@*****.**')) { $version = 0 + intval($_REQUEST['ver']); } else { $classesKeys = array_keys($migration->getMigrationClasses()); $version = 0 + array_pop($classesKeys); } if (isset($_SERVER['HTTP_HOST'])) { echo '<h1>'; } if ($migration->getCurrentVersion() == $version) { echo 'Database at version ' . $version . PHP_EOL; } else { $migration->migrate($version); echo 'Migrated succesfully to version ' . $migration->getCurrentVersion() . PHP_EOL; } if (isset($_SERVER['HTTP_HOST'])) { echo '</h1>'; } if (isset($_SERVER['SERVER_SOFTWARE']) && strstr(strtolower($_SERVER['SERVER_SOFTWARE']), 'engine')) { require_once 'google/appengine/api/cloud_storage/CloudStorageTools.php'; $path = 'gs://' . CloudStorageTools::getDefaultGoogleStorageBucketName() . '/sql'; if (file_exists($path)) { foreach (scandir($path) as $file) { $f = $path . '/' . $file; echo 'Execute ' . $file . PHP_EOL; $conn->execute(file_get_contents($f)); } }
<?php require_once 'bootstrap.php'; $migration = new Doctrine_Migration('db/migrations'); // If we got a command line argument, use that as the migration target if ($argc == 2) { $migrate_to = $argv[1]; $migration->migrate($migrate_to); } else { $migration->migrate(); }