Skip to content

Commit

Permalink
Merge pull request #1903 from felixgeyer/bugfix/duplicate_indices_whe…
Browse files Browse the repository at this point in the history
…n_converting_unique_to_index

Fix duplicate indices when converting unique index to index
  • Loading branch information
dereuromark authored Aug 29, 2022
2 parents e6b0e0e + d62faff commit f9b9e12
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,18 @@ protected function addArchiveTable(): void
// see https://github.com/propelorm/Propel/issues/175 for details
foreach ($table->getUnices() as $unique) {
$index = new Index();
$index->setTable($table);
$index->setTable($archiveTable);
foreach ($unique->getColumns() as $columnName) {
if ($size = $unique->getColumnSize($columnName)) {
$index->addColumn(['name' => $columnName, 'size' => $size]);
} else {
$index->addColumn(['name' => $columnName]);
}
}
$archiveTable->addIndex($index);

if (!$archiveTable->hasIndex($index->getName())) {
$archiveTable->addIndex($index);
}
}
// every behavior adding a table should re-execute database behaviors
foreach ($database->getBehaviors() as $behavior) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Map\MyOldArchivableTest3TableMap;
use Propel\Generator\Util\QuickBuilder;
use Propel\Tests\TestCase;
use function substr_count;

/**
* Tests for ArchivableBehavior class
Expand Down Expand Up @@ -83,6 +84,9 @@ public function setUp(): void
<unique>
<unique-column name="title"/>
</unique>
<index>
<index-column name="title"/>
</index>
<behavior name="archivable">
<parameter name="log_archived_at" value="false"/>
<parameter name="archive_table" value="my_old_archivable_test_3"/>
Expand Down Expand Up @@ -208,6 +212,15 @@ public function testCopiesUniquesToIndices()
$this->assertStringContainsString($expected, self::$generatedSQL);
}

/**
* @return void
*/
public function testCopiedUniqueDoesNotDuplicateCopiedIndex()
{
$expectedSqlMigration = 'CREATE INDEX my_old_archivable_test_3_i_639136 ON my_old_archivable_test_3 (title);';
$this->assertSame(1, substr_count(self::$generatedSQL, $expectedSqlMigration));
}

/**
* @return void
*/
Expand Down

0 comments on commit f9b9e12

Please sign in to comment.