DamienHarper / auditor

auditor, the missing audit log library
MIT License
164 stars 53 forks source link

exception while creating audit table schema when using single table inheritance #132

Closed nalxnet closed 1 year ago

nalxnet commented 1 year ago
Q A
auditor version 2.3.0
PHP version 8.1.8
Database MySQL / SQLite

Summary

I have an abstract parent entity which is not audited, but uses doctrine/orm inheritance type "SINGLE_TABLE" and defines the database table for its child entities. Some child entities are configured to be audited.

With auditor 2.3.0, creating the audit database schema throws an exception.

Current behavior

When the database schema is created for this abstract parent entity, the CreateSchemaListener wants to create the corresponding audit table. This throws an exception while trying to determine the table name from the configuration with the new logic introduced in auditor 2.3.0, as the parent entity does not have any configuration: Undefined array key "App\AbstractParentEntity"

https://github.com/DamienHarper/auditor/blob/19ff16a658e7bf268dbea7b88c08118da5caf1ae/src/Provider/Doctrine/Persistence/Schema/SchemaManager.php#L173

In versions before 2.3.0, the table name from the event was being used, which is independent of the configuration.

How to reproduce

Define an abstract parent entity and an audited child entity:

namespace App;

/**
 * @ORM\Table(name="entities")
 * @ORM\Entity()
 * @ORM\InheritanceType("SINGLE_TABLE")
 */
abstract class AbstractParentEntity {}

/**
 * @ORM\Entity
 */
class Entity {}

Configure auditing for the child entity:

dh_auditor:
    enabled: true
    providers:
        doctrine:
            table_prefix: ~
            table_suffix: '_audit'
            entities:
                App\Entity: ~

Create the database schema using Doctrine\ORM\Tools\SchemaTool::createSchema for the parent entity to create an Doctrine\ORM\Tools\ToolEvents::postGenerateSchemaTable event.

Expected behavior

Creating the database schema for the abstract parent entity and its child entities does not throw an exception and creates the corresponding audit table schema.

DamienHarper commented 1 year ago

@nalxnet should be fixed now with 2.3.1, thanks for the feedback!

nalxnet commented 1 year ago

Thanks for the quick fix, will test this in my code once its used by https://github.com/DamienHarper/auditor-bundle

DamienHarper commented 1 year ago

It should already be the case, auditor-bundle version 5.2.0 targets auditor version ^2.3 which means >= 2.3.0 and < 2.4.0 Just run composer update and it should do the trick

nalxnet commented 1 year ago

You're absolutely right, thanks. Tested it but unfortunately the exception is still thrown because SchemaManager::createAuditTable is still called with the untracked parent entity and not the tracked child entity. Looks like https://github.com/DamienHarper/auditor/pull/139 will fix this.

DamienHarper commented 1 year ago

@nalxnet 2.4.0 is out and fixes this issue. Sorry for the inconvenience and kudos to @dmitryuk.

nalxnet commented 1 year ago

Thanks, but since it's a new minor release I'll have to wait for a new auditor-bundle release this time. 😄

dmitryuk commented 1 year ago

@nalxnet kindly run ’composer u damienharper/auditor’

^2.3 which means >= 2.3.0 and < 3.0.0 as I know

nalxnet commented 1 year ago

You're absolutely right, I got confused by @DamienHarper 's comment above. It's working again without errors. Again, thanks a lot for the quick fix! 👍