Skip to content

Commit

Permalink
Apply schema filter more selectively
Browse files Browse the repository at this point in the history
It is more careful, since there seems to be a lot of situations where we
do not want to apply the filter.
Let us take the opposite approach and:
- Have the filter disabled by default, instead of enabled by default.
- Enable it for precise commands where we know we need it.
  • Loading branch information
greg0ire committed Jan 21, 2025
1 parent 66c74dc commit 47cfdb3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
9 changes: 5 additions & 4 deletions src/EventListener/SchemaFilterListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
namespace Doctrine\Bundle\MigrationsBundle\EventListener;

use Doctrine\DBAL\Schema\AbstractAsset;
use Doctrine\Migrations\Tools\Console\Command\DoctrineCommand;
use Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand;
use Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand;
use Symfony\Component\Console\Event\ConsoleCommandEvent;

/**
Expand All @@ -24,7 +25,7 @@ public function __construct(string $configurationTableName)
}

/** @var bool */
private $enabled = true;
private $enabled = false;

/** @param AbstractAsset|string $asset */
public function __invoke($asset): bool
Expand All @@ -44,10 +45,10 @@ public function onConsoleCommand(ConsoleCommandEvent $event): void
{
$command = $event->getCommand();

if (! $command instanceof DoctrineCommand) {
if (! $command instanceof ValidateSchemaCommand && ! $command instanceof UpdateCommand) {
return;
}

$this->enabled = false;
$this->enabled = true;
}
}
41 changes: 36 additions & 5 deletions tests/Collector/EventListener/SchemaFilterListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,28 @@
use Doctrine\Bundle\MigrationsBundle\EventListener\SchemaFilterListener;
use Doctrine\DBAL\Schema\Table;
use Doctrine\Migrations\Tools\Console\Command\DoctrineCommand;
use Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand;
use Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand;
use Doctrine\ORM\Tools\Console\EntityManagerProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;

class SchemaFilterListenerTest extends TestCase
{
public function testItFiltersOutMigrationMetadataTableByDefault(): void
public function testItFiltersNothingByDefault(): void
{
$listener = new SchemaFilterListener('doctrine_migration_versions');

self::assertFalse($listener(new Table('doctrine_migration_versions')));
self::assertTrue($listener(new Table('doctrine_migration_versions')));
self::assertTrue($listener(new Table('some_other_table')));
}

public function testItDisablesItselfWhenTheCurrentCommandIsAMigrationsCommand(): void
public function testItFiltersNothingWhenNotRunningSpecificCommands(): void
{
$listener = new SchemaFilterListener('doctrine_migration_versions');
$migrationsCommand = new class extends DoctrineCommand {
$migrationsCommand = new class () extends DoctrineCommand {
};

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

self::assertTrue($listener(new Table('doctrine_migration_versions')));
self::assertTrue($listener(new Table('some_other_table')));
}

/**
* @param class-string<Command> $command
*
* @dataProvider getCommands
*/
public function testItFiltersOutMigrationMetadataTableWhenRunningSpecificCommands(string $command): void
{
$listener = new SchemaFilterListener('doctrine_migration_versions');
$ormCommand = new $command($this->createStub(EntityManagerProvider::class));

Check failure on line 52 in tests/Collector/EventListener/SchemaFilterListenerTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis / PHPStan (PHP: 8.4)

Parameter #1 $name of class Symfony\Component\Console\Command\Command constructor expects string|null, Doctrine\ORM\Tools\Console\EntityManagerProvider&PHPUnit\Framework\MockObject\Stub given.

$listener->onConsoleCommand(new ConsoleCommandEvent(
$ormCommand,
new ArrayInput([]),
new NullOutput()
));

self::assertFalse($listener(new Table('doctrine_migration_versions')));
self::assertTrue($listener(new Table('some_other_table')));
}

/** @return iterable<string, array{class-string<Command>}> */
public static function getCommands(): iterable
{
yield 'orm:validate-schema' => [ValidateSchemaCommand::class];
yield 'orm:schema-tool:update' => [UpdateCommand::class];
}
}

0 comments on commit 47cfdb3

Please sign in to comment.