Expand description
Schema diff engine — EXPLAIN ALTER FOR runtime.
Computes the set of ALTER TABLE operations that would
close the gap between an existing CollectionContract and
the column shape of a new CreateTableQuery. Used by the
EXPLAIN ALTER FOR SQL command exposed at the parser /
executor boundary.
Pure logic — zero side effects, no DB access. The executor
loads the current contract, calls compute_column_diff,
and formats the result via format_as_sql /
format_as_json.
§Design highlights
- Equivalence check matches
apply_alter_operations_to_contract’s semantics so the round-trip property holds: applying the diff to a clone ofcurrentproduces a contract byte-equal to the target. sql_typeisOption<SqlTypeName>in the live contract (legacy tables don’t carry it). When the current side hasNone, we fall back to comparing the legacydata_typestring instead of declaring everything a TypeChange.- Rename detection is consultative. Even when the heuristic
is high-confidence, the SQL output still emits
DROP + ADDplus a-- hint:comment. Only a human (or a client with more context) confirms. - Three confidence tiers explicit:
High→sql_type+ every constraint +defaultmatchMedium→sql_type+ every constraint matchLow→ onlysql_typematches (constraints differ) Lower thanLowproduces no candidate.
§Out of scope (v1)
- Indexes (
CREATE INDEX) — not inCollectionContract. - Constraint-only changes (e.g.
NOT NULLadded with the same type) — folded intoTypeChangefor now. default_ttl_ms,context_index_fields,timestampsfromCreateTableQuery— ignored. Reserved for v2 once the correspondingAlterOperationvariants exist.- Constraint normalisation (e.g.
'foo'vsfoo) — best- effort string compare with leading/trailing-quote strip.
Structs§
- Diff
Summary - Per-category counters for the diff. Useful for dashboards and CI guardrails (“fail the build if a migration would drop more than N columns”).
- Rename
Candidate - Heuristic match between an unpaired
DropColumnand an unpairedAddColumn. Always advisory — clients decide. - Schema
Diff - Aggregate result of a column-level schema diff.
Enums§
- DiffOp
- One operation in a schema diff.
- Rename
Confidence
Functions§
- column_
equivalent - Returns true when a live
currentcolumn and a targetCreateColumnDefdescribe the same column shape. - compute_
column_ diff - Compute the column-level diff between a live
currentcontract and a target list ofCreateColumnDefs parsed from the embeddedCREATE TABLEstatement. - format_
as_ json - Format a
SchemaDiffas a structured JSON string. Hand- rolled emitter so this module stays free ofserde_json— reddb’s existingcrate::serde_jsonmodule provides the tiny JSON writer the rest of the codebase uses, but here we just produce text directly because the schema is small and stable. - format_
as_ sql - Format a
SchemaDiffas a copy-paste-friendly SQL string.