Skip to content

Store the metadata with DBAL in a table the bundle owns - #40

Merged
loevgaard merged 4 commits into
2.xfrom
metadata-store-dbal
Nov 30, 2026
Merged

loevgaard merged 4 commits into
2.xfrom
metadata-store-dbal

Conversation

@loevgaard

@loevgaard loevgaard commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Closes #16, closes #39. Supersedes #38.

Summary

The metadata store reads and writes its own table with DBAL, so it never touches the application's entity manager. The bundle has no entity anymore: a schema listener puts the table into the schema Doctrine generates, like Symfony's session, cache, lock and Messenger tables.

load(clientId)                       lazy, on first use of the metadata
  SELECT metadata FROM setono_client__metadata WHERE client_id = ?
store(client)                        kernel.finish_request
  if metadata hasn't changed → return            # no connection, no query
  UPDATE … SET metadata = ? WHERE client_id = ?
  if no row matched
    transactional(INSERT …)                      # a savepoint inside a transaction of the application
    unique violation → ignore                    # a concurrent request inserted it first and wins
postGenerateSchema                   doctrine:migrations:diff, doctrine:schema:update
  MetadataTableSchemaListener → store->configureSchema()
    adds setono_client__metadata (client_id VARCHAR(255) PK, metadata JSON NULL)
    only for an entity manager on the store's connection
 src/
-├── Entity/Metadata.php, MetadataInterface.php
 ├── MetadataStore/
-│   ├── DoctrineOrmBasedMetadataStore.php
+│   ├── DoctrineDbalBasedMetadataStore.php     # gets the default connection as a service closure
+│   └── MetadataTableSchemaListener.php
 config/
-└── doctrine-mapping/{entity,mapped-superclass}/Metadata.orm.xml

Also gone: the metadata_class option, the compile-time switch between the two mappings, setono/doctrine-orm-trait, and the custom_metadata and interface_only_metadata test environments. ADR-0005 records the decision and why #38's entity manager per write, keeping the mapping as the schema, and two schema code paths per DBAL version were rejected. The listener uses DBAL's schema API from before 4.5, which works on 3.7–4.x; DBAL 4.5 deprecates it silently unless the application enables doctrine/deprecations.

The third commit pins doctrine/orm ~2.17.0 in the PHP 8.1 lowest unit test job. Without setono/doctrine-orm-trait, whose constraint ruled out ORM 3.0, Composer's lowest dependencies resolve ORM 3.0.0, so nothing tested ORM 2 anymore.

The fourth commit adds a database test job: the suite against MySQL 8.4 and PostgreSQL 16 service containers, with the lowest and the highest dependencies. The store sends its own SQL now, and SQLite hides how those databases handle it.

Two deviations from #39: the concurrent INSERT is simulated with a DBAL wrapper_class (ConcurrentInsertConnection, which inserts the row right after the store's UPDATE) instead of a middleware, and UPGRADE-2.0.md says the next generated migration renames clientId, because it does (below), instead of asking for a rename by hand first.

Evidence

  • Before: on 2.x, MetadataTest asserted that a request using the metadata starts the entity manager, and Write the metadata through the store's own entity manager #38's test showed the store's flush writing another client's unflushed change.
    After: it_stores_metadata_without_an_entity_manager stores metadata and asserts that no entity manager was started.

  • Before: MetadataTableTest::it_adds_the_metadata_table_to_the_schema_of_an_entity_manager_on_the_default_connection failed: the 2.x mapping gave the columns clientId and metadata.
    After: it passes, and so do the tests that the table stays out of another connection's schema and that the created schema is in sync.

  • Before: on PostgreSQL, it_keeps_the_row_of_a_concurrent_request_that_stored_it_first failed with SQLSTATE[25P02]: current transaction is aborted: the failed INSERT aborted the surrounding transaction.
    After: with the INSERT in transactional(), it passes.

  • The suite (98 tests) passes on SQLite, MySQL 8.4 and PostgreSQL 16 with the highest dependencies; on SQLite, MySQL and PostgreSQL with PHP 8.1, ORM 2.17.0, DBAL 3.7.0, DoctrineBundle 2.11.3 and Symfony 6.4.0; and on SQLite with PHP 8.2, ORM 3.0.0 and Symfony 7.0.0. PHPStan, ECS, composer validate/normalize and the dependency analyser pass. Infection: 100% MSI, 98 of 98 mutants killed.

  • The table as 2.x's mapping created it, compared with the listener's table, on MySQL 8.4 and PostgreSQL 16, with DBAL 3.7 and 4.5:

    2.x naming strategy Generated migration
    Flex recipe (underscore_number_aware) none
    DoctrineBundle's default MySQL: CHANGE clientId client_id VARCHAR(255) NOT NULL (primary key dropped and added around it); PostgreSQL: RENAME COLUMN clientid TO client_id
    DoctrineBundle's default, column renamed by hand none

