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 schema filter more selectively #587

Open
wants to merge 2 commits into
base: 3.4.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
14 changes: 5 additions & 9 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 @@ -40,19 +41,14 @@ public function __invoke($asset): bool
return $asset !== $this->configurationTableName;
}

private function disable(): void
{
$this->enabled = false;
}

public function onConsoleCommand(ConsoleCommandEvent $event): void
{
$command = $event->getCommand();

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

$this->disable();
$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\AbstractEntityManagerCommand;
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\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<AbstractEntityManagerCommand> $command
*
* @dataProvider getCommands
*/
public function testItFiltersOutMigrationMetadataTableWhenRunningSpecificCommands(string $command): void
{
$listener = new SchemaFilterListener('doctrine_migration_versions');
$ormCommand = new $command($this->createStub(EntityManagerProvider::class));

$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<AbstractEntityManagerCommand>}> */
public static function getCommands(): iterable
{
yield 'orm:validate-schema' => [ValidateSchemaCommand::class];
yield 'orm:schema-tool:update' => [UpdateCommand::class];
}
}