Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/3.4.x' into 4.0.x
Browse files Browse the repository at this point in the history
  • Loading branch information
greg0ire committed Jan 16, 2025
2 parents ac75cd4 + a5c5fe0 commit 483d305
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 13 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/composer-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: "Composer Lint"

on:
pull_request:
branches:
- "*.x"
paths:
- ".github/workflows/composer-lint.yml"
- "composer.json"
push:
branches:
- "*.x"
paths:
- ".github/workflows/composer-lint.yml"
- "composer.json"

jobs:
composer-lint:
name: "Composer Lint"
uses: "doctrine/.github/.github/workflows/[email protected]"
30 changes: 19 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
{
"name": "doctrine/doctrine-migrations-bundle",
"type": "symfony-bundle",
"description": "Symfony DoctrineMigrationsBundle",
"keywords": ["DBAL", "Migrations", "Schema"],
"homepage": "https://www.doctrine-project.org",
"license": "MIT",
"type": "symfony-bundle",
"keywords": [
"DBAL",
"Migrations",
"Schema"
],
"authors": [
{
"name": "Fabien Potencier",
Expand All @@ -19,31 +22,36 @@
"homepage": "https://symfony.com/contributors"
}
],
"homepage": "https://www.doctrine-project.org",
"require": {
"php": "^8.1",
"symfony/deprecation-contracts": "^2.1 || ^3",
"symfony/framework-bundle": "^6.4 || ^7.0",
"doctrine/doctrine-bundle": "^2.8",
"doctrine/migrations": "^3.2"
"doctrine/migrations": "^3.2",
"symfony/deprecation-contracts": "^2.1 || ^3",
"symfony/framework-bundle": "^6.4 || ^7.0"
},
"require-dev": {
"composer/semver": "^3.0",
"phpunit/phpunit": "^10.5.40 || ^11.5",
"doctrine/coding-standard": "^12",
"doctrine/orm": "^2.15 || ^3",
"doctrine/persistence": "^3.1 ",
"phpstan/phpstan": "^2",
"phpstan/phpstan-deprecation-rules": "^2",
"phpstan/phpstan-phpunit": "^2",
"phpstan/phpstan-strict-rules": "^2",
"phpstan/phpstan-symfony": "^2",
"doctrine/orm": "^2.15 || ^3",
"doctrine/persistence": "^3.1 ",
"phpunit/phpunit": "^10.5.40 || ^11.5",
"symfony/var-exporter": "^6.4 || ^7"
},
"autoload": {
"psr-4": { "Doctrine\\Bundle\\MigrationsBundle\\": "src" }
"psr-4": {
"Doctrine\\Bundle\\MigrationsBundle\\": "src"
}
},
"autoload-dev": {
"psr-4": { "Doctrine\\Bundle\\MigrationsBundle\\Tests\\": "tests" }
"psr-4": {
"Doctrine\\Bundle\\MigrationsBundle\\Tests\\": "tests"
}
},
"config": {
"allow-plugins": {
Expand Down
5 changes: 5 additions & 0 deletions config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,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 @@ -88,15 +88,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');
} 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 {
$storageDefinition->addMethodCall('setTableName', [$storageConfiguration['table_name']]);
$filterDefinition->addArgument($storageConfiguration['table_name']);
}

if ($storageConfiguration['version_column_name'] !== null) {
Expand Down
52 changes: 52 additions & 0 deletions src/EventListener/SchemaFilterListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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
{
public function __construct(private string $configurationTableName)
{
}

private bool $enabled = true;

public function __invoke(AbstractAsset|string $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;
}

$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')));
}
}

0 comments on commit 483d305

Please sign in to comment.