Beispiel #1
0
 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'));
 }
 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(), 11);
     $this->assertEqual($migration->getNextVersion(), 12);
     $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'));
     $this->assertEqual(array(1 => 'AddPhonenumber', 2 => 'AddUser', 3 => 'AddProfile', 4 => 'DropProfile', 5 => 'Test5', 6 => 'Test6', 7 => 'Test7', 8 => 'Test8', 9 => 'Test9', 10 => 'Test10', 11 => 'Test11'), $migration->getMigrationClasses());
 }
    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);
    }
Beispiel #4
0
 /**
  * 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');
 }
 protected function execute($arguments = array(), $options = array())
 {
     // initialize the database connection
     $databaseManager = new sfDatabaseManager($this->configuration);
     $connection = $databaseManager->getDatabase($options['connection'])->getConnection();
     $config = $this->getCliConfig();
     $migration = new Doctrine_Migration($config['migrations_path']);
     $from = $migration->getCurrentVersion();
     $new_version = $arguments['new_version'];
     if ($new_version !== $from) {
         $migration->setCurrentVersion($new_version);
     }
     $this->log('Current migration version is ' . $new_version);
 }
 /**
  * @see sfTask
  */
 protected function execute($arguments = array(), $options = array())
 {
     $databaseManager = new sfDatabaseManager($this->configuration);
     $migration = new Doctrine_Migration();
     $currentVersion = $migration->getCurrentVersion();
     if (!isset($arguments['version'])) {
         $this->logSection('doctrine', 'Current migration version is ' . $currentVersion);
     } else {
         $version = $arguments['version'];
         if (!is_numeric($version)) {
             $this->logSection('doctrine', 'Unknown version ' . $version, null, 'ERROR');
             return;
         }
         $migration->setCurrentVersion($version);
         $this->logSection('doctrine', 'Current migration version was forced to ' . $version);
     }
 }
 protected function getCurrentMigrationVersion()
 {
     $migratePath = $this->_getMigrationsDirectoryPath();
     $migration = new Doctrine_Migration($migratePath);
     return $migration->getCurrentVersion();
 }
Beispiel #9
0
 /**
  * (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;
     }
 }
Beispiel #10
0
 $conn = Doctrine_Manager::connection($db);
 if (isset($google_user)) {
     echo 'user: '******'<br>';
 }
 $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;
Beispiel #11
0
 /**
  * Get the current version of the database
  *
  * @return integer $version
  */
 public function getCurrentVersion()
 {
     return $this->_migration->getCurrentVersion();
 }
Beispiel #12
0
 public function Version()
 {
     $migration = new Doctrine_Migration(APPLICATION_PATH . '/configs/migrations');
     return $migration->getCurrentVersion();
 }
 /**
  * @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();
     $to = $this->getMigrationVersion($migration, $arguments, $options);
     $noConfirmation = $options['no-confirmation'];
     $environment = $this->configuration instanceof sfApplicationConfiguration ? $this->configuration->getEnvironment() : 'all';
     // check if a migration is needed
     if ($from == $to) {
         $this->logSection('doctrine', sprintf('Already at migration version %s', $to));
         return;
     }
     // check connection is mysql
     if (!$migration->getConnection() instanceof Doctrine_Connection_Mysql) {
         $this->logSection('doctrine', 'Database connection must be MySQL', null, 'ERROR');
         return;
     }
     if ($options['disable-env']) {
         $result = $this->enableDisableEnv($options['env'], false);
         if (!$result) {
             $this->logSection('disable', 'Disabling failed task aborting', null, 'ERROR');
             return 1;
         }
     }
     // check if it's a dry run
     if ($options['dry-run'] && !$noConfirmation) {
         $confirmation = $this->askConfirmation(array('As MySQL auto commits on an alter table the database will be backed
       up prior to attempting migration and the backup restored once
       complete', 'There is the posibility of data loss during this process, It is
       set to run in the ' . $environment . ' environment', '', 'Are you sure you want to proceed? (y/N)'), 'QUESTION_LARGE', false);
         // allow user to abort
         if (!$confirmation) {
             $this->cleanUp($options);
             $this->logSection('doctrine', 'Task aborted');
             return 1;
         }
         $noConfirmation = true;
     }
     // do database backup
     $backupPath = $this->getBackupPath($arguments, $options, $environment . (!$options['keep-backup'] ? '.tmp' : ''));
     $this->logSection('mysql', 'Dumping database to ' . $backupPath);
     try {
         $this->backupDatabase($migration->getConnection(), $backupPath);
     } catch (Exception $e) {
         $this->cleanUp($options);
         $this->logBlock(array('Dumping MySQL database failed:', '', $e->getMessage()), 'ERROR_LARGE');
         return 1;
     }
     $this->logSection('mysql', 'Database dump completed');
     $this->logSection('doctrine', sprintf('Migrating from version %s to %s%s', $from, $to, $options['dry-run'] ? ' (dry run)' : ''));
     // perform migration
     try {
         $migration->migrate($to, $options['dry-run']);
     } catch (Exception $e) {
     }
     // do clean up if database needs to be restored
     if ($migration->hasErrors() || $options['dry-run']) {
         // check if we need to confirm database drop
         if (!$noConfirmation) {
             $confirmation = $this->askConfirmation(array('An error has occurred with the migration and the database can be' . ' restored to its previous state', 'To complete the restore the database in the ' . $environment . ' environment will be dropped and the  backup file (located at ' . $backupPath . ') will be used to restore the database.', '', 'Are you sure you want to proceed? (Y/n)'), 'QUESTION_LARGE', true);
             if (!$confirmation) {
                 $this->cleanUp($options);
                 $this->logSection('mysql', 'Database restore task aborted');
                 $this->logSection('mysql', 'Database backup at ' . $backupPath . 'has not been deleted');
                 $this->outputMigrationErrors($migration);
                 $this->logSection('doctrine', 'Task aborted');
                 return 1;
             }
         }
         // restore database
         try {
             $this->restoreBackup($migration->getConnection(), $backupPath);
         } catch (Exception $e) {
             $this->cleanUp($options);
             $this->outputMigrationErrors($migration);
             $this->logBlock(array('Database restore failed:', '', $e->getMessage()), 'ERROR_LARGE');
             $this->logSection('mysql', 'Database backup at ' . $backupPath . 'has not been deleted');
             return 1;
         }
         $this->logSection('mysql', 'Database restored successfully');
     }
     // delete backup if needed
     if (!$options['keep-backup']) {
         $this->logSection('mysql', 'Deleting backup file');
         $this->getFilesystem()->remove($backupPath);
     }
     if ($migration->hasErrors()) {
         $this->cleanUp($options);
         $this->outputMigrationErrors($migration);
         return 1;
     }
     $this->cleanUp($options);
     $this->logSection('doctrine', 'Migration complete');
 }