feat(backend): roll back a merge whose writes have several timestamps
Human summary
Once the schema migrations visibility work is complete, it will be possible to have multiple schema migrations that run in the same chain and have different timestamps (right now any changes on a schema load all get the same timestamp). If any of those migrations fail we need to roll all of them back, so we need to support multiple timestamps in the rollback logic and in the schema migrations.
- update all the schema migration queries to use the same shared cypher snippet for setting the
[previous_]updated_at/bymetadata during a migration so that we can correctly roll back if an object is touched by multiple schema migrations during the same schema application - update the rollback cypher to support checking multiple timestamps instead of just one time stamp
Why
IFC-3623 will run each schema change of a merge at its own timestamp (project F2 of the INFP-719 design document). The merge rollback assumes that every write of a merge has one timestamp, so it breaks in two ways when a merge has several:
- On the global branch, the rollback reverses only the edges stamped at exactly the merge start. In the second spike of the design document, a branch-agnostic attribute added by a later step stayed on main after a failed merge.
- Each vertex keeps one previous value of
updated_atandupdated_by. A metadata writer saves the current value as the previous one wheneverupdated_at <> $at, so when two steps change the same vertex, the second step saves the first step's stamp, and rolling back both steps leavesupdated_atandupdated_bynull.
This PR changes the rollback and the metadata writers so that a merge with several timestamps rolls back to its state before the merge. Today's callers pass one timestamp, so today's merge, recovery and schema load write and revert what they do now.
Not in this PR:
- Passing the step timestamps through the schema migration flow (
SchemaApplyMigrationData,schema_path_migrate, the schema update coordinator, the schema change application runner): IFC-3623. That flow is reworked onajtmccarty/ifc-3614-store-migrations. - Reading the step timestamps from the stored schema change applications in
recover(): IFC-3623. - Passing the step timestamps to
DiffMerger.rollback(core/diff/merger/merger.py), which rolls back a merge that fails in the same process: IFC-3623. Today it passes the merge start only. - Passing the operation's timestamps to the number pool writers that a NumberPool attribute-add migration runs after its queries: they stamp with only their own timestamp. IFC-3623 decides whether they receive them.
Closes IFC-3631
What changed
Behaviour for callers that pass several timestamps:
GraphRollbacker.rollback(target_branch, ats, scope)takes a list of timestamps in place of one. On the global branch, and on the target branch withAT_TIMESTAMP, it reverses the edges stamped at exactly one of them. WithSINCE_TIMESTAMP, the target-branch range starts at the earliest one, in whatever order the list is given. The metadata restore matchesupdated_at IN $ats. An empty list raisesValueError.- A metadata writer saves the previous value only when
updated_atis not one of the operation's timestamps ($operation_ats, which always includes the write's own timestamp). A vertex changed by several steps of one operation therefore keeps the value from before the operation, and the rollback restores it.
Implementation notes:
operation_atsis an optional argument ofQuery.__init__, next toatanduser_id. Schema migrations fill it fromMigrationInput.operation_ats. Putting it onQuerykeeps the inheritance of the migration queries as it is; a shared base class and a narrower query type forSchemaMigrationwere considered and not chosen.stamp_vertex_metadata(core/query/vertex_metadata.py) returns its Cypher text together with theoperation_atsparameter. The seven migration query modules that repeated the sameCASEblock now call it, so the rule exists in one place.
What stayed the same:
- With one timestamp,
IN [x]and>= xselect what= xand>= xselected before, andNOT updated_at IN [at]is the oldupdated_at <> at. - No database schema change, no new vertex property, no new index.
DiffMergeMetadataQueryis unchanged: the graph merge is the first write of a merge, so no earlier step can have changed a vertex.
How to review
backend/infrahub/core/query/rollback.pyandbackend/infrahub/core/rollback.py: the rollback conditions.backend/infrahub/core/query/vertex_metadata.pyandbackend/infrahub/core/query/__init__.py: the writer rule and whereoperation_atsenters a query.- The migration queries in
backend/infrahub/core/migrations/and the callers incore/query/resource_manager.pyandcore/query/node.py: mechanical switches to the helper, each keeping its own timestamp parameter. TestRollbackOfStepsAtSeveralTimestampsinbackend/tests/component/core/test_rollback.py.dev/knowledge/backend/merge-failure-recovery.md: invariants 3 and 5, the range rollback paragraph and the list of changes that require re-evaluating recovery. Invariant 5 states the condition under which the metadata restore runs only once: every writer of the operation received the earlier steps' timestamps. The schema migration flow does not pass them yet.
The other test files only change at=X to ats=[X].
How to test
uv run pytest backend/tests/component/core/test_rollback.py -v
uv run pytest backend/tests/component/core/migrations/schema -q
uv run pytest backend/tests/unit/core/migrations -q
TestRollbackOfStepsAtSeveralTimestamps builds one scenario in a class-scoped fixture:
- step 1 at T1 adds a branch-aware and a branch-agnostic attribute, and changes the branch-agnostic
serialof widget D on main, which closes a global edge at T1; - between the steps, a user branch writes a global edge, and widget B's metadata is stamped;
- step 2 at T2 adds a branch-agnostic attribute with
operation_ats=[T1]; - the rollback runs with
ats=[T2, T1], then again.
Its tests check that main is back to its state before step 1, that the global edges of both steps are removed, that widget D's global edge closed at T1 is open again (T1 is not the first element of ats), that the user branch's global edge is kept, that widget A's updated_at/updated_by are back to the values before step 1, that widget B's are back to the write between the steps, that a second rollback changes nothing, and that an empty list is refused.
Impact & rollout
- Backward compatibility: callers that pass one timestamp behave as before.
GraphRollbacker.rollbackand the two rollback queries takeatsin place ofat; every caller in this repository is updated. - Performance: both rollback passes were already label-less scans over a branch's edges;
INon a short list replaces=. - Config/env changes: none.
Checklist
- Tests added/updated
- Changelog entry added (
uv run towncrier create ...): not added, no behaviour a user can observe changes - External docs updated (if user-facing or ops-facing change): not applicable
- Internal .md docs updated (internal knowledge and AI code tools knowledge)
- I have reviewed AI generated content
š¤ Generated with Claude Code