/** * @see sfTask */ protected function execute($arguments = array(), $options = array()) { $databaseManager = new sfDatabaseManager($this->configuration); $connections = $this->getConnections($databaseManager); //$connection = $databaseManager->getDatabase($options['connection'] ? $options['connection'] : null)->getConnection(); $i = new afsDbInfo(); $this->logSection('propel', 'Reading databases structure...'); $ad = new AppData(); $totalNbTables = 0; foreach ($connections as $name => $params) { $pdo = $databaseManager->getDatabase($name)->getConnection(); $database = new Database($name); $platform = $this->getPlatform($databaseManager, $name); $database->setPlatform($platform); $database->setDefaultIdMethod(IDMethod::NATIVE); $parser = $this->getParser($databaseManager, $name, $pdo); //$parser->setMigrationTable($options['migration-table']); $parser->setPlatform($platform); $nbTables = $parser->parse($database); $ad->addDatabase($database); $totalNbTables += $nbTables; $this->logSection('propel', sprintf(' %d tables imported from database "%s"', $nbTables, $name), null, 'COMMENT'); } if ($totalNbTables) { $this->logSection('propel', sprintf('%d tables imported from databases.', $totalNbTables)); } else { $this->logSection('propel', 'Database is empty'); } $this->logSection('propel', 'Loading XML schema files...'); Phing::startup(); // required to locate behavior classes... $this->schemaToXML(self::DO_NOT_CHECK_SCHEMA, 'generated-'); $this->copyXmlSchemaFromPlugins('generated-'); $appData = $this->getModels($databaseManager, true); $this->logSection('propel', sprintf('%d tables defined in the schema files.', $appData->countTables())); $this->cleanup(true); $this->logSection('sql-diff', 'Comparing databases and schemas...'); $manager = new PropelMigrationManager(); $manager->setConnections($connections); foreach ($ad->getDatabases() as $database) { $name = $database->getName(); $filenameDiff = sfConfig::get('sf_data_dir') . "/sql/{$name}." . time() . ".diff.sql"; $this->logSection('sql-diff', sprintf(' Comparing database "%s"', $name), null, 'COMMENT'); if (!$appData->hasDatabase($name)) { // FIXME: tables present in database but not in XML continue; } $databaseDiff = PropelDatabaseComparator::computeDiff($database, $appData->getDatabase($name)); if (!$databaseDiff) { //no diff } $this->logSection('sql-diff', sprintf('Structure of database was modified in datasource "%s": %s', $name, $databaseDiff->getDescription())); $platform = $this->getPlatform($databaseManager, $name); //up sql $upDiff = $platform->getModifyDatabaseDDL($databaseDiff); //down sql $downDiff = $platform->getModifyDatabaseDDL($databaseDiff->getReverseDiff()); if ($databaseDiff) { $this->logSection('sql-diff', "Writing file {$filenameDiff}"); afStudioUtil::writeFile($filenameDiff, $upDiff); if ($options['insert'] === true || $options['insert'] === 'true') { $this->logSection('sql-diff', "Inserting sql diff"); $i->executeSql($upDiff, Propel::getConnection($name)); } if ($options['build'] === true || $options['build'] === 'true') { $this->logSection('sql-diff', 'Creating models from current schema'); $this->createTask('propel:build-model')->run(); $this->logSection('sql-diff', 'Creating forms from current schema'); $this->createTask('propel:build-forms')->run(); $this->logSection('sql-diff', 'Setting AppFlower project permissions'); $this->createTask('afs:fix-perms')->run(); $this->logSection('sql-diff', 'Creating AppFlower validator cache'); $this->createTask('appflower:validator-cache')->run(array('frontend', 'cache', 'yes')); $this->logSection('sql-diff', 'Clearing Symfony cache'); $this->createTask('cc')->run(); } } } }
/** * Main method builds all the targets for a typical propel project. */ public function main() { // check to make sure task received all correct params $this->validate(); $generatorConfig = $this->getGeneratorConfig(); // loading model from database $this->log('Reading databases structure...'); $connections = $generatorConfig->getBuildConnections(); if (!$connections) { throw new Exception('You must define database connection settings in a buildtime-conf.xml file to use diff'); } $totalNbTables = 0; $ad = new AppData(); foreach ($connections as $name => $params) { $this->log(sprintf('Connecting to database "%s" using DSN "%s"', $name, $params['dsn']), Project::MSG_VERBOSE); $pdo = $generatorConfig->getBuildPDO($name); $database = new Database($name); $platform = $generatorConfig->getConfiguredPlatform($pdo); $database->setPlatform($platform); $database->setDefaultIdMethod(IDMethod::NATIVE); $parser = $generatorConfig->getConfiguredSchemaParser($pdo); $nbTables = $parser->parse($database, $this); $ad->addDatabase($database); $totalNbTables += $nbTables; $this->log(sprintf('%d tables imported from database "%s"', $nbTables, $name), Project::MSG_VERBOSE); } if ($totalNbTables) { $this->log(sprintf('%d tables imported from databases.', $totalNbTables)); } else { $this->log('Database is empty'); } // loading model from XML $this->packageObjectModel = true; $appDatasFromXml = $this->getDataModels(); $appDataFromXml = array_pop($appDatasFromXml); // comparing models $this->log('Comparing models...'); $manager = new PropelMigrationManager(); $manager->setConnections($connections); $manager->setMigrationDir($this->getOutputDirectory()); $migrationsUp = array(); $migrationsDown = array(); foreach ($ad->getDatabases() as $database) { $name = $database->getName(); $this->log(sprintf('Comparing database "%s"', $name), Project::MSG_VERBOSE); if (!$appDataFromXml->hasDatabase($name)) { // FIXME: tables present in database but not in XML continue; } $databaseDiff = PropelDatabaseComparator::computeDiff($database, $appDataFromXml->getDatabase($name), $this->isCaseInsensitive()); if (!$databaseDiff) { $this->log(sprintf('Same XML and database structures for datasource "%s" - no diff to generate', $name), Project::MSG_VERBOSE); continue; } $this->log(sprintf('Structure of database was modified in datasource "%s": %s', $name, $databaseDiff->getDescription())); $platform = $generatorConfig->getConfiguredPlatform(null, $name); $migrationsUp[$name] = $platform->getModifyDatabaseDDL($databaseDiff); $migrationsDown[$name] = $platform->getModifyDatabaseDDL($databaseDiff->getReverseDiff()); } if (!$migrationsUp) { $this->log('Same XML and database structures for all datasource - no diff to generate'); return; } $timestamp = time(); $migrationFileName = $manager->getMigrationFileName($timestamp); $migrationClassBody = $manager->getMigrationClassBody($migrationsUp, $migrationsDown, $timestamp); $_f = new PhingFile($this->getOutputDirectory(), $migrationFileName); file_put_contents($_f->getAbsolutePath(), $migrationClassBody); $this->log(sprintf('"%s" file successfully created in %s', $_f->getName(), $_f->getParent())); if ($editorCmd = $this->getEditorCmd()) { $this->log(sprintf('Using "%s" as text editor', $editorCmd)); shell_exec($editorCmd . ' ' . escapeshellarg($_f->getAbsolutePath())); } else { $this->log(' Please review the generated SQL statements, and add data migration code if necessary.'); $this->log(' Once the migration class is valid, call the "migrate" task to execute it.'); } }
/** * @see sfTask */ protected function execute($arguments = array(), $options = array()) { $databaseManager = new sfDatabaseManager($this->configuration); $connections = $this->getConnections($databaseManager); $this->logSection('propel', 'Reading databases structure...'); $ad = new AppData(); $totalNbTables = 0; foreach ($connections as $name => $params) { if ($options['verbose']) { $this->logSection('propel', sprintf(' Connecting to database "%s" using DSN "%s"', $name, $params['dsn']), null, 'COMMENT'); } $pdo = $databaseManager->getDatabase($name)->getConnection(); $database = new Database($name); $platform = $this->getPlatform($databaseManager, $name); $database->setPlatform($platform); $database->setDefaultIdMethod(IDMethod::NATIVE); $parser = $this->getParser($databaseManager, $name, $pdo); $parser->setMigrationTable($options['migration-table']); $parser->setPlatform($platform); $nbTables = $parser->parse($database); $ad->addDatabase($database); $totalNbTables += $nbTables; if ($options['verbose']) { $this->logSection('propel', sprintf(' %d tables imported from database "%s"', $nbTables, $name), null, 'COMMENT'); } } if ($totalNbTables) { $this->logSection('propel', sprintf('%d tables imported from databases.', $totalNbTables)); } else { $this->logSection('propel', 'Database is empty'); } $this->logSection('propel', 'Loading XML schema files...'); Phing::startup(); // required to locate behavior classes... $this->schemaToXML(self::DO_NOT_CHECK_SCHEMA, 'generated-'); $this->copyXmlSchemaFromPlugins('generated-'); $appData = $this->getModels($databaseManager, $options['verbose']); $this->logSection('propel', sprintf('%d tables defined in the schema files.', $appData->countTables())); $this->cleanup($options['verbose']); $this->logSection('propel', 'Comparing databases and schemas...'); $manager = new PropelMigrationManager(); $manager->setConnections($connections); $migrationsUp = array(); $migrationsDown = array(); foreach ($ad->getDatabases() as $database) { $name = $database->getName(); if ($options['verbose']) { $this->logSection('propel', sprintf(' Comparing database "%s"', $name), null, 'COMMENT'); } if (!$appData->hasDatabase($name)) { // FIXME: tables present in database but not in XML continue; } $databaseDiff = PropelDatabaseComparator::computeDiff($database, $appData->getDatabase($name)); if (!$databaseDiff) { if ($options['verbose']) { $this->logSection('propel', sprintf(' Same XML and database structures for datasource "%s" - no diff to generate', $name), null, 'COMMENT'); } continue; } $this->logSection('propel', sprintf('Structure of database was modified in datasource "%s": %s', $name, $databaseDiff->getDescription())); if ($options['verbose']) { $this->logBlock($databaseDiff, 'COMMENT'); } $platform = $this->getPlatform($databaseManager, $name); $migrationsUp[$name] = $platform->getModifyDatabaseDDL($databaseDiff); $migrationsDown[$name] = $platform->getModifyDatabaseDDL($databaseDiff->getReverseDiff()); } if (!$migrationsUp) { $this->logSection('propel', 'Same XML and database structures for all datasources - no diff to generate'); return; } $timestamp = time(); $migrationDirectory = sfConfig::get('sf_root_dir') . DIRECTORY_SEPARATOR . $options['migration-dir']; $migrationFileName = $manager->getMigrationFileName($timestamp); $migrationFilePath = $migrationDirectory . DIRECTORY_SEPARATOR . $migrationFileName; if ($options['ask-confirmation'] && !$this->askConfirmation(array(sprintf('Migration class will be generated in %s', $migrationFilePath), 'Are you sure you want to proceed? (Y/n)'), 'QUESTION_LARGE', true)) { $this->logSection('propel', 'Task aborted.'); return 1; } $this->getFilesystem()->mkdirs($migrationDirectory); $migrationClassBody = $manager->getMigrationClassBody($migrationsUp, $migrationsDown, $timestamp); file_put_contents($migrationFilePath, $migrationClassBody); $this->logSection('propel', sprintf('"%s" file successfully created in %s', $migrationFileName, $migrationDirectory)); if ($editorCmd = $options['editor-cmd']) { $this->logSection('propel', sprintf('Using "%s" as text editor', $editorCmd)); shell_exec($editorCmd . ' ' . escapeshellarg($migrationFilePath)); } else { $this->logSection('propel', ' Please review the generated SQL statements, and add data migration code if necessary.'); $this->logSection('propel', ' Once the migration class is valid, call the "propel:migrate" task to execute it.'); } }