Merge Danger

Door: two-way

2.0 isn't released, so a revert restores the entity and the ORM store, and no application has run the column rename yet.

Blast Radius: schema

  • Applications on DoctrineBundle's default naming strategy, e.g. Sylius-Standard shops, get a migration that renames clientId to client_id. UPGRADE-2.0.md says to check that it renames rather than drops the column.
  • Applications with a custom metadata_class copy their rows into setono_client__metadata and remove their own mapping of that table, or Doctrine fails to generate the schema.
  • The application's Doctrine listeners no longer see metadata writes.
  • On DBAL 3 without use_savepoints, a failed INSERT inside a transaction of the application still aborts it on PostgreSQL, as the ORM's flush did.
  • The listener adds the table even when an application replaces the store by overriding the MetadataStoreInterface alias.

Adds ADR-0005, decided while reviewing #38, the first fix for #16.
The metadata store wrote through the application's entity manager, so its
flush also wrote every change the application made to its managed
entities without flushing it, and a failed INSERT closed that entity
manager (#16).

DoctrineDbalBasedMetadataStore replaces DoctrineOrmBasedMetadataStore. It
reads and writes the table setono_client__metadata (client_id, metadata)
with DBAL on DoctrineBundle's default connection, which it gets as a
service closure, so a request that doesn't use the metadata doesn't start
the connection. A write is an UPDATE, and an INSERT when no row matched.
The INSERT runs in a transaction, so when it fails inside a transaction
of the application, it rolls back to a savepoint instead of aborting the
application's transaction on PostgreSQL. MetadataTableSchemaListener adds
the table to the schema of the entity managers on that connection, so
migrations create it.

The bundle has no entity anymore: Entity\Metadata,
Entity\MetadataInterface, the metadata_class option, both mappings and
the compile-time switch between them are gone, with the custom_metadata
and interface_only_metadata test environments. A table created under
DoctrineBundle's default naming strategy has the column clientId, which
the next generated migration renames to client_id.

Closes #16
Closes #39
Without setono/doctrine-orm-trait, whose constraint ruled out ORM 3.0,
Composer's lowest dependencies resolve ORM 3.0.0: they pick doctrine/cache
1.11.0 and symfony/polyfill-php80 1.15.0, which ORM 2.17 doesn't accept.
So no job tested ORM 2 anymore. The PHP 8.1 lowest unit test job now pins
doctrine/orm ~2.17.0, while the PHP 8.2 lowest jobs keep testing ORM 3.0.
The metadata store sends its own SQL through DBAL now, and SQLite hides
how MySQL and PostgreSQL handle it: PostgreSQL aborts the surrounding
transaction when a statement fails, which only a manual run against
PostgreSQL showed. A database test job runs the suite against MySQL 8.4
and PostgreSQL 16 service containers, with the lowest and the highest
dependencies, through SETONO_CLIENT_BUNDLE_DATABASE_URL.
@loevgaard
loevgaard merged commit 2b2e8a8 into 2.x Nov 30, 2026
23 checks passed
@loevgaard
loevgaard deleted the metadata-store-dbal branch November 30, 2026 12:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Store the metadata with DBAL in a table the bundle owns The metadata persister flushes the application's unflushed changes

1 participant