Skip to main content
Version: 8.x

Upgrading to 8.0

Migrate from auditor-bundle 7.x to 8.0

This guide covers upgrading from auditor-bundle 7.x to 8.0.

⚠️ DoctrineProvider extraction completed in auditor 5.0​

auditor-bundle 8.0 targets damienharper/auditor 5.x, in which the embedded DH\Auditor\Provider\Doctrine namespace has been fully removed from the core library.

All Doctrine ORM functionality now lives exclusively in damienharper/auditor-doctrine-provider, which auditor-bundle already requires as a direct dependency.

IMPORTANT

The class namespace has not changed. Classes are still under DH\Auditor\Provider\Doctrine\* — they simply come from the auditor-doctrine-provider package instead of the auditor core. Only two imports change: the deprecated attribute aliases (see Attributes) and NeedsConversionToAuditableType.

Attributes​

The deprecated 4.x aliases DH\Auditor\Provider\Doctrine\Auditing\Attribute\Auditable, Ignore and Security have been removed (only AttributeLoader remains in that namespace). Import the attributes from DH\Auditor\Attribute:

// Before (deprecated aliases, auditor 4.x)
use DH\Auditor\Provider\Doctrine\Auditing\Attribute\Auditable;
use DH\Auditor\Provider\Doctrine\Auditing\Attribute\Ignore;
use DH\Auditor\Provider\Doctrine\Auditing\Attribute\Security;

// After
use DH\Auditor\Attribute\Auditable;
use DH\Auditor\Attribute\Ignore;
use DH\Auditor\Attribute\Security;

For auditor-bundle users​

No action required for the extraction. The bundle automatically requires damienharper/auditor-doctrine-provider and wires everything transparently. Your dh_auditor.yaml configuration continues to work without any changes. Your existing audit tables, however, must be migrated: see Audit Table Schema Reform below.

For users using DoctrineProvider directly (without the bundle)​

If you use DoctrineProvider directly in a non-bundle project, install the standalone package explicitly:

composer require damienharper/auditor-doctrine-provider:^2.0

The provider imports remain identical:

// Works in both 4.x (from auditor core) and 5.x (from auditor-doctrine-provider)
use DH\Auditor\Provider\Doctrine\DoctrineProvider;
use DH\Auditor\Provider\Doctrine\Configuration;
use DH\Auditor\Provider\Doctrine\Persistence\Reader\Reader;
// ...

NeedsConversionToAuditableType (if applicable)​

If you implemented the NeedsConversionToAuditableType interface on a custom Doctrine type, update the import:

// Before (auditor 4.x)
use DH\Auditor\Transaction\NeedsConversionToAuditableType;

// After (auditor 5.x / auditor-doctrine-provider 2.x)
use DH\Auditor\Provider\Doctrine\Transaction\NeedsConversionToAuditableType;

⚠️ Audit Table Schema Reform (schema version 2)​

auditor-bundle 8.0 also ships damienharper/auditor-doctrine-provider 2.x, which introduces a revised audit table schema. The new schema:

  • Replaces four separate blame_* columns with a single blame JSON column
  • Replaces the SHA-1 transaction_hash VARCHAR(40) with a ULID transaction_id CHAR(26)
  • Adds a schema_version column to distinguish legacy and current diffs formats
  • Adds a transaction_id index and two composite indexes, (object_id, type) and (blame_id, created_at), and drops the 7.x (blame_user, blame_id) index
  • Unifies the diffs JSON format across all operation types

Existing audit tables are not modified automatically: they must be migrated with audit:schema:migrate, which converts the existing entries, then drops the legacy columns. There is no rollback: back up the audit tables first. Count 3 to 5 minutes per million audit rows, during which the application must not write audits.

🚀 Upgrade Steps​

The complete procedure, with the backup, the rehearsal and the estimate of the duration, the options of audit:schema:migrate and troubleshooting, is in the auditor-doctrine-provider 2.0 upgrade guide. In short, and in this order:

1️⃣ Back up the audit tables and rehearse​

Back up every audit table, then run the whole procedure on a copy of your production data: it tells you how long the maintenance window must be.

2️⃣ Stop writing audits​

Put the application in maintenance mode, and stop the Messenger workers and scheduled tasks (including the audit retention): neither 7.x nor 8.0 can write audits into a table during its migration.

