Skip to content

Fix audit schema generation for reserved-keyword table names on DBAL 4.3+#683

Open
gulaandrij wants to merge 1 commit into
sonata-project:1.xfrom
gulaandrij:fix/audit-reserved-table-name
Open

Fix audit schema generation for reserved-keyword table names on DBAL 4.3+#683
gulaandrij wants to merge 1 commit into
sonata-project:1.xfrom
gulaandrij:fix/audit-reserved-table-name

Conversation

@gulaandrij

Copy link
Copy Markdown

Subject

On doctrine/dbal >= 4.3, audit schema generation crashes with Doctrine\DBAL\Schema\Exception\InvalidState: Object name has not been initialized for any audited entity whose table name is an SQL reserved keyword (and is therefore force-quoted).

CreateSchemaListener derives the audit table name from $entityTable->getObjectName()->toString(), which on DBAL >= 4.3 renders a quoted identifier for reserved keywords (e.g. "user"). Concatenating it with the configured table prefix/suffix yields "user"_audit, which is not a parseable identifier: DBAL silently leaves the new table's name uninitialized and then throws from getObjectName() on the next access (here, while building the revision index name).

The pre-4.3 code path used Table::getName() (which returns the unquoted name), so it was not affected.

This PR adds DbalCompatibilityTrait::getUnquotedTableName(), which on DBAL >= 4.3 returns the raw unquoted identifier value (getObjectName()->getUnqualifiedName()->getValue(), prefixed with the qualifier when present) and otherwise falls back to Table::getName(). It is used at the three audit-table-name derivation sites (the entity table and the many-to-many join tables), restoring the historical behaviour.

A regression test (IssueReservedTableNameTest, with an entity mapped to a reserved `user` table) reproduces the crash before the fix and passes after it. The full test suite, PHPStan, PHP-CS-Fixer and Rector all pass on doctrine/dbal 4.4.3.

I am targeting the 1.x branch, because this is a backwards-compatible bug fix.

Changelog

### Fixed
- Crash during audit schema generation for entities whose table name is a reserved SQL keyword on `doctrine/dbal` 4.3+

…4.3+

On doctrine/dbal >= 4.3 the audit table name was derived from getObjectName()->toString(), which renders a quoted identifier for reserved keywords (e.g. "user"). Concatenating it with the table prefix/suffix produced an unparseable name ("user"_audit), leaving the new table's name uninitialized and crashing with InvalidState: Object name has not been initialized. Use the unquoted identifier value instead, matching the pre-4.3 getName() behaviour.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.

1 participant