Skip to main content

MIGRATION_3

Constant MIGRATION_3 

pub const MIGRATION_3: &str = "-- Migration 3 \u{2014} write groups (#420), on the event log\'s lineage.\n--\n-- Which rows one `append`, or one ingested group with its compensation, wrote\n-- together, so a runner and a store peer can read the log back in the writes\n-- that made it (SY-39). `write_group` holds the lowest position the write put\n-- in, the same value on every row of it; `group_guard` holds the guard an\n-- ingested group arrived under, in `happenstance-sync`\'s stored form, on that\n-- first row only.\n--\n-- Additive, and idempotent for the reason migration 1 is: `IF NOT EXISTS`\n-- throughout. Both columns are nullable and carry no default, so adding them\n-- rewrites no row. A row written before this migration has `NULL` in both and\n-- reads as a group of its own, named by its position; nothing recorded which\n-- rows one earlier call wrote, so there is no backfill to run.\n--\n-- Apply it before deploying a build that writes these columns: such a build\'s\n-- `INSERT` names `write_group` on every row, and fails against a table without\n-- it.\n--\n-- The index serves the group read, which looks rows up by their group. Partial,\n-- because a row from before this migration is found by its position through\n-- the primary key and needs no entry. Built inside the migration\'s implicit\n-- transaction, so it holds a lock that blocks writes while it builds; on a\n-- large existing table, create it first with `CREATE INDEX CONCURRENTLY` under\n-- the same name, and `IF NOT EXISTS` below then leaves it alone.\n\nALTER TABLE event ADD COLUMN IF NOT EXISTS write_group bigint;\n\nALTER TABLE event ADD COLUMN IF NOT EXISTS group_guard bytea;\n\nCREATE INDEX IF NOT EXISTS event_write_group_idx\n    ON event (write_group)\n    WHERE write_group IS NOT NULL;\n";
Available on crate feature event-store and (crate features event-store or projection-store) only.
Expand description

The event log’s second migration, as SQL: the write-group columns and their index (#420).

write_group, on every row, the lowest position the write that put the row in gave out, and group_guard, on that first row only, the guard an ingested group arrived under. A runner and a store peer read the log back in whole writes through them (SY-39). Both nullable with no default, so adding them rewrites no row; a row from before this migration reads as a group of its own.

Apply it before deploying this build: every INSERT this build sends names write_group, and fails against a table without it. Idempotent, as MIGRATION_1 is. The index is built in the migration’s transaction; the file says how to build it CONCURRENTLY first on a large table.

Numbered 3 because that is the directory’s sequence: migrations/0002_projection_checkpoint.sql belongs to the other role.