Skip to content

Commit

Permalink
Merge pull request #117 from patchlevel/doctrine-schema-injection
Browse files Browse the repository at this point in the history
merge with doctrine orm schema
  • Loading branch information
DavidBadura authored Dec 17, 2022
2 parents 4c11ed7 + b570b61 commit 27addd6
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 41 deletions.
24 changes: 22 additions & 2 deletions docs/pages/store.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ patchlevel_event_sourcing:

!!! warning

You should avoid that this connection or database is used by other tools or libraries.
Create for e.g. doctrine orm its own database and connection.
If you want to use the same connection as doctrine orm, then you have to set the flag `merge_orm_schema`.
Otherwise you should avoid using the same connection as other tools.

!!! note

Expand Down Expand Up @@ -117,3 +117,23 @@ patchlevel_event_sourcing:
namespace: EventSourcingMigrations
path: "%kernel.project_dir%/migrations"
```


## Merge ORM Schema

You can also merge the schema with doctrine orm. You have to set the following flag for this:

```yaml
patchlevel_event_sourcing:
store:
merge_orm_schema: true
```

!!! note

All schema relevant commands are removed if you activate this option. You should use the doctrine commands then.

!!! warning

If you want to merge the schema, then the same doctrine connection must be used as with the doctrine orm.
Otherwise errors may occur!
1 change: 1 addition & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->defaultNull()
->setDeprecated('patchlevel/event-sourcing-bundle', '2.1')
->end()
->booleanNode('merge_orm_schema')->defaultFalse()->end()
->end()
->end()

Expand Down
87 changes: 48 additions & 39 deletions src/DependencyInjection/PatchlevelEventSourcingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
use Patchlevel\EventSourcing\Schema\DoctrineSchemaDirector;
use Patchlevel\EventSourcing\Schema\DoctrineSchemaManager;
use Patchlevel\EventSourcing\Schema\DoctrineSchemaProvider;
use Patchlevel\EventSourcing\Schema\DoctrineSchemaSubscriber;
use Patchlevel\EventSourcing\Schema\SchemaConfigurator;
use Patchlevel\EventSourcing\Schema\SchemaDirector;
use Patchlevel\EventSourcing\Schema\SchemaManager;
Expand Down Expand Up @@ -113,7 +114,7 @@
* projection: array{projectionist: bool},
* watch_server: array{enabled: bool, host: string},
* connection: ?array{service: ?string, url: ?string},
* store: array{schema_manager: string, type: string, options: array<string, mixed>},
* store: array{schema_manager: string, type: string, merge_orm_schema: bool, options: array<string, mixed>},
* aggregates: list<string>,
* events: list<string>,
* snapshot_stores: array<string, array{type: string, service: string}>,
Expand Down Expand Up @@ -161,7 +162,7 @@ public function load(array $configs, ContainerBuilder $container): void
$this->configureProjectionListener($container);
}

if (class_exists(DependencyFactory::class)) {
if (class_exists(DependencyFactory::class) && $config['store']['merge_orm_schema'] === false) {
$this->configureMigration($config, $container);
}

Expand Down Expand Up @@ -492,43 +493,6 @@ private function configureAggregates(array $config, ContainerBuilder $container)

private function configureCommands(ContainerBuilder $container): void
{
$container->register(DoctrineHelper::class);

$container->register(DatabaseCreateCommand::class)
->setArguments([
new Reference(Store::class),
new Reference(DoctrineHelper::class),
])
->addTag('console.command');

$container->register(DatabaseDropCommand::class)
->setArguments([
new Reference(Store::class),
new Reference(DoctrineHelper::class),
])
->addTag('console.command');

$container->register(SchemaCreateCommand::class)
->setArguments([
new Reference(Store::class),
new Reference(SchemaDirector::class),
])
->addTag('console.command');

$container->register(SchemaUpdateCommand::class)
->setArguments([
new Reference(Store::class),
new Reference(SchemaDirector::class),
])
->addTag('console.command');

$container->register(SchemaDropCommand::class)
->setArguments([
new Reference(Store::class),
new Reference(SchemaDirector::class),
])
->addTag('console.command');

$container->register(ShowCommand::class)
->setArguments([
new Reference(Store::class),
Expand Down Expand Up @@ -679,6 +643,14 @@ private function configureSchema(array $config, ContainerBuilder $container): vo

$container->setAlias(SchemaConfigurator::class, ChainSchemaConfigurator::class);

if ($config['store']['merge_orm_schema']) {
$container->register(DoctrineSchemaSubscriber::class)
->setArguments([new Reference(SchemaConfigurator::class)])
->addTag('doctrine.event_subscriber');

return;
}

$container->register(DoctrineSchemaDirector::class)
->setArguments([
new Reference('event_sourcing.dbal_connection'),
Expand All @@ -695,5 +667,42 @@ private function configureSchema(array $config, ContainerBuilder $container): vo
$container->register(DoctrineSchemaManager::class);
$container->setAlias(SchemaManager::class, DoctrineSchemaManager::class);
}

$container->register(DoctrineHelper::class);

$container->register(DatabaseCreateCommand::class)
->setArguments([
new Reference(Store::class),
new Reference(DoctrineHelper::class),
])
->addTag('console.command');

$container->register(DatabaseDropCommand::class)
->setArguments([
new Reference(Store::class),
new Reference(DoctrineHelper::class),
])
->addTag('console.command');

$container->register(SchemaCreateCommand::class)
->setArguments([
new Reference(Store::class),
new Reference(SchemaDirector::class),
])
->addTag('console.command');

$container->register(SchemaUpdateCommand::class)
->setArguments([
new Reference(Store::class),
new Reference(SchemaDirector::class),
])
->addTag('console.command');

$container->register(SchemaDropCommand::class)
->setArguments([
new Reference(Store::class),
new Reference(SchemaDirector::class),
])
->addTag('console.command');
}
}
28 changes: 28 additions & 0 deletions tests/Unit/PatchlevelEventSourcingBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
use Patchlevel\EventSourcing\Repository\DefaultRepository;
use Patchlevel\EventSourcing\Repository\DefaultRepositoryManager;
use Patchlevel\EventSourcing\Repository\RepositoryManager;
use Patchlevel\EventSourcing\Schema\DoctrineSchemaProvider;
use Patchlevel\EventSourcing\Schema\DoctrineSchemaSubscriber;
use Patchlevel\EventSourcing\Schema\SchemaDirector;
use Patchlevel\EventSourcing\Schema\SchemaManager;
use Patchlevel\EventSourcing\Snapshot\Adapter\Psr16SnapshotAdapter;
use Patchlevel\EventSourcing\Snapshot\Adapter\Psr6SnapshotAdapter;
Expand Down Expand Up @@ -689,6 +692,31 @@ public function testProjectionist()
self::assertFalse($container->has(ProjectionListener::class));
}

public function testSchemaMerge()
{
$container = new ContainerBuilder();

$this->compileContainer(
$container,
[
'patchlevel_event_sourcing' => [
'connection' => [
'service' => 'doctrine.dbal.eventstore_connection',
],
'store' => [
'merge_orm_schema' => true,
],
],
]
);

self::assertInstanceOf(DoctrineSchemaSubscriber::class, $container->get(DoctrineSchemaSubscriber::class));
self::assertFalse($container->has(SchemaDirector::class));
self::assertFalse($container->has(DoctrineSchemaProvider::class));
self::assertFalse($container->has(DatabaseCreateCommand::class));
self::assertFalse($container->has('event_sourcing.command.migration_diff'));
}

public function testFullBuild()
{
$container = new ContainerBuilder();
Expand Down

0 comments on commit 27addd6

Please sign in to comment.