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

Merged
merged 1 commit into from
Jan 16, 2025
Merged
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
5 changes: 5 additions & 0 deletions 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\Bundle\MigrationsBundle\EventListener\SchemaFilterListener">
<tag name="kernel.event_listener" event="console.command" method="onConsoleCommand" />
<tag name="doctrine.dbal.schema_filter" />
</service>

</services>

</container>
10 changes: 8 additions & 2 deletions src/DependencyInjection/DoctrineMigrationsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,21 @@ public function load(array $configs, ContainerBuilder $container): void
$diDefinition->addMethodCall('setDefinition', [$doctrineId, new Reference($symfonyId)]);
}

if (! isset($config['services'][MetadataStorage::class])) {
if (isset($config['services'][MetadataStorage::class])) {
$container->removeDefinition('doctrine_migrations.schema_filter_listener');
Copy link
Member Author

Choose a reason for hiding this comment

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

When a user defines their own storage class, there is no telling what they are going to do… they might even not store it in a database. At this point they're one their own.

} else {
$filterDefinition = $container->getDefinition('doctrine_migrations.schema_filter_listener');
$storageConfiguration = $config['storage']['table_storage'];

$storageDefinition = new Definition(TableMetadataStorageConfiguration::class);
$container->setDefinition('doctrine.migrations.storage.table_storage', $storageDefinition);
$container->setAlias('doctrine.migrations.metadata_storage', 'doctrine.migrations.storage.table_storage');

if ($storageConfiguration['table_name'] !== null) {
if ($storageConfiguration['table_name'] === null) {
$filterDefinition->addArgument('doctrine_migration_versions');
} else {
Copy link

Choose a reason for hiding this comment

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

After implementing these changes, running the migration results in the following exception:
SQLSTATE[42P07]: Duplicate table: 7 ERROR: relation "doctrine_migration_versions" already exists.

$storageDefinition->addMethodCall('setTableName', [$storageConfiguration['table_name']]);
$filterDefinition->addArgument($storageConfiguration['table_name']);
}

if ($storageConfiguration['version_column_name'] !== null) {
Expand Down
58 changes: 58 additions & 0 deletions src/EventListener/SchemaFilterListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Doctrine\Bundle\MigrationsBundle\EventListener;

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

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

public function __construct(string $configurationTableName)
{
$this->configurationTableName = $configurationTableName;
}

/** @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 !== $this->configurationTableName;
}

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

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

if (! $command instanceof DoctrineCommand) {
return;

Check warning on line 53 in src/EventListener/SchemaFilterListener.php

View check run for this annotation

Codecov / codecov/patch

src/EventListener/SchemaFilterListener.php#L53

Added line #L53 was not covered by tests
}

$this->disable();
}
}
39 changes: 39 additions & 0 deletions tests/Collector/EventListener/SchemaFilterListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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('doctrine_migration_versions');

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

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

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

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