/**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $configOptions = [];
     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 = [];
     $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(['dsn' => $dsn], $infos);
         }
     }
     $manager->setConnections($connections);
     $manager->setMigrationTable($generatorConfig->getSection('migrations')['tableName']);
     $manager->setWorkingDirectory($generatorConfig->getSection('paths')['migrationDir']);
     if (!$manager->getFirstUpMigrationTimestamp()) {
         $output->writeln('All migrations were already executed - nothing to migrate.');
         return false;
     }
     $timestamps = $manager->getValidMigrationTimestamps();
     if (count($timestamps) > 1) {
         $output->writeln(sprintf('%d migrations to execute', count($timestamps)));
     }
     foreach ($timestamps as $timestamp) {
         if ($input->getOption('fake')) {
             $output->writeln(sprintf('Faking migration %s up', $manager->getMigrationClassName($timestamp)));
         } else {
             $output->writeln(sprintf('Executing migration %s up', $manager->getMigrationClassName($timestamp)));
         }
         if (!$input->getOption('fake')) {
             $migration = $manager->getMigrationObject($timestamp);
             if (property_exists($migration, 'comment') && $migration->comment) {
                 $output->writeln(sprintf('<info>%s</info>', $migration->comment));
             }
             if (false === $migration->preUp($manager)) {
                 if ($input->getOption('force')) {
                     $output->writeln('<error>preUp() returned false. Continue migration.</error>');
                 } else {
                     $output->writeln('<error>preUp() returned false. Aborting migration.</error>');
                     return false;
                 }
             }
             foreach ($migration->getUpSQL() 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));
                         }
                         $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));
             }
         }
         // migrations for datasources have passed - update the timestamp
         // for all datasources
         foreach ($manager->getConnections() as $datasource => $connection) {
             $manager->updateLatestMigrationTimestamp($datasource, $timestamp);
             if ($input->getOption('verbose')) {
                 $output->writeln(sprintf('Updated latest migration date to %d for datasource "%s"', $timestamp, $datasource));
             }
         }
         if (!$input->getOption('fake')) {
             $migration->postUp($manager);
         }
     }
     $output->writeln('Migration complete. No further migration to execute.');
 }
 /**
  * {@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']);
     $output->writeln('Checking Database Versions...');
     foreach ($manager->getConnections() as $datasource => $params) {
         if ($input->getOption('verbose')) {
             $output->writeln(sprintf('Connecting to database "%s" using DSN "%s"', $datasource, $params['dsn']));
         }
         if (!$manager->migrationTableExists($datasource)) {
             if ($input->getOption('verbose')) {
                 $output->writeln(sprintf('Migration table does not exist in datasource "%s"; creating it.', $datasource));
             }
             $manager->createMigrationTable($datasource);
         }
     }
     $oldestMigrationTimestamp = $manager->getOldestDatabaseVersion();
     if ($input->getOption('verbose')) {
         if ($oldestMigrationTimestamp) {
             $output->writeln(sprintf('Latest migration was executed on %s (timestamp %d)', date('Y-m-d H:i:s', $oldestMigrationTimestamp), $oldestMigrationTimestamp));
         } else {
             $output->writeln('No migration was ever executed on these connection settings.');
         }
     }
     $output->writeln('Listing Migration files...');
     $dir = $generatorConfig->getSection('paths')['migrationDir'];
     $migrationTimestamps = $manager->getMigrationTimestamps();
     $nbExistingMigrations = count($migrationTimestamps);
     if ($migrationTimestamps) {
         $output->writeln(sprintf('%d valid migration classes found in "%s"', $nbExistingMigrations, $dir));
         if ($validTimestamps = $manager->getValidMigrationTimestamps()) {
             $countValidTimestamps = count($validTimestamps);
             if ($countValidTimestamps == 1) {
                 $output->writeln('1 migration needs to be executed:');
             } else {
                 $output->writeln(sprintf('%d migrations need to be executed:', $countValidTimestamps));
             }
         }
         foreach ($migrationTimestamps as $timestamp) {
             if ($timestamp > $oldestMigrationTimestamp || $input->getOption('verbose')) {
                 $output->writeln(sprintf(' %s %s %s', $timestamp == $oldestMigrationTimestamp ? '>' : ' ', $manager->getMigrationClassName($timestamp), !in_array($timestamp, $validTimestamps) ? '(executed)' : ''));
             }
         }
     } else {
         $output->writeln(sprintf('No migration file found in "%s".', $dir));
         return false;
     }
     $migrationTimestamps = $manager->getValidMigrationTimestamps();
     $nbNotYetExecutedMigrations = count($migrationTimestamps);
     if (!$nbNotYetExecutedMigrations) {
         $output->writeln('All migration files were already executed - Nothing to migrate.');
         return false;
     }
     $output->writeln(sprintf('Call the "migrate" task to execute %s', $countValidTimestamps == 1 ? 'it' : 'them'));
 }
 /**
  * {@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();
     $optionConnections = $input->getOption('connection');
     if (!$optionConnections) {
         $connections = $generatorConfig->getBuildConnections($input->getOption('input-dir'));
     } 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($input->getOption('migration-table'));
     $manager->setWorkingDirectory($input->getOption('output-dir'));
     if (!$manager->getFirstUpMigrationTimestamp()) {
         $output->writeln('All migrations were already executed - nothing to migrate.');
         return false;
     }
     $timestamps = $manager->getValidMigrationTimestamps();
     if (count($timestamps) > 1) {
         $output->writeln(sprintf('%d migrations to execute', count($timestamps)));
     }
     foreach ($timestamps as $timestamp) {
         $output->writeln(sprintf('Executing migration %s up', $manager->getMigrationClassName($timestamp)));
         $migration = $manager->getMigrationObject($timestamp);
         if (false === $migration->preUp($manager)) {
             $output->writeln('<error>preUp() returned false. Aborting migration.</error>');
             return false;
         }
         foreach ($migration->getUpSQL() 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));
                     // continue
                 }
             }
             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($timestamp)));
                 $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));
         }
         // migrations for datasources have passed - update the timestamp
         // for all datasources
         foreach ($manager->getConnections() as $datasource => $connection) {
             $manager->updateLatestMigrationTimestamp($datasource, $timestamp);
             if ($input->getOption('verbose')) {
                 $output->writeln(sprintf('Updated latest migration date to %d for datasource "%s"', $timestamp, $datasource));
             }
         }
         $migration->postUp($manager);
     }
     $output->writeln('Migration complete. No further migration to execute.');
 }