Repository navigation
fix(audit): keep campaign history in write order within the same second - #166
Merged
Merged
Conversation
Audit events store whole-second timestamps and random IDs, and history was ordered created_at DESC, id ASC, so events written in one second (a scripted create, snapshot, submit, approve) appeared in arbitrary order. Schema 6 adds sequence_number, a unique auto-increment insertion order, to the audit events table, and history is now ordered created_at DESC, sequence_number DESC. dbDelta adds a column and its index in separate statements, which MySQL refuses for an AUTO_INCREMENT column, so the migration adds the column and its key in one ALTER before dbDelta runs; the CREATE TABLE carries the same definition for fresh installs. Existing events are numbered as the database reads them, since their order within a second was never recorded. Re-running, or resuming before the version is stamped, changes nothing. Verified on MySQL in CI's harness and on the Studio SQLite site (281 events, every one numbered). The runbook documents rolling a release back to schema 5: that code ignores the new column, so lowering the stamped version is safe. Closes #164 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #164. Part of #65.
Problem
Audit events store whole-second
created_atvalues and random IDs, and history was orderedcreated_at DESC, id ASC. Events written in the same second therefore showed in arbitrary order. For example, a scripted create → snapshot → submit → approve listed "Approved" below "Created".Change
sequence_numberto{prefix}campaignbridge_audit_events. It's a uniqueAUTO_INCREMENTinsertion order.created_at DESC, sequence_number DESC. Thetarget_createdindex still serves the filter.AUTO_INCREMENTcolumn that isn't a key. SoSchema_Manageradds the column and its key in oneALTERbefore dbDelta runs, and dbDelta then sees nothing to change. Fresh installs get the column from theCREATE TABLE.api.md: the history order.campaign-persistence.md: the new column.runbook.md: how to roll back to a schema-5 release. That code never reads the column, so lowering the stamped version is safe.Evidence
Campaign_Schema_Migration_Test):sequence_number.Campaign_Operation_Repository_Test): four events in one second, with IDs that sort against write order, read back newest write first, including across pages. A later second still sorts first even when written earlier.id ASCordering makes this test fail.auto_increment, and a second run was a no-op.Verification
pnpm qa:fastpasses.🤖 Generated with Claude Code