-
-
Notifications
You must be signed in to change notification settings - Fork 455
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1037 from nicolas-grekas/auto-ignore-sf-tables
Filter out well-known Symfony tables from DBAL schema migrations
- Loading branch information
Showing
9 changed files
with
296 additions
and
0 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,29 @@ | ||
<?php | ||
|
||
namespace Doctrine\Bundle\DoctrineBundle\Dbal; | ||
|
||
use Doctrine\DBAL\Schema\AbstractAsset; | ||
use function in_array; | ||
|
||
class BlacklistSchemaAssetFilter | ||
{ | ||
/** @var string[] */ | ||
private $blacklist; | ||
|
||
/** | ||
* @param string[] $blacklist | ||
*/ | ||
public function __construct(array $blacklist) | ||
{ | ||
$this->blacklist = $blacklist; | ||
} | ||
|
||
public function __invoke($assetName) : bool | ||
{ | ||
if ($assetName instanceof AbstractAsset) { | ||
$assetName = $assetName->getName(); | ||
} | ||
|
||
return ! in_array($assetName, $this->blacklist, true); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
DependencyInjection/Compiler/WellKnownSchemaFilterPass.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,65 @@ | ||
<?php | ||
|
||
namespace Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler; | ||
|
||
use Doctrine\DBAL\Configuration; | ||
use Symfony\Component\Cache\Adapter\PdoAdapter; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler; | ||
use Symfony\Component\Lock\Store\PdoStore; | ||
use Symfony\Component\Messenger\Transport\Doctrine\Connection; | ||
|
||
/** | ||
* Blacklist tables used by well-known Symfony classes. | ||
*/ | ||
class WellKnownSchemaFilterPass implements CompilerPassInterface | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if (! method_exists(Configuration::class, 'setSchemaAssetsFilter')) { | ||
// only supported when using doctrine/dbal 2.9 or higher | ||
return; | ||
} | ||
|
||
$blacklist = []; | ||
|
||
foreach ($container->getDefinitions() as $definition) { | ||
if ($definition->isAbstract() || $definition->isSynthetic()) { | ||
continue; | ||
} | ||
|
||
switch ($definition->getClass()) { | ||
case PdoAdapter::class: | ||
$blacklist[] = $definition->getArguments()[3]['db_table'] ?? 'cache_items'; | ||
break; | ||
|
||
case PdoSessionHandler::class: | ||
$blacklist[] = $definition->getArguments()[1]['db_table'] ?? 'lock_keys'; | ||
break; | ||
|
||
case PdoStore::class: | ||
$blacklist[] = $definition->getArguments()[1]['db_table'] ?? 'sessions'; | ||
break; | ||
|
||
case Connection::class: | ||
$blacklist[] = $definition->getArguments()[0]['table_name'] ?? 'messenger_messages'; | ||
break; | ||
} | ||
} | ||
|
||
if (! $blacklist) { | ||
return; | ||
} | ||
|
||
$definition = $container->getDefinition('doctrine.dbal.well_known_schema_asset_filter'); | ||
$definition->replaceArgument(0, $blacklist); | ||
|
||
foreach (array_keys($container->getParameter('doctrine.connections')) as $name) { | ||
$definition->addTag('doctrine.dbal.schema_filter', ['connection' => $name]); | ||
} | ||
} | ||
} |
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
24 changes: 24 additions & 0 deletions
24
Tests/DependencyInjection/Fixtures/config/xml/well_known_schema_filter_default_tables.xml
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,24 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<srv:container xmlns="http://symfony.com/schema/dic/doctrine" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:srv="http://symfony.com/schema/dic/services" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd | ||
http://symfony.com/schema/dic/doctrine http://symfony.com/schema/dic/doctrine/doctrine-1.0.xsd"> | ||
|
||
<config> | ||
<dbal default-connection="connection1"> | ||
<connection name="connection1" schema-filter="~^(?!t_)~" /> | ||
<connection name="connection2" /> | ||
<connection name="connection3" /> | ||
</dbal> | ||
</config> | ||
|
||
<srv:services> | ||
<srv:service id="well_known_filter" alias="doctrine.dbal.well_known_schema_asset_filter" public="true" /> | ||
<srv:service id="symfony.cache" class="Symfony\Component\Cache\Adapter\PdoAdapter" /> | ||
<srv:service id="symfony.session" class="Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler" /> | ||
<srv:service id="symfony.lock" class="Symfony\Component\Lock\Store\PdoStore" /> | ||
<srv:service id="symfony.messenger" class="Symfony\Component\Messenger\Transport\Doctrine\Connection" /> | ||
</srv:services> | ||
</srv:container> |
45 changes: 45 additions & 0 deletions
45
Tests/DependencyInjection/Fixtures/config/xml/well_known_schema_filter_overridden_tables.xml
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,45 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<srv:container xmlns="http://symfony.com/schema/dic/doctrine" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:srv="http://symfony.com/schema/dic/services" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd | ||
http://symfony.com/schema/dic/doctrine http://symfony.com/schema/dic/doctrine/doctrine-1.0.xsd"> | ||
|
||
<config> | ||
<dbal default-connection="connection1"> | ||
<connection name="connection1" schema-filter="~^(?!t_)~" /> | ||
<connection name="connection2" /> | ||
<connection name="connection3" /> | ||
</dbal> | ||
</config> | ||
|
||
<srv:services> | ||
<srv:service id="well_known_filter" alias="doctrine.dbal.well_known_schema_asset_filter" public="true" /> | ||
<srv:service id="symfony.cache" class="Symfony\Component\Cache\Adapter\PdoAdapter"> | ||
<srv:argument /> | ||
<srv:argument /> | ||
<srv:argument /> | ||
<srv:argument type="collection"> | ||
<srv:argument key="db_table">app_cache</srv:argument> | ||
</srv:argument> | ||
</srv:service> | ||
<srv:service id="symfony.session" class="Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler"> | ||
<srv:argument /> | ||
<srv:argument type="collection"> | ||
<srv:argument key="db_table">app_session</srv:argument> | ||
</srv:argument> | ||
</srv:service> | ||
<srv:service id="symfony.lock" class="Symfony\Component\Lock\Store\PdoStore"> | ||
<srv:argument /> | ||
<srv:argument type="collection"> | ||
<srv:argument key="db_table">app_locks</srv:argument> | ||
</srv:argument> | ||
</srv:service> | ||
<srv:service id="symfony.messenger" class="Symfony\Component\Messenger\Transport\Doctrine\Connection"> | ||
<srv:argument type="collection"> | ||
<srv:argument key="table_name">app_messages</srv:argument> | ||
</srv:argument> | ||
</srv:service> | ||
</srv:services> | ||
</srv:container> |
25 changes: 25 additions & 0 deletions
25
Tests/DependencyInjection/Fixtures/config/yml/well_known_schema_filter_default_tables.yml
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,25 @@ | ||
doctrine: | ||
dbal: | ||
default_connection: connection1 | ||
connections: | ||
connection1: | ||
schema_filter: ~^(?!t_)~ | ||
connection2: [] | ||
connection3: [] | ||
|
||
services: | ||
well_known_filter: | ||
alias: 'doctrine.dbal.well_known_schema_asset_filter' | ||
public: true | ||
|
||
symfony.cache: | ||
class: 'Symfony\Component\Cache\Adapter\PdoAdapter' | ||
|
||
symfony.session: | ||
class: 'Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler' | ||
|
||
symfony.lock: | ||
class: 'Symfony\Component\Lock\Store\PdoStore' | ||
|
||
symfony.messenger: | ||
class: 'Symfony\Component\Messenger\Transport\Doctrine\Connection' |
38 changes: 38 additions & 0 deletions
38
Tests/DependencyInjection/Fixtures/config/yml/well_known_schema_filter_overridden_tables.yml
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,38 @@ | ||
doctrine: | ||
dbal: | ||
default_connection: connection1 | ||
connections: | ||
connection1: | ||
schema_filter: ~^(?!t_)~ | ||
connection2: [] | ||
connection3: [] | ||
|
||
services: | ||
well_known_filter: | ||
alias: 'doctrine.dbal.well_known_schema_asset_filter' | ||
public: true | ||
|
||
symfony.cache: | ||
class: 'Symfony\Component\Cache\Adapter\PdoAdapter' | ||
arguments: | ||
- ~ | ||
- ~ | ||
- ~ | ||
- db_table: app_cache | ||
|
||
symfony.session: | ||
class: 'Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler' | ||
arguments: | ||
- ~ | ||
- db_table: app_session | ||
|
||
symfony.lock: | ||
class: 'Symfony\Component\Lock\Store\PdoStore' | ||
arguments: | ||
- ~ | ||
- db_table: app_locks | ||
|
||
symfony.messenger: | ||
class: 'Symfony\Component\Messenger\Transport\Doctrine\Connection' | ||
arguments: | ||
- table_name: app_messages |