Referencia · §8.36

Dry runs and schema diffs

schema_diff, §M14.16 M4

Ez az oldal a nucleus saját dokumentációjából készül, ezért angol nyelvű.

nucleus migrate up --dry-run (and down --dry-run) makes a branch of the database (migrate_dry_run_<pid>, §M14.13), applies the migrations there — each with its time, a failing one with its error — prints how the schema would change (the definitions gone, -, and new, +) and drops the branch however it went: the database is untouched, and the run met its real data (a SET NOT NULL over rows with NULLs, a unique index over duplicates fail as they would). Over the network the branch is reached as a database of its name.

nucleus migrate diff replays every migration’s up into a database in memory, replays the database’s definitions into another, and prints the statements from the first schema to the second (nucleus_sql::schema_diff); every file must be applied first (else the diff would undo the pending ones). --new <name> writes them as the next migration, its down the diff the other way, and records it as applied — the database has the change already. The diff compares the definitions (§8.33) by kind, name and table, and a table both have by its catalog structure: what only the old schema has is dropped, what only the new has is created, what changed is dropped and created again — except what changes in place: a table’s columns (ADD, DROP, ALTER COLUMN … TYPE, SET / DROP DEFAULT, SET / DROP NOT NULL, an identity’s ADD GENERATED, SET GENERATED, SET options and DROP IDENTITY) and constraints, an enum’s new labels (ADD VALUE … BEFORE), an ordering’s mapping, a sequence’s options (ALTER SEQUENCE … AS … with every option, its value kept), a table’s settings (storage, expiry, subject, row security). The order: what depends on tables dropped (foreign keys always explicitly, so tables that reference each other drop in any order), the tables’ columns and constraints that go, the types and models nothing uses any more; then models, types and sequences made, tables, new columns and changes, the sequences no default calls any more dropped, and last indexes, foreign keys, sequences’ owners (ALTER SEQUENCE … OWNED BY), comments, settings and policies. What a diff cannot see or must not do is a -- note, not a statement: a column or table that may have been renamed (a drop and an add with the same type or columns: the note gives the RENAME), a changed primary key or partitioning, a partition, memory store or chunk table whose statement changed (dropping it would lose its rows), a column order ADD COLUMN cannot give. The law, over every script of the test kit: the diff from an empty database makes the script’s schema, and the diff back leaves an empty one (crates/sql/tests/schema_diff.rs).