Skip to content

Commit

Permalink
Apply assets filter on sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
ottaviano committed Jun 1, 2024
1 parent 7780f8d commit 4fdbc3b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/Generator/DiffGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ private function createToSchema(): Schema

$toSchema->dropTable($tableName);
}

foreach ($toSchema->getSequences() as $sequence) {
$sequenceName = $sequence->getName();

if ($schemaAssetsFilter($sequenceName)) {
continue;
}

$toSchema->dropSequence($sequenceName);
}
}

return $toSchema;
Expand Down
23 changes: 22 additions & 1 deletion tests/Generator/DiffGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\SchemaDiff;
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Table;
use Doctrine\Migrations\Generator\DiffGenerator;
use Doctrine\Migrations\Generator\Generator;
Expand All @@ -18,6 +19,8 @@
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

use function in_array;

class DiffGeneratorTest extends TestCase
{
private DBALConfiguration&MockObject $dbalConfiguration;
Expand All @@ -43,7 +46,7 @@ public function testGenerate(): void
$this->dbalConfiguration->expects(self::once())
->method('getSchemaAssetsFilter')
->willReturn(
static fn ($name): bool => $name === 'table_name1',
static fn ($name): bool => in_array($name, ['table_name1', 'table_name2_id_seq'], true),
);

$table1 = $this->createMock(Table::class);
Expand All @@ -61,10 +64,24 @@ public function testGenerate(): void
->method('getName')
->willReturn('schema.table_name3');

$sequence1 = $this->createMock(Sequence::class);
$sequence1->expects(self::once())
->method('getName')
->willReturn('table_name1_id_seq');

$sequence2 = $this->createMock(Sequence::class);
$sequence2->expects(self::once())
->method('getName')
->willReturn('table_name2_id_seq');

$toSchema->expects(self::once())
->method('getTables')
->willReturn([$table1, $table2, $table3]);

$toSchema->expects(self::once())
->method('getSequences')
->willReturn([$sequence1, $sequence2]);

$this->emptySchemaProvider->expects(self::never())
->method('createSchema');

Expand All @@ -80,6 +97,10 @@ public function testGenerate(): void
->method('dropTable')
->willReturnSelf();

$toSchema->expects(self::once())
->method('dropSequence')
->with('table_name1_id_seq');

$schemaDiff = self::createStub(SchemaDiff::class);

$this->platform->method('getAlterSchemaSQL')->willReturnCallback(static function (): array {
Expand Down

0 comments on commit 4fdbc3b

Please sign in to comment.