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

Add support for --complete #526

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 51 additions & 0 deletions EventListener/SchemaFilterListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Doctrine\Bundle\MigrationsBundle\EventListener;

use Doctrine\DBAL\Schema\AbstractAsset;
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration;
use Doctrine\Migrations\Tools\Console\Command\DoctrineCommand;
use Symfony\Component\Console\Event\ConsoleCommandEvent;

/**
* Acts as a schema filter able that hides the migration metadata table except
* when the execution context is that of command inside the migrations
* namespace.
*/
final class SchemaFilterListener
{
/** @var bool */
private $enabled = true;

/** @param AbstractAsset|string $asset */
public function __invoke($asset): bool
{
if (! $this->enabled) {
return true;
}

if ($asset instanceof AbstractAsset) {
$asset = $asset->getName();
}

return $asset !== (new TableMetadataStorageConfiguration())->getTableName();
}

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

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

if (! $command instanceof DoctrineCommand) {
return;

Check warning on line 46 in EventListener/SchemaFilterListener.php

View check run for this annotation

Codecov / codecov/patch

EventListener/SchemaFilterListener.php#L46

Added line #L46 was not covered by tests
}

$this->disable();
}
}
5 changes: 5 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@
<tag name="console.command" command="doctrine:migrations:version" />
</service>

<service id="doctrine_migrations.schema_filter_listener" class="Doctrine\Migrations\EventListener\SchemaFilterListener">
<tag name="kernel.event_listener" event="console.command" method="onConsoleCommand" />
<tag name="doctrine.dbal.schema_filter" />
</service>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the part I am not sure about, because I have been enable to test it. I've tried accessing the schema filter manager definition in order to inspect it after the compiler pass, like this:

    public function testItRegistersTheSchemaFilterListener(): void
    {
        $container = $this->getContainer([]);

        $container->getDefinition('doctrine.dbal.schema_asset_filter_manager')->setPublic(true);
        $container->compile();

        $container->get('doctrine.dbal.schema_asset_filter_manager');
    }

That last line results in

Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: The "doctrine.dbal.schema_asset_filter_manager" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.

Any ideas how to test this?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doctrine.dbal.schema_asset_filter_manager is defined as an abstract. The container builder removes unused and/or abstract definitions during its compilation causing the service to be hidden.

An example fix, not per se the wanted fix, could be:

$container
  ->getDefinition('doctrine.dbal.schema_asset_filter_manager')
  ->setAbstract(false)
  ->setArgument(0, [])
  ->setPublic(true);


</services>

</container>
48 changes: 48 additions & 0 deletions Tests/Collector/EventListener/SchemaFilterListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Doctrine\Bundle\MigrationsBundle\Tests\Collector\EventListener;

use Doctrine\Bundle\MigrationsBundle\EventListener\SchemaFilterListener;
use Doctrine\DBAL\Schema\Table;
use Doctrine\Migrations\Tools\Console\Command\DoctrineCommand;
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
{
$listener = new SchemaFilterListener();

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

public function testItFiltersNothingWhenDisabled(): void
{
$listener = new SchemaFilterListener();
$listener->disable();

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

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

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

self::assertTrue($listener(new Table('doctrine_migration_versions')));
}
}
1 change: 1 addition & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ parameters:
phpVersion: 80200
paths:
- DependencyInjection
- EventListener
- Tests

excludePaths:
Expand Down
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
>
<projectFiles>
<directory name="DependencyInjection" />
<directory name="EventListener" />
<directory name="Tests" />
<file name="DoctrineMigrationsBundle.php" />
<ignoreFiles>
Expand Down