Skip to content

Commit

Permalink
add propel.migration.ignoreTables
Browse files Browse the repository at this point in the history
  • Loading branch information
havvg committed Oct 24, 2012
1 parent 9d4763a commit 14c251e
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
1 change: 1 addition & 0 deletions generator/default.properties
Expand Up @@ -207,6 +207,7 @@ propel.sql.mapper.to = *.sql
propel.migration.editor =
propel.migration.table = propel_migration
propel.migration.caseInsensitive = true
propel.migration.ignoreTables =

# -------------------------------------------------------------------
#
Expand Down
5 changes: 5 additions & 0 deletions generator/lib/config/GeneratorConfig.php
Expand Up @@ -318,4 +318,9 @@ public function getBuildPDO($database)

return $pdo;
}

public function getDefaultBuildConnectionName()
{
return $this->defaultBuildConnection;
}
}
20 changes: 17 additions & 3 deletions generator/lib/model/diff/PropelDatabaseComparator.php
Expand Up @@ -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;
}
Expand All @@ -103,17 +104,22 @@ 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();
$databaseDifferences = 0;

// 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++;
Expand All @@ -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++;
Expand All @@ -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);
Expand Down
45 changes: 44 additions & 1 deletion generator/lib/task/PropelSQLDiffTask.php
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 14c251e

Please sign in to comment.