public function main() { $manager = new PropelMigrationManager(); $manager->setConnections($this->getGeneratorConfig()->getBuildConnections()); $manager->setMigrationTable($this->getMigrationTable()); $manager->setMigrationDir($this->getOutputDirectory()); // the following is a verbose version of PropelMigrationManager::getValidMigrationTimestamps() // mostly for explicit output $this->log('Checking Database Versions...'); foreach ($manager->getConnections() as $datasource => $params) { $this->log(sprintf('Connecting to database "%s" using DSN "%s"', $datasource, $params['dsn']), Project::MSG_VERBOSE); if (!$manager->migrationTableExists($datasource)) { $this->log(sprintf('Migration table does not exist in datasource "%s"; creating it.', $datasource), Project::MSG_VERBOSE); $manager->createMigrationTable($datasource); } } if ($oldestMigrationTimestamp = $manager->getOldestDatabaseVersion()) { $this->log(sprintf('Latest migration was executed on %s (timestamp %d)', date('Y-m-d H:i:s', $oldestMigrationTimestamp), $oldestMigrationTimestamp), Project::MSG_VERBOSE); } else { $this->log('No migration was ever executed on these connection settings.', Project::MSG_VERBOSE); } $this->log('Listing Migration files...'); $dir = $this->getOutputDirectory(); $migrationTimestamps = $manager->getMigrationTimestamps(); $nbExistingMigrations = count($migrationTimestamps); if ($migrationTimestamps) { $this->log(sprintf('%d valid migration classes found in "%s"', $nbExistingMigrations, $dir), Project::MSG_VERBOSE); if ($validTimestamps = $manager->getValidMigrationTimestamps()) { $countValidTimestamps = count($validTimestamps); if ($countValidTimestamps == 1) { $this->log('1 migration needs to be executed:'); } else { $this->log(sprintf('%d migrations need to be executed:', $countValidTimestamps)); } } foreach ($migrationTimestamps as $timestamp) { $this->log(sprintf(' %s %s %s', $timestamp == $oldestMigrationTimestamp ? '>' : ' ', $manager->getMigrationClassName($timestamp), $timestamp <= $oldestMigrationTimestamp ? '(executed)' : ''), $timestamp <= $oldestMigrationTimestamp ? Project::MSG_VERBOSE : Project::MSG_INFO); } } else { $this->log(sprintf('No migration file found in "%s".', $dir)); $this->log('Make sure you run the sql-diff task.'); return false; } $migrationTimestamps = $manager->getValidMigrationTimestamps(); $nbNotYetExecutedMigrations = count($migrationTimestamps); if (!$nbNotYetExecutedMigrations) { $this->log('All migration files were already executed - Nothing to migrate.'); return false; } $this->log(sprintf('Call the "migrate" task to execute %s', $countValidTimestamps == 1 ? 'it' : 'them')); }
/** * @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); $this->logSection('propel', 'Checking Database Versions...'); foreach ($connections as $name => $params) { if ($options['verbose']) { $this->logSection('propel', sprintf(' Connecting to database "%s" using DSN "%s"', $name, $params['dsn']), null, 'COMMENT'); } if (!$manager->migrationTableExists($name)) { if ($options['verbose']) { $this->logSection('propel', sprintf(' Migration table does not exist in datasource "%s"; creating it.', $name), null, 'COMMENT'); } $manager->createMigrationTable($name); } } $oldestMigrationTimestamp = $manager->getOldestDatabaseVersion(); if ($options['verbose']) { if ($oldestMigrationTimestamp) { $this->logSection('propel', sprintf(' Latest migration was executed on %s (timestamp %d)', date('Y-m-d H:i:s', $oldestMigrationTimestamp), $oldestMigrationTimestamp), null, 'COMMENT'); } else { $this->logSection('propel', ' No migration was ever executed on these connection settings.', null, 'COMMENT'); } } $this->logSection('propel', 'Listing Migration files...'); $migrationTimestamps = $manager->getMigrationTimestamps(); $nbExistingMigrations = count($migrationTimestamps); if ($migrationTimestamps) { if ($options['verbose']) { $this->logSection('propel', sprintf(' %d valid migration classes found in "%s"', $nbExistingMigrations, $options['migration-dir']), null, 'COMMENT'); } if ($validTimestamps = $manager->getValidMigrationTimestamps()) { $countValidTimestamps = count($validTimestamps); if ($countValidTimestamps == 1) { $this->logSection('propel', '1 migration needs to be executed:'); } else { $this->logSection('propel', sprintf('%d migrations need to be executed:', $countValidTimestamps)); } } foreach ($migrationTimestamps as $timestamp) { if ($timestamp <= $oldestMigrationTimestamp && $options['verbose']) { $this->logSection('propel', sprintf(' %s %s (executed)', $timestamp == $oldestMigrationTimestamp ? '>' : ' ', $manager->getMigrationClassName($timestamp)), null, 'COMMENT'); } elseif ($timestamp > $oldestMigrationTimestamp) { $this->logSection('propel', sprintf(' %s', $manager->getMigrationClassName($timestamp))); } } } else { $this->logSection('propel', sprintf('No migration file found in "%s".', $options['migration-dir'])); $this->logSection('propel', 'Make sure you run the sql-diff task.'); return false; } $migrationTimestamps = $manager->getValidMigrationTimestamps(); $nbNotYetExecutedMigrations = count($migrationTimestamps); if (!$nbNotYetExecutedMigrations) { $this->logSection('propel', 'All migration files were already executed - Nothing to migrate.'); return false; } $this->logSection('propel', sprintf('Call the "propel:migrate" task to execute %s', $countValidTimestamps == 1 ? 'it' : 'them')); }