3️⃣ Update auditor-bundle​

composer require damienharper/auditor-bundle:^8.0
bin/console cache:clear

This will automatically pull in damienharper/auditor:^5.0 and damienharper/auditor-doctrine-provider:^2.0.

CAUTION

Do not run doctrine:schema:update, nor a Doctrine migration that changes the audit tables, before step 4: on v1 tables, they drop the legacy columns without converting their data. If your audit tables are created by Doctrine migrations (test and CI databases, new environments, a deployment that migrates when it starts), see Audit tables managed by Doctrine Migrations: a replayable migration and a deployment order.

4️⃣ Migrate the audit tables​

# Preview the DDL
bin/console audit:schema:migrate --dump-sql --convert-all

# Run the full migration in one pass (new columns, transaction hash and diffs conversions, legacy columns dropped)
bin/console audit:schema:migrate --force --convert-all

WARNING

Always pass --convert-all: --force alone adds the new columns, moves the blame into blame and drops the legacy blame_* and ip columns, but transaction_hash and the legacy diffs stay, and the tables stay on the legacy schema (audit:schema:update keeps refusing them) until the conversions have run. --drop-transaction-hash drops transaction_hash without converting it: the migrated entries lose their transaction grouping for good.

5️⃣ Apply the rest of the schema​

audit:schema:update refuses to run while an audit table still has the legacy v1 schema: run it only now.

bin/console audit:schema:update --dump-sql
bin/console audit:schema:update --force

6️⃣ Check, then reopen​

audit:schema:migrate must report that all audit tables are at schema version 2, and audit:schema:update --dump-sql that there is nothing to update. Open a few entries in the viewer, then restart the workers and scheduled tasks, and leave maintenance mode.

🔄 Breaking Changes (schema v2)​

$entry->transactionHash removed — use $entry->transactionId

// Before (7.x)
$entry->transactionHash; // SHA-1 VARCHAR(40)

// After (8.0)
$entry->transactionId; // ULID CHAR(26)

The old property has been removed (no deprecated alias). Update any code that accesses transactionHash directly. For un-migrated v1 rows, Entry::fromArray() automatically falls back to the legacy transaction_hash column so $entry->transactionId is still populated before your tables are migrated.

Blame properties now read from blame JSON column

$username, $userFqdn, $userFirewall, and $ip still work — they now read from the unified blame JSON column. No code changes required.

User provider and impersonation

  • The built-in UserProvider no longer appends [impersonator <name>] to the username: the impersonated user is blamed, and the user who impersonates them is written in the impersonator key of the blame JSON ($entry->impersonator, ImpersonatorFilter, an "impersonated by" badge in the viewer). Exports get the blame_impersonator_id, blame_impersonator and blame_impersonator_fqdn fields.
  • user_fqdn is the class of the user of the security token (it was DH\Auditor\User\User). Custom user providers pass it as the third argument of User: new User($id, $name, DoctrineHelper::getRealClassName($user)) (DH\Auditor\Provider\Doctrine\Persistence\Helper\DoctrineHelper resolves Doctrine proxies).
  • Existing entries are not rewritten. To find the ones written with the former suffix:
-- MySQL, MariaDB, SQLite
SELECT * FROM post_audit WHERE JSON_EXTRACT(blame, '$.username') LIKE '%[impersonator %';
-- PostgreSQL
SELECT * FROM post_audit WHERE CAST(blame AS jsonb) ->> 'username' LIKE '%[impersonator %';

Remove entries can include a full entity snapshot

For schema version 2 entries, getDiffs() on a remove entry is empty by default: getDiffSource() identifies the deleted entity. With the provider option full_snapshot_on_remove: true (under providers.doctrine), it returns the full entity state before deletion, with old populated and new set to null for every field.

getDiffSource() available on all entry types

Use getDiffSource() to access the entity metadata (id, class, label, table) that is now present on all operation types in schema version 2.

✨ New Features​

None of these features exists in 7.x. They are all optional: a 7.x configuration keeps working unchanged.

