예제 #1
0
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $configOptions = array();
     if ($this->hasInputOption('output-dir', $input)) {
         $configOptions['propel']['paths']['migrationDir'] = $input->getOption('output-dir');
     }
     if ($this->hasInputOption('migration-table', $input)) {
         $configOptions['propel']['migrations']['tableName'] = $input->getOption('migration-table');
     }
     $generatorConfig = $this->getGeneratorConfig($configOptions, $input);
     $this->createDirectory($generatorConfig->getSection('paths')['migrationDir']);
     $manager = new MigrationManager();
     $manager->setGeneratorConfig($generatorConfig);
     $connections = array();
     $optionConnections = $input->getOption('connection');
     if (!$optionConnections) {
         $connections = $generatorConfig->getBuildConnections();
     } else {
         foreach ($optionConnections as $connection) {
             list($name, $dsn, $infos) = $this->parseConnection($connection);
             $connections[$name] = array_merge(array('dsn' => $dsn), $infos);
         }
     }
     $manager->setConnections($connections);
     $manager->setMigrationTable($generatorConfig->getSection('migrations')['tableName']);
     $manager->setWorkingDirectory($generatorConfig->getSection('paths')['migrationDir']);
     $previousTimestamps = $manager->getAlreadyExecutedMigrationTimestamps();
     if (!($nextMigrationTimestamp = array_pop($previousTimestamps))) {
         $output->writeln('No migration were ever executed on this database - nothing to reverse.');
         return false;
     }
     $output->writeln(sprintf('Executing migration %s down', $manager->getMigrationClassName($nextMigrationTimestamp)));
     if ($nbPreviousTimestamps = count($previousTimestamps)) {
         $previousTimestamp = array_pop($previousTimestamps);
     } else {
         $previousTimestamp = 0;
     }
     $migration = $manager->getMigrationObject($nextMigrationTimestamp);
     if (!$input->getOption('fake')) {
         if (false === $migration->preDown($manager)) {
             if ($input->getOption('force')) {
                 $output->writeln('<error>preDown() returned false. Continue migration.</error>');
             } else {
                 $output->writeln('<error>preDown() returned false. Aborting migration.</error>');
                 return false;
             }
         }
     }
     foreach ($migration->getDownSQL() as $datasource => $sql) {
         $connection = $manager->getConnection($datasource);
         if ($input->getOption('verbose')) {
             $output->writeln(sprintf('Connecting to database "%s" using DSN "%s"', $datasource, $connection['dsn']));
         }
         $conn = $manager->getAdapterConnection($datasource);
         $res = 0;
         $statements = SqlParser::parseString($sql);
         if (!$input->getOption('fake')) {
             foreach ($statements as $statement) {
                 try {
                     if ($input->getOption('verbose')) {
                         $output->writeln(sprintf('Executing statement "%s"', $statement));
                     }
                     $conn->exec($statement);
                     $res++;
                 } catch (\Exception $e) {
                     if ($input->getOption('force')) {
                         //continue, but print error message
                         $output->writeln(sprintf('<error>Failed to execute SQL "%s". Continue migration.</error>', $statement));
                     } else {
                         throw new RuntimeException(sprintf('<error>Failed to execute SQL "%s". Aborting migration.</error>', $statement), 0, $e);
                     }
                 }
             }
             $output->writeln(sprintf('%d of %d SQL statements executed successfully on datasource "%s"', $res, count($statements), $datasource));
         }
         $manager->removeMigrationTimestamp($datasource, $nextMigrationTimestamp);
         if ($input->getOption('verbose')) {
             $output->writeln(sprintf('Downgraded migration date to %d for datasource "%s"', $previousTimestamp, $datasource));
         }
     }
     if (!$input->getOption('fake')) {
         $migration->postDown($manager);
     }
     if ($nbPreviousTimestamps) {
         $output->writeln(sprintf('Reverse migration complete. %d more migrations available for reverse.', $nbPreviousTimestamps));
     } else {
         $output->writeln('Reverse migration complete. No more migration available for reverse');
     }
 }
예제 #2
0
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $generatorConfig = $this->getGeneratorConfig(array('propel.platform.class' => $input->getOption('platform')), $input);
     $this->createDirectory($input->getOption('output-dir'));
     $manager = new MigrationManager();
     $manager->setGeneratorConfig($generatorConfig);
     $connections = array();
     foreach ($input->getOption('connection') as $connection) {
         list($name, $dsn, $infos) = $this->parseConnection($connection);
         $connections[$name] = array_merge(array('dsn' => $dsn), $infos);
     }
     $manager->setConnections($connections);
     $manager->setMigrationTable($input->getOption('migration-table'));
     $manager->setWorkingDirectory($input->getOption('output-dir'));
     $previousTimestamps = $manager->getAlreadyExecutedMigrationTimestamps();
     if (!($nextMigrationTimestamp = array_pop($previousTimestamps))) {
         $output->writeln('No migration were ever executed on this database - nothing to reverse.');
         return false;
     }
     $output->writeln(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)) {
         $output->writeln('<error>preDown() returned false. Aborting migration.</error>');
         return false;
     }
     foreach ($migration->getDownSQL() as $datasource => $sql) {
         $connection = $manager->getConnection($datasource);
         if ($input->getOption('verbose')) {
             $output->writeln(sprintf('Connecting to database "%s" using DSN "%s"', $datasource, $connection['dsn']));
         }
         $conn = $manager->getAdapterConnection($datasource);
         $res = 0;
         $statements = SqlParser::parseString($sql);
         foreach ($statements as $statement) {
             try {
                 if ($input->getOption('verbose')) {
                     $output->writeln(sprintf('Executing statement "%s"', $statement));
                 }
                 $stmt = $conn->prepare($statement);
                 $stmt->execute();
                 $res++;
             } catch (PDOException $e) {
                 $output->writeln(sprintf('<error>Failed to execute SQL "%s"</error>', $statement));
             }
         }
         if (!$res) {
             $output->writeln('No statement was executed. The version was not updated.');
             $output->writeln(sprintf('Please review the code in "%s"', $manager->getMigrationDir() . DIRECTORY_SEPARATOR . $manager->getMigrationClassName($nextMigrationTimestamp)));
             $output->writeln('<error>Migration aborted</error>');
             return false;
         }
         $output->writeln(sprintf('%d of %d SQL statements executed successfully on datasource "%s"', $res, count($statements), $datasource));
         $manager->updateLatestMigrationTimestamp($datasource, $previousTimestamp);
         if ($input->getOption('verbose')) {
             $output->writeln(sprintf('Downgraded migration date to %d for datasource "%s"', $previousTimestamp, $datasource));
         }
     }
     $migration->postDown($manager);
     if ($nbPreviousTimestamps) {
         $output->writeln(sprintf('Reverse migration complete. %d more migrations available for reverse.', $nbPreviousTimestamps));
     } else {
         $output->writeln('Reverse migration complete. No more migration available for reverse');
     }
 }