/**
  * Main method builds all the targets for a typical propel project.
  */
 public function main()
 {
     $manager = new PropelMigrationManager();
     $manager->setConnections($this->getGeneratorConfig()->getBuildConnections());
     $manager->setMigrationTable($this->getMigrationTable());
     $manager->setMigrationDir($this->getOutputDirectory());
     $previousTimestamps = $manager->getAlreadyExecutedMigrationTimestamps();
     if (!($nextMigrationTimestamp = array_pop($previousTimestamps))) {
         $this->log('No migration were ever executed on this database - nothing to reverse.');
         return false;
     }
     $this->log(sprintf('Executing migration %s down', $manager->getMigrationClassName($nextMigrationTimestamp)));
     if ($nbPreviousTimestamps = count($previousTimestamps)) {
         $previousTimestamp = array_pop($previousTimestamps);
     } else {
         $previousTimestamp = 0;
     }
     $migration = $manager->getMigrationObject($nextMigrationTimestamp);
     if (false === $migration->preDown($manager)) {
         $this->log('preDown() returned false. Aborting migration.', Project::MSG_ERR);
         return false;
     }
     foreach ($migration->getDownSQL() as $datasource => $sql) {
         $connection = $manager->getConnection($datasource);
         $this->log(sprintf('Connecting to database "%s" using DSN "%s"', $datasource, $connection['dsn']), Project::MSG_VERBOSE);
         $pdo = $manager->getPdoConnection($datasource);
         $res = 0;
         $statements = PropelSQLParser::parseString($sql);
         foreach ($statements as $statement) {
             try {
                 $this->log(sprintf('Executing statement "%s"', $statement), Project::MSG_VERBOSE);
                 $stmt = $pdo->prepare($statement);
                 $stmt->execute();
                 $res++;
             } catch (PDOException $e) {
                 $this->log(sprintf('Failed to execute SQL "%s"', $statement), Project::MSG_ERR);
                 // continue
             }
         }
         if (!$res) {
             $this->log('No statement was executed. The version was not updated.');
             $this->log(sprintf('Please review the code in "%s"', $manager->getMigrationDir() . DIRECTORY_SEPARATOR . $manager->getMigrationClassName($nextMigrationTimestamp)));
             $this->log('Migration aborted', Project::MSG_ERR);
             return false;
         }
         $this->log(sprintf('%d of %d SQL statements executed successfully on datasource "%s"', $res, count($statements), $datasource));
         $manager->updateLatestMigrationTimestamp($datasource, $previousTimestamp);
         $this->log(sprintf('Downgraded migration date to %d for datasource "%s"', $previousTimestamp, $datasource), Project::MSG_VERBOSE);
     }
     $migration->postDown($manager);
     if ($nbPreviousTimestamps) {
         $this->log(sprintf('Reverse migration complete. %d more migrations available for reverse.', $nbPreviousTimestamps));
     } else {
         $this->log('Reverse migration complete. No more migration available for reverse');
     }
 }
 /**
  * @see sfTask
  */
 protected function execute($arguments = array(), $options = array())
 {
     $databaseManager = new sfDatabaseManager($this->configuration);
     $connections = $this->getConnections($databaseManager);
     $manager = new PropelMigrationManager();
     $manager->setConnections($connections);
     $manager->setMigrationTable($options['migration-table']);
     $migrationDirectory = sfConfig::get('sf_root_dir') . DIRECTORY_SEPARATOR . $options['migration-dir'];
     $manager->setMigrationDir($migrationDirectory);
     $previousTimestamps = $manager->getAlreadyExecutedMigrationTimestamps();
     if (!($nextMigrationTimestamp = array_pop($previousTimestamps))) {
         $this->logSection('propel', 'No migration were ever executed on this database - nothing to reverse.');
         return false;
     }
     $this->logSection('propel', sprintf('Executing migration %s down', $manager->getMigrationClassName($nextMigrationTimestamp)));
     if ($nbPreviousTimestamps = count($previousTimestamps)) {
         $previousTimestamp = array_pop($previousTimestamps);
     } else {
         $previousTimestamp = 0;
     }
     $migration = $manager->getMigrationObject($nextMigrationTimestamp);
     if (false === $migration->preDown($manager)) {
         $this->logSection('propel', 'preDown() returned false. Aborting migration.', null, 'ERROR');
         return false;
     }
     foreach ($migration->getDownSQL() as $datasource => $sql) {
         $connection = $manager->getConnection($datasource);
         if ($options['verbose']) {
             $this->logSection('propel', sprintf('  Connecting to database "%s" using DSN "%s"', $datasource, $connection['dsn']), null, 'COMMENT');
         }
         $pdo = $manager->getPdoConnection($datasource);
         $res = 0;
         $statements = PropelSQLParser::parseString($sql);
         foreach ($statements as $statement) {
             try {
                 if ($options['verbose']) {
                     $this->logSection('propel', sprintf('  Executing statement "%s"', $statement), null, 'COMMENT');
                 }
                 $stmt = $pdo->prepare($statement);
                 $stmt->execute();
                 $res++;
             } catch (PDOException $e) {
                 $this->logSection(sprintf('Failed to execute SQL "%s". Aborting migration.', $statement), null, 'ERROR');
                 return false;
                 // continue
             }
         }
         if (!$res) {
             $this->logSection('propel', 'No statement was executed. The version was not updated.');
             $this->logSection('propel', sprintf('Please review the code in "%s"', $manager->getMigrationDir() . DIRECTORY_SEPARATOR . $manager->getMigrationClassName($nextMigrationTimestamp)));
             $this->logSection('propel', 'Migration aborted', null, 'ERROR');
             return false;
         }
         $this->logSection('propel', sprintf('%d of %d SQL statements executed successfully on datasource "%s"', $res, count($statements), $datasource));
         $manager->updateLatestMigrationTimestamp($datasource, $previousTimestamp);
         if ($options['verbose']) {
             $this->logSection('propel', sprintf('  Downgraded latest migration date to %d for datasource "%s"', $previousTimestamp, $datasource), null, 'COMMENT');
         }
     }
     $migration->postDown($manager);
     if ($nbPreviousTimestamps) {
         $this->logSection('propel', sprintf('Reverse migration complete. %d more migrations available for reverse.', $nbPreviousTimestamps));
     } else {
         $this->logSection('propel', 'Reverse migration complete. No more migration available for reverse');
     }
 }