From 14c251ed7f1362a05569653f46350a1e09283862 Mon Sep 17 00:00:00 2001 From: Toni Uebernickel Date: Tue, 29 May 2012 15:00:01 +0200 Subject: [PATCH] add propel.migration.ignoreTables --- generator/default.properties | 1 + generator/lib/config/GeneratorConfig.php | 5 +++ .../model/diff/PropelDatabaseComparator.php | 20 +++++++-- generator/lib/task/PropelSQLDiffTask.php | 45 ++++++++++++++++++- 4 files changed, 67 insertions(+), 4 deletions(-) diff --git a/generator/default.properties b/generator/default.properties index 9ef4e86c9..6a581e80f 100644 --- a/generator/default.properties +++ b/generator/default.properties @@ -207,6 +207,7 @@ propel.sql.mapper.to = *.sql propel.migration.editor = propel.migration.table = propel_migration propel.migration.caseInsensitive = true +propel.migration.ignoreTables = # ------------------------------------------------------------------- # diff --git a/generator/lib/config/GeneratorConfig.php b/generator/lib/config/GeneratorConfig.php index 0568e1a71..d92957f3b 100644 --- a/generator/lib/config/GeneratorConfig.php +++ b/generator/lib/config/GeneratorConfig.php @@ -318,4 +318,9 @@ public function getBuildPDO($database) return $pdo; } + + public function getDefaultBuildConnectionName() + { + return $this->defaultBuildConnection; + } } diff --git a/generator/lib/model/diff/PropelDatabaseComparator.php b/generator/lib/model/diff/PropelDatabaseComparator.php index 12fbfe9bd..799ee5e94 100644 --- a/generator/lib/model/diff/PropelDatabaseComparator.php +++ b/generator/lib/model/diff/PropelDatabaseComparator.php @@ -82,16 +82,17 @@ public function getToDatabase() * @param Database $toDatabase * @param boolean $caseInsensitive Whether the comparison is case insensitive. * False by default. + * @param array $ignoredTables A list of table names to ignore. * * @return PropelDatabaseDiff|boolean return false if the two databases are similar */ - public static function computeDiff(Database $fromDatabase, Database $toDatabase, $caseInsensitive = false) + public static function computeDiff(Database $fromDatabase, Database $toDatabase, $caseInsensitive = false, $ignoredTables = array()) { $dc = new self(); $dc->setFromDatabase($fromDatabase); $dc->setToDatabase($toDatabase); $differences = 0; - $differences += $dc->compareTables($caseInsensitive); + $differences += $dc->compareTables($caseInsensitive, $ignoredTables); return ($differences > 0) ? $dc->getDatabaseDiff() : false; } @@ -103,10 +104,11 @@ public static function computeDiff(Database $fromDatabase, Database $toDatabase, * * @param boolean $caseInsensitive Whether the comparison is case insensitive. * False by default. + * @param array $ignoredTables A list of table names to ignore. * * @return integer The number of table differences */ - public function compareTables($caseInsensitive = false) + public function compareTables($caseInsensitive = false, $ignoredTables = array()) { $fromDatabaseTables = $this->fromDatabase->getTables(); $toDatabaseTables = $this->toDatabase->getTables(); @@ -114,6 +116,10 @@ public function compareTables($caseInsensitive = false) // check for new tables in $toDatabase foreach ($toDatabaseTables as $table) { + if (in_array($table->getName(), $ignoredTables[$this->toDatabase->getName()])) { + continue; + } + if (!$this->fromDatabase->hasTable($table->getName(), $caseInsensitive) && !$table->isSkipSql()) { $this->databaseDiff->addAddedTable($table->getName(), $table); $databaseDifferences++; @@ -122,6 +128,10 @@ public function compareTables($caseInsensitive = false) // check for removed tables in $toDatabase foreach ($fromDatabaseTables as $table) { + if (in_array($table->getName(), $ignoredTables[$this->fromDatabase->getName()])) { + continue; + } + if (!$this->toDatabase->hasTable($table->getName(), $caseInsensitive) && !$table->isSkipSql()) { $this->databaseDiff->addRemovedTable($table->getName(), $table); $databaseDifferences++; @@ -130,6 +140,10 @@ public function compareTables($caseInsensitive = false) // check for table differences foreach ($fromDatabaseTables as $fromTable) { + if (in_array($fromTable->getName(), $ignoredTables[$this->fromDatabase->getName()])) { + continue; + } + if ($this->toDatabase->hasTable($fromTable->getName(), $caseInsensitive)) { $toTable = $this->toDatabase->getTable($fromTable->getName(), $caseInsensitive); $databaseDiff = PropelTableComparator::computeDiff($fromTable, $toTable, $caseInsensitive); diff --git a/generator/lib/task/PropelSQLDiffTask.php b/generator/lib/task/PropelSQLDiffTask.php index 5f3affef4..a734c1f3d 100644 --- a/generator/lib/task/PropelSQLDiffTask.php +++ b/generator/lib/task/PropelSQLDiffTask.php @@ -113,6 +113,7 @@ public function main() throw new Exception('Uncommitted migrations have been found ; you should either execute or delete them before rerunning the \'diff\' task'); } + $ignoredTables = $this->getIgnoredTables($generatorConfig); $totalNbTables = 0; $ad = new AppData(); foreach ($connections as $name => $params) { @@ -154,7 +155,7 @@ public function main() // FIXME: tables present in database but not in XML continue; } - $databaseDiff = PropelDatabaseComparator::computeDiff($database, $appDataFromXml->getDatabase($name), $this->isCaseInsensitive()); + $databaseDiff = PropelDatabaseComparator::computeDiff($database, $appDataFromXml->getDatabase($name), $this->isCaseInsensitive(), $ignoredTables); if (!$databaseDiff) { $this->log(sprintf('Same XML and database structures for datasource "%s" - no diff to generate', $name), Project::MSG_VERBOSE); @@ -188,4 +189,46 @@ public function main() $this->log(' Once the migration class is valid, call the "migrate" task to execute it.'); } } + + /** + * Retrieve a list of ignored tables given by the configuration. + * + * The returned list is indexed by the database and contains the list of tables to be ignored on that database. + * If the configured list of tables are not namespaced by a dot the default database name will be used. + * + * @param GeneratorConfig $generatorConfig + * + * @return array + */ + public function getIgnoredTables(GeneratorConfig $generatorConfig) + { + $this->log(' Reading ignored tables...'); + + $tables = $generatorConfig->getBuildProperty('migrationIgnoreTables'); + if (empty($tables)) { + $this->log(' No tables to be ignored.'); + return array(); + } + + $tables = explode(',', $tables); + $this->log(sprintf(' %d table(s) to be ignored.', count($tables))); + + $ignored = array(); + foreach ($tables as $ignore) { + if (false !== strpos($ignore, '.')) { + list($database, $table) = explode('.', $ignore); + } else { + $database = $generatorConfig->getDefaultBuildConnectionName(); + $table = $ignore; + } + + if (empty($ignored[$database])) { + $ignored[$database] = array(); + } + + $ignored[$database][] = $table; + } + + return $ignored; + } }