forked from doctrine/DoctrineMigrationsBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/3.4.x' into 4.0.x
- Loading branch information
Showing
6 changed files
with
143 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
39
tests/Collector/EventListener/SchemaFilterListenerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'))); | ||
} | ||
} |