-
-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When using orm:schema-tool:update together with --complete, all assets not described by the current metadata is dropped. This is an issue when using doctrine/migrations, because not using that option is deprecated, and because when it is used, the table that holds the migrations is dropped as well since it is not described by ORM metadata. A solution to that is configuring an asset filter that filters out the metadata table except when running commands inside the migrations namespace.
- Loading branch information
Showing
4 changed files
with
110 additions
and
2 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
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,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; | ||
} | ||
|
||
$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'))); | ||
} | ||
} |