Referencia · §8.34

Migrations

migrations, nucleus migrate, §M14.16 M2

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

A migration is a file <version>_<name>.sql in a directory (migrations by default, --dir): <version> a whole number — nucleus migrate new <name> makes the file with the time as its version, YYYYMMDDHHMMSS UTC, and both directions in it, as dbmate’s:

-- migrate:up
CREATE TABLE notes (id int PRIMARY KEY, body text);
CREATE INDEX notes_body ON notes USING text (body);
-- migrate:down
DROP TABLE notes;

nucleus migrate up applies the pending ones in version order (--to v: up to and with v): each runs as BEGIN; <up>; RECORD MIGRATION v NAME 'name' CHECKSUM 'sha256'; COMMIT — DDL is transactional, so a migration that fails leaves nothing, neither its first statements nor its record. A direction marked transaction:false (-- migrate:up transaction:false) runs its statements one by one, then its record — for those that cannot be in a block (CREATE BRANCH, ALTER TABLE … SET STORAGE, VACUUM). down reverts the newest applied one (--to v: each above v; --all), each as BEGIN; <down>; FORGET MIGRATION v; COMMIT. status lists each one: applied (when), pending, CHANGED since applied, or MISSING its file. Before up or down the files are checked against the records: an applied file changed since (its SHA-256, line ends as \n) is refused — a change goes into a new migration —, as is a pending file older than the newest applied one (out of order) and a record whose file is gone. The database is a directory, :memory: or a server (-h, -U, … as nucleus sql).

The records are the catalog’s (00 1B <version>), so they commit with the change, travel with the database (backups, branches: a branch’s migrations are its own) and are nc_catalog.migrations (version, name, checksum, applied_at, applied_by). RECORD MIGRATION v NAME 'n' CHECKSUM 'c' (42710 if recorded) and FORGET MIGRATION v (42704 if not) are SQL any client may send, with the right to create in the database — another tool can keep them. Two migrators at once: both write the record’s key, so one commits and the other’s transaction conflicts (40001) or finds the record (42710); nucleus migrate then reads the records again and goes on if the same file was applied.