From 4fdbc3b6954ed77f2fdfbbc30aa9a855cfca94db Mon Sep 17 00:00:00 2001 From: Dimitri Gritsajuk Date: Sat, 25 Feb 2023 14:48:37 +0100 Subject: [PATCH] Apply assets filter on sequences --- src/Generator/DiffGenerator.php | 10 ++++++++++ tests/Generator/DiffGeneratorTest.php | 23 ++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/Generator/DiffGenerator.php b/src/Generator/DiffGenerator.php index 19b6f3da8..140bef490 100644 --- a/src/Generator/DiffGenerator.php +++ b/src/Generator/DiffGenerator.php @@ -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; diff --git a/tests/Generator/DiffGeneratorTest.php b/tests/Generator/DiffGeneratorTest.php index 48ad71db5..8d7fd7810 100644 --- a/tests/Generator/DiffGeneratorTest.php +++ b/tests/Generator/DiffGeneratorTest.php @@ -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; @@ -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; @@ -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); @@ -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'); @@ -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 {