Skip to content

Commit e858ce0

Browse files
authored
Merge pull request #587 from greg0ire/selective-filter
Apply schema filter more selectively
2 parents 9872551 + 31417c0 commit e858ce0

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

src/EventListener/SchemaFilterListener.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
namespace Doctrine\Bundle\MigrationsBundle\EventListener;
66

77
use Doctrine\DBAL\Schema\AbstractAsset;
8-
use Doctrine\Migrations\Tools\Console\Command\DoctrineCommand;
8+
use Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand;
9+
use Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand;
910
use Symfony\Component\Console\Event\ConsoleCommandEvent;
1011

1112
/**
@@ -24,7 +25,7 @@ public function __construct(string $configurationTableName)
2425
}
2526

2627
/** @var bool */
27-
private $enabled = true;
28+
private $enabled = false;
2829

2930
/** @param AbstractAsset|string $asset */
3031
public function __invoke($asset): bool
@@ -40,19 +41,14 @@ public function __invoke($asset): bool
4041
return $asset !== $this->configurationTableName;
4142
}
4243

43-
private function disable(): void
44-
{
45-
$this->enabled = false;
46-
}
47-
4844
public function onConsoleCommand(ConsoleCommandEvent $event): void
4945
{
5046
$command = $event->getCommand();
5147

52-
if (! $command instanceof DoctrineCommand) {
48+
if (! $command instanceof ValidateSchemaCommand && ! $command instanceof UpdateCommand) {
5349
return;
5450
}
5551

56-
$this->disable();
52+
$this->enabled = true;
5753
}
5854
}

tests/Collector/EventListener/SchemaFilterListenerTest.php

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,28 @@
77
use Doctrine\Bundle\MigrationsBundle\EventListener\SchemaFilterListener;
88
use Doctrine\DBAL\Schema\Table;
99
use Doctrine\Migrations\Tools\Console\Command\DoctrineCommand;
10+
use Doctrine\ORM\Tools\Console\Command\AbstractEntityManagerCommand;
11+
use Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand;
12+
use Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand;
13+
use Doctrine\ORM\Tools\Console\EntityManagerProvider;
1014
use PHPUnit\Framework\TestCase;
1115
use Symfony\Component\Console\Event\ConsoleCommandEvent;
1216
use Symfony\Component\Console\Input\ArrayInput;
1317
use Symfony\Component\Console\Output\NullOutput;
1418

1519
class SchemaFilterListenerTest extends TestCase
1620
{
17-
public function testItFiltersOutMigrationMetadataTableByDefault(): void
21+
public function testItFiltersNothingByDefault(): void
1822
{
1923
$listener = new SchemaFilterListener('doctrine_migration_versions');
20-
21-
self::assertFalse($listener(new Table('doctrine_migration_versions')));
24+
self::assertTrue($listener(new Table('doctrine_migration_versions')));
2225
self::assertTrue($listener(new Table('some_other_table')));
2326
}
2427

25-
public function testItDisablesItselfWhenTheCurrentCommandIsAMigrationsCommand(): void
28+
public function testItFiltersNothingWhenNotRunningSpecificCommands(): void
2629
{
2730
$listener = new SchemaFilterListener('doctrine_migration_versions');
28-
$migrationsCommand = new class extends DoctrineCommand {
31+
$migrationsCommand = new class () extends DoctrineCommand {
2932
};
3033

3134
$listener->onConsoleCommand(new ConsoleCommandEvent(
@@ -35,5 +38,33 @@ public function testItDisablesItselfWhenTheCurrentCommandIsAMigrationsCommand():
3538
));
3639

3740
self::assertTrue($listener(new Table('doctrine_migration_versions')));
41+
self::assertTrue($listener(new Table('some_other_table')));
42+
}
43+
44+
/**
45+
* @param class-string<AbstractEntityManagerCommand> $command
46+
*
47+
* @dataProvider getCommands
48+
*/
49+
public function testItFiltersOutMigrationMetadataTableWhenRunningSpecificCommands(string $command): void
50+
{
51+
$listener = new SchemaFilterListener('doctrine_migration_versions');
52+
$ormCommand = new $command($this->createStub(EntityManagerProvider::class));
53+
54+
$listener->onConsoleCommand(new ConsoleCommandEvent(
55+
$ormCommand,
56+
new ArrayInput([]),
57+
new NullOutput()
58+
));
59+
60+
self::assertFalse($listener(new Table('doctrine_migration_versions')));
61+
self::assertTrue($listener(new Table('some_other_table')));
62+
}
63+
64+
/** @return iterable<string, array{class-string<AbstractEntityManagerCommand>}> */
65+
public static function getCommands(): iterable
66+
{
67+
yield 'orm:validate-schema' => [ValidateSchemaCommand::class];
68+
yield 'orm:schema-tool:update' => [UpdateCommand::class];
3869
}
3970
}

0 commit comments

Comments
 (0)