Viewer​

  • Actions told as phrases (alice changed Status: Draft → Published), with their technical details one click away, on new screens: an overview with the recent activity, the history of an object and of the entities attached to it, an action page, and the actions of a user. See Pages.
  • Compare two revisions of an object, field by field, from its history. See Compare Revisions.
  • New filters: who, what, field, origin and period on the history of an object; changed field (needs native JSON and a migrated table) and date range on the stream of an entity. The entity list can be sorted by name, number of actions or latest activity.
  • Schema v2 diffs: removals can show the full state of the deleted object (full_snapshot_on_remove), JSON deep diffs get a dedicated rendering, and diff label resolvers (#[DiffLabel]) show readable values, wired by their FQCN as service ID.
  • URL generator: DH\AuditorBundle\Viewer\AuditViewerUrlGenerator and its Twig functions link an entity, an object, a transaction or a user to its page in the viewer. See Generating URLs.
  • The user filter is computed from the blame_id index, which keeps the stream fast on large audit tables.
  • Strict Content Security Policy: no inline script nor style attribute any more. See Content Security Policy.

Export​

/audit/export streams audit entries as NDJSON, JSON or CSV, filtered by entity, object, period or user, optionally anonymized, and restricted to the entities the user can view. See Export.

Initial snapshot: audit:snapshot​

When auditing is enabled on a database that already holds data, run audit:snapshot once: it writes an insert entry flagged as a snapshot for every existing entity (in batches, skipping the entities that already have an audit history), so their history starts with their state at that time.

php bin/console audit:snapshot --all --dry-run # count what would be written
php bin/console audit:snapshot --all

The command is registered by the bundle. The viewer tells these entries as snapshots (a SNAPSHOT badge on the compare page), and Entry::isSnapshot() tells them from real insertions in your own code.

audited_columns and ignored_columns_resolver​

entities.*.audited_columns audits only the listed properties (allowlist), and ignored_columns_resolver takes the ID of a service returning the properties to ignore for a given entity object. Ignoring always wins over the allowlist. See Configuration.

Histories across entities​

#[AuditRoot] and the roots entity option attach the audit entries of an entity to the history of another one (the audit_root table, audit:roots:rebuild); Reader::createRootQuery() reads that history with cursor pagination. root_resolver, entity_label_resolver and read_scope take service IDs, field labels come from the dh_auditor_fields translation domain, and the dh_auditor_last_change(), dh_auditor_last_changes() and dh_auditor_field_label() Twig functions tell who changed a field. See Histories. The |dh_auditor_phrase, |dh_auditor_phrase_text and |dh_auditor_author Twig filters tell an action as a readable phrase whose objects link to their history, customizable through the dh_auditor_history translation domain, #[AsChangeDescriber] services and the ObjectUrlResolverInterface service. See Phrases.

Blame contexts​

Audit entries can record how a change was made in the via key of the blame JSON (no schema change):

  • every console command runs in a console context labelled with the command name;
  • the opt-in dh_auditor.messenger.blame_context_middleware blames the changes made by a worker on the user who dispatched the message;
  • your code can push its own contexts through the autowirable DH\Auditor\Blame\BlameContextStack service;
  • the viewer shows the context next to the author, and exports carry it as blame_via (see Export).

See Blame Context.

Scheduled retention​

Audit entries can be pruned on a schedule with Symfony Scheduler, per entity (retention.max_age / retention.max_entries, or #[Auditable(maxAge: …, maxEntries: …)]), with an optional default. Disabled by default; it needs symfony/scheduler, symfony/messenger and dragonmantank/cron-expression. See retention.

Doctrine provider options​

The providers.doctrine options of auditor-doctrine-provider 2.0 are available:

  • batch_size: writes the audit entries of a transaction with multi-row INSERT statements (10 to 67 % faster flushes with many entities);
  • compression: compressed diffs and extra_data columns on MariaDB;
  • skip_disabled_entities: no audit table for entities that are disabled by default;
  • full_snapshot_on_remove: remove entries record the last value of every audited field.

Extensibility​

  • Custom providers are registered with the dh_auditor.provider tag.
  • The console user provider can be decorated: decorate the DH\AuditorBundle\User\ConsoleUserProvider service with your own DH\AuditorBundle\User\ConsoleUserProviderInterface implementation.
  • Failures that cannot be thrown (e.g. audit rows left behind after a rollback) are logged, on the auditor channel with MonologBundle.

🔄 Other Changes​

Entity ignored_columns of #[Auditable] entities are applied

In 7.x, the attributes of an #[Auditable] entity replaced its whole YAML entity options (except retention): the entity ignored_columns set in YAML were silently dropped. They are now added to the #[Ignore] properties, as documented. Check that the properties listed there are really meant to be ignored.

Viewer templates rewritten

The templates of the viewer were rewritten around actions (see Override Templates): a template overridden from 7.x under templates/bundles/DHAuditorBundle/Audit/ no longer receives the same variables and must be rewritten from the new one. In particular:

  • audits.html.twig (now the overview) receives activity and counts actions instead of entries, in its activity graph too (activityGraphRaw; the audit.audit_details.events_count translation is removed);
  • entity_stream.html.twig only renders the stream of a whole entity: the history of an object is object_history.html.twig, whose period filter is when (last days) instead of date_from / date_to;
  • transaction_stream.html.twig renders one action (row, entries), no longer paginated;
  • user_stream.html.twig receives rows (actions) instead of entries;
  • entry.html.twig is removed: compare.html.twig renders its changes like the action details, with the text_change() and related() macros of History/_macros.html.twig.

The templates are styled with design tokens (see Theme) instead of the Tailwind palette, and every Tailwind class carries the dh: prefix (dh:bg-dh-card, dh:text-dh-muted-foreground, dh:hidden dh:sm:inline…): the viewer never meets the classes of an application that uses Tailwind too, whatever the order of the stylesheets. The precompiled app.css only holds the prefixed classes of the bundle templates, so an overridden template that keeps 7.x classes (slate-*, indigo-*, emerald-*…) or unprefixed ones loses their styles: rewrite it with prefixed classes and the tokens.

app.css no longer resets the whole page: its reset only reaches the content of the viewer pages, under the <div data-dh-auditor-viewer> element in which each page of Audit/ now wraps the content of its dh_auditor_content block (keep it in an overridden page), and the <twig:DhAuditor:History> component. The layout of the bundle loads bundles/dhauditor/preflight.css too; a custom layout that extends the one of the application only loads app.css, before or after its own stylesheets, and the rest of its page keeps their reset and font. The wrapping element makes the content of each page one child of the element that renders the dh_auditor_content block, which a flex or grid layout around it may need to know. An application that compiles the viewer or the <twig:DhAuditor:History> component with its own Tailwind does it in a stylesheet of its own that imports viewer.css with the dh prefix (see Styling).

The route names and their path parameters are unchanged, but the history of an object (/audit/{entity}/{id}) reads other query parameters: type became what, user became who, date_from / date_to became when (last 7, 30 or 90 days), page became the after cursor, and origin and attached are new. Links built with the AuditViewerUrlGenerator (or dh_auditor_entity_url()) get type and user translated; links written by hand with the 7.x filters lose them.

Overridden layout.html.twig: load viewer.js

The theme toggle, the sort of the entity list and the filters submitted on change moved from inline scripts to bundles/dhauditor/viewer.js. A layout that does not extend the bundle's one must load it (see Custom Layout); templates overridden from 7.x keep their inline scripts and work as before, outside a strict CSP.

Flushes made before the provider is built are audited (also in 7.3)

The auditing listeners are declared as lazy Doctrine listeners of the connections of the auditing entity managers, instead of being attached when the DoctrineProvider service is built. Before this fix, with a warm metadata cache (the usual production setup), nothing built the provider before kernel.controller: the flushes made earlier in a web request (a form_login / json_login listener updating the user, a kernel.request listener) were not audited.

This needs every auditing entity manager to be a DoctrineBundle one, using the event manager of its connection. Otherwise the provider keeps attaching the listeners when it is built, as before, and the container compilation log (var/cache/<env>/*Compiler.log) says which entity manager prevents it.

When the listeners are declared this way, workarounds that built the Auditor service before the first flush, so that writes made before any controller (a firewall listener, a kernel.request listener) were audited, are no longer needed: a Doctrine preFlush listener that only injects Auditor, for example, can be removed. Check first that the container compilation log does not report an entity manager that prevents it.

❓ Need Help?​

  • Check GitHub Issues
  • Open a new issue with details about your error