feat(schema-change): store schema changes and their history [IFC-3614]
slide deck going over the major changes
Why
A schema owner cannot view which schema changes were applied to a branch, which migrations they ran, or why a schema load failed. A merge and a rebase also cannot apply a branch's schema loads one by one in order, because the loads are not stored.
This PR is project B of the INFP-719 design document: every schema load and every dropdown or enum mutation that changes the schema stores a schema change, its operations, its application and its migrations, and a GraphQL query returns the schema change history of a branch.
Non-goals, left to later projects of the design document:
- Merge and rebase store no records, and do not replay stored schema changes (project F2).
- No staged schema changes (D1), no progress of a running load (C1), no node counts per migration (C2).
- A cancelled load, or a load whose API server stops, is not rolled back: IFC-3637.
Closes IFC-3614
Refuses the attribute renames that IFC-3597 reports, where two attributes swap names or renames chain in one load, with SchemaNameReusedError (HTTP 422). Supporting those loads is IFC-3642.
What changed
Behavioral changes
-
Every schema load and every dropdown or enum mutation that changes the schema of a branch stores a schema change with its operations and one application. A load stores one migration record for each migration it runs. A load or mutation that changes nothing stores nothing.
-
A schema load and
POST /api/schema/checknow save or return the schema that the operations build when applied to the branch schema. If that schema differs from the schema file laid over the branch schema, or if applying the operations raises aValueError, the request fails withSchemaOperationsRebuildError(HTTP 422), and the message says the error is in Infrahub, not in the file. It is not a 5XX because the SDK retries those, and the same file always gives the same result; with a 422, the SDK andinfrahubctlshow the message (IFC-3643). The applier refuses an operation that adds a kind, an attribute or a relationship that the schema already has, renames a kind to one it has, or a rename that leaves two attributes or relationships of a kind with the same name (SchemaOperationTargetExistsError). -
A schema load or check that frees an attribute name of a kind, or a kind, and gives it to another attribute or kind is refused with
SchemaNameReusedError(HTTP 422) before any operation is applied. The message tells the user to load the changes in separate steps. The refused loads are:- chained or swapped attribute renames;
- an attribute renamed while a new attribute takes its old name;
- an attribute removed while another attribute is renamed onto its name;
- swapped kind names.
developaccepts the attribute cases, but its migrations find attributes by name and run at the same time, so the data can come out wrong. Supporting them is IFC-3642.developalready refuses swapped kind names, with a validation error. -
These loads are accepted, as on
develop:- a new kind that takes the name of a kind renamed in the same load;
- a relationship that takes a relationship name freed in the same load, including chained and swapped relationship renames, because relationship data is keyed by the relationship identifier, which a load cannot change;
- a kind renamed in both its name and its namespace, when its new name in the old namespace is an existing kind.
-
A file that gives an existing kind an ID that the branch schema does not hold is now refused before any operation is built, with the same
SchemaNotFoundErroras before. As a result, the schema check now refuses such a file too; before, it returned a diff. -
The schema change and its application must be saved: if a write fails, the load fails and is rolled back. When a load fails, its migration records are closed:
appliedbecomesrolled_back,waitingbecomescancelled, andrunningbecomesfailedwith the error. -
A cancelled load records a schema change that is still
running, and its application, asfailed. It leaves the migration records to the migration flow run, which keeps running on the task worker and writes their statuses. A load cancelled while its schema change is being created records nothing, so that schema change staysrunning. The schema and data the load wrote are not rolled back, as ondevelop(IFC-3637). -
A load whose data migrations report errors returns HTTP 422 instead of 502, so the SDK no longer retries it, and it returns the migration errors instead of raising
HTTPStatusError. This changes released behaviour, so it has a changelog fragment. -
When the records of a schema change are deleted during its load, for example because the branch was deleted, the load is rolled back with
SchemaChangeRecordNotFoundError(HTTP 422). -
The dropdown and enum mutations read, validate and change the list of choices or values inside the schema lock. An attribute removed before the lock is refused with a
ValidationError(HTTP 422), and an operation that the applier cannot apply withSchemaOperationsRebuildError(HTTP 422), instead of aValueError(HTTP 500). -
Deleting a branch deletes its schema change records.
-
New GraphQL query
InfrahubSchemaChange(logged-in account required): the schema changes of the request's branch in pages, each with its operations, its applications and their migrations, also in pages. It has no user documentation and no changelog fragment yet, because the epic will change it before the release.- It lists the schema changes by the time they were created, the most recent first. A
runningorfailedschema change sits among the applied ones at the time it was created. Each API server sets the creation time from its own clock, so with several API servers whose clocks differ, the order can differ from the order of the versions. - A
statusfilter that is an empty list returns no schema change. - The resolver refuses a session that is not authenticated. The permission checker finds the field only by the names of the root selections, which leave out a field inside a fragment.
- It lists the schema changes by the time they were created, the most recent first. A
Implementation notes
- The records are four vertex types linked by edges that have no
branchproperty, so rollback, diff, merge, rebase and the generic branch-data deletion do not touch them. They do not extendStandardNode, and a serializer of their own turns them into properties and back. - Graph migration
m084adds three indexes:SchemaChange(uuid),SchemaChange(branch)andSchemaChangeApplication(uuid). SchemaUpdateCoordinator.executedrops theupdate_registryparameter, which every caller set toTrue, and gainsapplication_idandfinal_step.SchemaApplyMigrationDataserializes its fields with field serializers, andSchemaMigrationPathResponseData.migration_nameandschema_pathare now required.- The migrations of a load are sorted by schema path before they get their positions, so the positions are the same between runs.
- An operation is one of seven types.
node_renamesets the name and the namespace of a kind together, so a renamed kind never passes through an existing kind, andnode_propertycannot change them. The GraphQL enumSchemaOperationTypegainsNODE_RENAME. - The operations of a load come in this order: removed kinds, then changed kinds, then added kinds. Within a changed kind: element removals, kind properties, element properties, element renames, element additions, then the kind rename.
- The applier finds an element by its name before the load. While a chain or a swap of renames runs, it picks the element that no earlier operation renamed, tracking renamed elements by ID.
Suggested review order
dev/knowledge/backend/schema-changes.mdfor the overall design.core/schema_change/operations.py,operation_builder.py,applier.py,candidate.py,candidate_comparer.py.core/schema_change/models.py,record_serializer.py,repository.py,history_reader.py, andcore/migrations/graph/m084_schema_change_indexes.py.core/schema_change/recorder.py,application_runner.py,core/schema/update_coordinator.py,core/migrations/schema/tasks.py.api/schema.py,graphql/mutations/schema.py, and thegraphql/{queries,types,resolvers,loaders}/schema_change.pymodules.core/branch/data_deleter.py.
How to review
- Generated files:
schema/schema.graphqlandfrontend/app/src/shared/api/graphql/generated/. - Spec, plan and research:
dev/specs/ifc-3614-store-schema-changes/. - Areas that need extra attention:
- the comparison of the applied schema with the file laid over the branch schema, which can refuse a load;
- the refusal of a reused attribute name or kind (
candidate.py::_refuse_a_reused_name), and the applier's handling of names that two elements share during a chain or a swap of renames; - the coordinator's final step and its rollback;
- the branch deletion query, which uses
CALL { ... } IN TRANSACTIONS.
- Four known load bugs are pinned as strict expected failures in
test_applier_known_bugs.py, and the tests are linked from IFC-3085, IFC-3599, IFC-3601 and IFC-3602.
How to test
uv run pytest backend/tests/unit/core/schema_change/
uv run pytest backend/tests/component/core/schema_change/
uv run pytest backend/tests/component/graphql/queries/test_schema_change.py
uv run pytest backend/tests/component/graphql/mutations/test_schema_dropdown_and_enum_history.py
uv run pytest backend/tests/functional/schema_change/
uv run pytest backend/tests/integration/schema_lifecycle/ -n 4
backend/tests/integration_docker/test_schema_migration.py was not run locally after the review changes; CI runs it.
Impact & rollout
- Backward compatibility: no breaking change to existing APIs. The
InfrahubSchemaChangequery is new and can change before the release. A schema load that frees an attribute name and reuses it in the same load, whichdevelopaccepts, is now refused with HTTP 422. - Performance: a schema load and a schema check now process the schema twice. On the benchmark in
backend/tests/benchmark/test_schema_candidate_builder.py, building the candidate takes 2.00 times as long as the direct load did (540.5 ms against 269.7 ms). A schema of customer size is not measured. - Deployment notes: graph migration
m084(GRAPH_VERSION84) creates three indexes.developis at 83, so the number must change if another migration lands first.
Checklist
- Tests added/updated
- Changelog entry added: one fragment, for the 502 to 422 change of a failed migration; the rest is left out until the epic is complete
- External docs updated: left out until the epic is complete
- Internal .md docs updated
- I have reviewed AI generated content
š¤ Generated with Claude Code