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

[Feature] Define schema assets filter in configuration (continue) #101

Closed
wants to merge 3 commits into from
Closed
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
18 changes: 18 additions & 0 deletions example/full-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Doctrine\Common\Cache\XcacheCache;
use Doctrine\Common\Cache\ZendDataCache;
use Doctrine\DBAL\Driver\PDOMySql\Driver;
use Doctrine\DBAL\Schema\AbstractAsset;
use Doctrine\Migrations\Configuration\Migration\ConfigurationLoader;
use Doctrine\Migrations\DependencyFactory;
use Doctrine\Migrations\Tools\Console\Command;
Expand Down Expand Up @@ -63,6 +64,7 @@
'app.foo.middleware', // Will be looked up in the container.
'app.bar.middleware', // Will be looked up in the container.
],
'schema_assets_filter' => 'my_schema_assets_filter',
],
],
'connection' => [
Expand Down Expand Up @@ -193,6 +195,22 @@

DependencyFactory::class => DependencyFactoryFactory::class,
ConfigurationLoader::class => ConfigurationLoaderFactory::class,

'my_schema_assets_filter' => static function (): callable {
/**
* Filter out assets (table, sequence) by name from Schema
* because ones have no mapping and this cause unwanted create|drop statements in migration
* generated with migrations:diff command when compare ORM schema and schema introspected from database
*/
return static fn (AbstractAsset|string $asset): bool => ! in_array(
$asset instanceof AbstractAsset ? $asset->getName() : $asset,
[
'sequence_to_generate_value',
'table_without_doctrine_mapping',
],
true,
);
},
],
],
];
5 changes: 5 additions & 0 deletions src/ConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ protected function createWithConfig(ContainerInterface $container, string $confi
$configuration->setEntityListenerResolver($config['entity_listener_resolver']);
}

if (is_string($config['schema_assets_filter'])) {
$configuration->setSchemaAssetsFilter($container->get($config['schema_assets_filter']));
}

if ($config['default_repository_class_name'] !== null) {
$configuration->setDefaultRepositoryClassName($config['default_repository_class_name']);
}
Expand Down Expand Up @@ -215,6 +219,7 @@ protected function getDefaultConfig(string $configKey): array
'repository_factory' => null,
'class_metadata_factory_name' => null,
'entity_listener_resolver' => null,
'schema_assets_filter' => null,
'second_level_cache' => [
'enabled' => false,
'default_lifetime' => 3600,
Expand Down
84 changes: 84 additions & 0 deletions test/ConfigurationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,90 @@ static function (string $id): bool {
self::assertSame([$middlewareFoo, $middlewareBar], $configuration->getMiddlewares());
}

public function testWillSetSchemaAssetsFilterByContainerId(): void
{
$testFilter = static fn (): bool => true;
$config = [
'doctrine' => [
'configuration' => [
'orm_default' => ['schema_assets_filter' => 'testFilterContainerId'],
],
],
];

$container = $this->createStub(ContainerInterface::class);

$container
->method('has')
->willReturnCallback(
static fn (string $id) => in_array(
$id,
[
'config',
'doctrine.driver.orm_default',
'testFilterContainerId',
],
true,
),
);

$container
->method('get')
->willReturnMap(
[
['config', $config],
['doctrine.driver.orm_default', $this->createStub(MappingDriver::class)],
['testFilterContainerId', $testFilter],
],
);

$configuration = (new ConfigurationFactory())($container);

self::assertSame($testFilter, $configuration->getSchemaAssetsFilter());
}

public function testMistypeInSchemaAssetsFilterResolvedContainerId(): void
{
$testFilter = ['misconfig' => 'resolved service is not callable'];
$config = [
'doctrine' => [
'configuration' => [
'orm_default' => ['schema_assets_filter' => 'testFilterContainerId'],
],
],
];

$container = $this->createStub(ContainerInterface::class);

$container
->method('has')
->willReturnCallback(
static fn (string $id) => in_array(
$id,
[
'config',
'doctrine.driver.orm_default',
'testFilterContainerId',
],
true,
),
);

$container
->method('get')
->willReturnMap(
[
['config', $config],
['doctrine.driver.orm_default', $this->createStub(MappingDriver::class)],
['testFilterContainerId', $testFilter],
],
);

self::expectError();

(new ConfigurationFactory())($container);
}

/**
* @param non-empty-string $propertyName
*
Expand Down