Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply assets filter on sequences #1328

Open
wants to merge 1 commit into
base: 3.8.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading