Skip to main content

MIGRATION_1

Constant MIGRATION_1 

pub const MIGRATION_1: &str = "-- Migration 1 \u{2014} the event log, as Neon\'s `/sql` endpoint has to receive it.\n--\n-- This is NOT `crates/happenstance-postgres/migrations/0001_event_log.sql`\n-- copied. It carries the same columns, the same indexes and the same reasoning,\n-- and it differs in the two ways this endpoint forces:\n--\n-- 1. **Every identifier is a placeholder**, rendered by `crate::migration`.\n--    The Postgres adapter hard-codes unqualified `event`, `event_position_seq`\n--    and `store_meta` and leans on a pooled `search_path` to place them. There\n--    is no session here to hold a `search_path` in, and the `/sql` proxy\n--    discards `options=-c search_path=\u{2026}` from the connection string \u{2014} measured,\n--    `SHOW search_path` still answers `\"$user\", public`. So a name that is not\n--    written out is a name resolved against whatever backend the proxy picked,\n--    and the fixture\'s one-schema-per-instance isolation would be a lie. Every\n--    `@\u{2026}@` below is substituted with an already-quoted, already-qualified\n--    identifier by `NeonConfig`.\n--\n-- 2. **One statement per statement.** The endpoint\'s `query` field takes exactly\n--    one; a multi-statement string is refused with `Database request failed`\n--    and no detail. `crate::migration` splits this file on its statement\n--    terminators and sends the pieces as one non-interactive batch, so the whole\n--    migration is still one round trip and still atomic.\n--\n-- Idempotent (`IF NOT EXISTS` throughout) for the reason the Postgres file\n-- gives: one fixture instance is connected to more than once, and a migration\n-- that fails on its second application reports a broken environment as an\n-- adapter defect.\n--\n-- The visibility mechanism is `xid8` + `pg_snapshot_xmin`, inherited from\n-- ADR-0024 and RE-MEASURED here rather than assumed, because that ADR\'s numbers\n-- were taken against a dedicated container and this is a shared Neon branch with\n-- a pooler and Neon\'s own compute processes on it. Ten append-then-read cycles\n-- against the live endpoint: the frontier had already passed the appended\n-- position on the FIRST read every time, ten of ten, with the read landing\n-- 80-100 ms after the append\'s commit. The mechanism is bought here, not\n-- borrowed.\n--\n-- `position` still carries NO column default. The sequence is real and is read\n-- explicitly by the INSERT, which keeps `information_schema` reporting\n-- `column_default IS NULL` and `is_identity = \'NO\'` \u{2014} so \"position is not\n-- bigserial\" stays a checkable fact rather than a turn of phrase.\n\nCREATE SCHEMA IF NOT EXISTS @schema@;\n\nCREATE TABLE IF NOT EXISTS @event@ (\n    -- Deliberately not `bigserial`, and the distinction is not cosmetic even\n    -- though the allocator is the same. `nextval()` DOES allocate outside the\n    -- transaction, and that IS where position order stops being commit order.\n    -- The frontier predicate on the read side is what answers it; keeping the\n    -- default off the column is what makes the allocation site explicit at the\n    -- one INSERT that performs it, rather than implicit in the DDL.\n    position        bigint  PRIMARY KEY,\n    event_type      text    NOT NULL,\n    -- Payloads are opaque bytes. The contract crate never sees them decoded\n    -- (ADR-0003), so neither does this schema. On the wire they are base64\n    -- rather than `\\x` hex: both survive the round trip, and base64 costs 33%\n    -- expansion against `MAX_RESPONSE_BYTES` where hex costs 100%.\n    data            bytea   NOT NULL,\n    -- Nullable on purpose: `None` and `Some(empty)` are different values and the\n    -- suite has a rule that says so. A `NOT NULL DEFAULT \'\'` here would collapse\n    -- them and pass every other rule.\n    metadata        bytea,\n    -- `Tags` is canonically sorted, which is what keeps the containment operator\n    -- `@>` available against the GIN index below.\n    tags            text[]  NOT NULL,\n    -- The two halves of `EventId`, which is `(StoreId, SequencePosition)`.\n    -- `StoreId` is `[u8; 16]`, and the CHECK is what makes that a fact about the\n    -- table rather than a convention in the encoder. Nullable because a\n    -- replication ingest may hold rows minted elsewhere before this store stamps\n    -- its own.\n    origin_store    bytea   CONSTRAINT event_origin_store_is_16_bytes\n                            CHECK (origin_store IS NULL OR octet_length(origin_store) = 16),\n    origin_position bigint,\n    -- Milliseconds since the epoch, and signed because `RecordedAt` is `i64` and\n    -- admits times *before* it. A `timestamptz` column would be a lossy\n    -- conversion the port never asked for.\n    recorded_at     bigint  NOT NULL,\n    -- The mechanism. The appending transaction stamps its own id, and a reader\n    -- admits only rows beneath `pg_snapshot_xmin(pg_current_snapshot())` \u{2014} the\n    -- frontier below which no transaction can still be in flight. Writers never\n    -- serialise; the cost is paid on the read side and it is structural rather\n    -- than incremental: `head` reports a frontier rather than a maximum, and\n    -- read-your-own-writes is not promised.\n    --\n    -- A DEFAULT rather than an explicit value in the INSERT, and the asymmetry\n    -- with `position` above is deliberate: `pg_current_xact_id()` must be\n    -- evaluated by the writing transaction itself, so a default is the one\n    -- spelling that cannot be got wrong by a caller assembling the statement.\n    xact_id         xid8    NOT NULL DEFAULT pg_current_xact_id()\n);\n\n-- Read explicitly by the INSERT, never as a column default. `OWNED BY` ties its\n-- lifetime to the column so a dropped table does not leave it behind.\nCREATE SEQUENCE IF NOT EXISTS @sequence@ OWNED BY @event@.position;\n\n-- Tag matching. `text[]` + GIN, as in the Postgres adapter and for the same\n-- reason: `@>` expresses a query item\'s \"tags are AND, and a superset matches\"\n-- directly rather than as a self-join per tag.\n--\n-- It carries a second load here that it does not carry there. A conditional\n-- append is `SERIALIZABLE`, so its guard read has to be predicate-locked for SSI\n-- to see the write-skew at all; Postgres has supported predicate locking on GIN\n-- since 11, and the server behind this endpoint is 18.6.\nCREATE INDEX IF NOT EXISTS @tags_idx@ ON @event@ USING gin (tags);\n\n-- Type-and-position, in that order, so a query item that names types is a range\n-- scan rather than a filter over one.\nCREATE INDEX IF NOT EXISTS @type_idx@ ON @event@ (event_type, position);\n\n-- One store never mints one `EventId` twice, and this is where that is a fact\n-- rather than an intention: `event_ids_are_unique_within_a_store` is a\n-- conformance rule, and an adapter that enforces it only in Rust enforces it\n-- only until two requests race \u{2014} which here is every request, because there is\n-- no session and nothing is serialised anywhere. A partial index because the\n-- pair is nullable until a row is stamped, and NULLs do not collide in a plain\n-- unique index.\nCREATE UNIQUE INDEX IF NOT EXISTS @origin_idx@\n    ON @event@ (origin_store, origin_position)\n    WHERE origin_store IS NOT NULL AND origin_position IS NOT NULL;\n\n-- The frontier predicate\'s own index. Without it every read is a sequential\n-- scan, which would charge the mechanism for the absence of an index rather than\n-- for what it actually costs.\nCREATE INDEX IF NOT EXISTS @xact_idx@ ON @event@ (xact_id);\n\n-- The store\'s own identity, which is the `StoreId` half of every `EventId` this\n-- store mints. One row, inserted once and never updated by the adapter.\n--\n-- `gen_random_uuid()` rather than an extension: it is core since PostgreSQL 13\n-- and gives exactly the sixteen bytes `StoreId` holds, so `uuid_send` is a\n-- reinterpretation rather than a conversion. `ON CONFLICT DO NOTHING` makes the\n-- insert idempotent, which is what lets two handles race a first `connect()` and\n-- both end up agreeing about who this store is.\nCREATE TABLE IF NOT EXISTS @meta@ (\n    k text  PRIMARY KEY,\n    v bytea NOT NULL\n);\n\nINSERT INTO @meta@ (k, v)\nVALUES (\'store_id\', uuid_send(gen_random_uuid()))\nON CONFLICT (k) DO NOTHING;\n";
Expand description

The event log, before its identifiers are filled in.