Move the database to Postgres #8

Merged
niklas merged 1 commits from postgres into main 2026-09-20 08:54:24 +00:00
Owner

Closes #2. First step of #1: the teams work in #4 adds a team_id to nearly every table, and doing that against two databases is work nobody gets paid for.

The commit message carries the full reasoning. The short version:

Schema

  • The ten SQLite migrations become one Postgres baseline, 001_baseline.sql. They were incremental in a way that has no value on a fresh install — 004 adds columns 008 drops again, and 008's backfill rewrites data a Postgres database never had. The history stays in git.
  • Timestamps stay BIGINT unix seconds. Converting to timestamptz is a larger change and deserves its own PR.
  • The JSON columns become jsonb, because #4 will want to filter and index on labels.

Beyond the mechanical port

170 placeholders moved from ? to $1, but four things needed more than a search and replace:

  • Dynamic WHERE clauses hand out placeholders through a sqlArgs helper, so numbering cannot drift from the values.
  • SUM(resolved_at IS NULL) was SQLite counting a boolean as 0/1. Postgres has no sum(boolean), and this was breaking every dead man's switch — silently, since the sweeper only logs. Now COUNT(*) FILTER.
  • unixepoch() → FLOOR(EXTRACT(EPOCH FROM now()))::bigint. The FLOOR is load-bearing: a bare cast rounds half up, putting a timestamp a second in the future.
  • The unique-violation check matched SQLite's error text; it matches SQLSTATE 23505 now.

Tests

There is no in-memory Postgres, so each test gets its own schema on a shared server. TERDUT_TEST_DSN says where; make test-db starts one locally and ci.yaml runs one as a service container. An unset DSN fails rather than skips.

TestMigration_BackfillCarriesAckAndComments is deleted with the migrations it replayed. What it protected now belongs to scripts/sqlite-to-postgres.go.

Chart

Loses the PVC, the data volume and the python backup sidecar; requires database.dsnSecret.name, since it provisions no database and a render without it should fail.

Verified

  • make fmt lint test helm-lint green, with -race, against Postgres 17.
  • govulncheck and gitleaks clean.
  • The migration script run end to end against a SQLite database built at the old schema and seeded in every table. Ids survive, so incidents keep their numbers and every foreign key still points where it did. A webhook after the migration opened incident 12 rather than colliding at 1.

Not in here

  • The wrapper chart in Ryuvia/charts: the postgresql CR, the k8up pg_dump annotation, the schedule moved off the SQLite dump, the network policy.
  • The cutover for the running install: stop, run the script against the PVC, start on the new version. It is ordered and cannot ride along with a normal release.
Closes #2. First step of #1: the teams work in #4 adds a `team_id` to nearly every table, and doing that against two databases is work nobody gets paid for. The commit message carries the full reasoning. The short version: ### Schema - The ten SQLite migrations become one Postgres baseline, `001_baseline.sql`. They were incremental in a way that has no value on a fresh install — 004 adds columns 008 drops again, and 008's backfill rewrites data a Postgres database never had. The history stays in git. - Timestamps stay `BIGINT` unix seconds. Converting to `timestamptz` is a larger change and deserves its own PR. - The JSON columns become `jsonb`, because #4 will want to filter and index on labels. ### Beyond the mechanical port 170 placeholders moved from `?` to `$1`, but four things needed more than a search and replace: - Dynamic `WHERE` clauses hand out placeholders through a `sqlArgs` helper, so numbering cannot drift from the values. - `SUM(resolved_at IS NULL)` was SQLite counting a boolean as 0/1. Postgres has no `sum(boolean)`, and this was breaking **every dead man's switch** — silently, since the sweeper only logs. Now `COUNT(*) FILTER`. - `unixepoch()` → `FLOOR(EXTRACT(EPOCH FROM now()))::bigint`. The `FLOOR` is load-bearing: a bare cast rounds half up, putting a timestamp a second in the future. - The unique-violation check matched SQLite's error text; it matches SQLSTATE 23505 now. ### Tests There is no in-memory Postgres, so each test gets its own schema on a shared server. `TERDUT_TEST_DSN` says where; `make test-db` starts one locally and `ci.yaml` runs one as a service container. An unset DSN **fails** rather than skips. `TestMigration_BackfillCarriesAckAndComments` is deleted with the migrations it replayed. What it protected now belongs to `scripts/sqlite-to-postgres.go`. ### Chart Loses the PVC, the data volume and the python backup sidecar; requires `database.dsnSecret.name`, since it provisions no database and a render without it should fail. ### Verified - `make fmt lint test helm-lint` green, with `-race`, against Postgres 17. - `govulncheck` and `gitleaks` clean. - The migration script run end to end against a SQLite database built at the old schema and seeded in every table. Ids survive, so incidents keep their numbers and every foreign key still points where it did. A webhook after the migration opened incident 12 rather than colliding at 1. ### Not in here - **The wrapper chart** in `Ryuvia/charts`: the `postgresql` CR, the k8up `pg_dump` annotation, the schedule moved off the SQLite dump, the network policy. - **The cutover** for the running install: stop, run the script against the PVC, start on the new version. It is ordered and cannot ride along with a normal release.
niklas added 1 commit 2026-09-20 08:48:13 +00:00
Move the database to Postgres, before teams need the schema
CI / chart (pull_request) Successful in 1s
CI / security (pull_request) Successful in 17s
CI / test (pull_request) Successful in 2m5s
dc39e3a5d3
First step of #1, and it goes first for one reason: #4 adds a team_id to
nearly every table, and doing that twice -- once for SQLite, once for
Postgres -- is work nobody gets paid for. The teams migrations now only
have to be written against one database.

The ten SQLite migrations are replaced by a single Postgres baseline
rather than ported one by one. They were incremental in a way that has
no value on a fresh install: 004 adds columns 008 drops again, and 008's
backfill rewrites data a Postgres database never had. The history stays
in git; the schema they add up to is now 001_baseline.sql.

Timestamps stay BIGINT unix seconds and are NOT converted to timestamptz.
Everything in Go already speaks epochs, so converting would have been a
second, larger change riding along inside this one. It is worth doing on
its own. The JSON columns did move to jsonb, because #4 will want to
filter and index on labels.

Most of the port is mechanical -- 170 placeholders from ? to $1 -- but
four things needed more than a search and replace:

  * Dynamically built WHERE clauses cannot keep their numbering straight
    by hand, so they hand out placeholders through sqlArgs instead. A
    filter can now be added or reordered without renumbering anything.

  * SUM(resolved_at IS NULL) was SQLite counting a boolean as 0 or 1.
    Postgres has no sum(boolean), and this was breaking every dead man's
    switch -- silently, since the sweeper only logs. Now COUNT(*) FILTER.

  * unixepoch() became FLOOR(EXTRACT(EPOCH FROM now()))::bigint. The
    FLOOR is load-bearing: a bare cast rounds half up, so a row written
    at .6 of a second claimed a timestamp a second in the future and
    disagreed with the time.Now().Unix() the Go side stamps.

  * The unique-violation check matched SQLite's error text. It matches
    SQLSTATE 23505 now, so a renamed constraint cannot turn a 409 back
    into a 500.

Tests need a real Postgres, because there is no in-memory Postgres the
way there was an in-memory SQLite. Each test gets its own schema on a
shared server -- cheaper than a database each, and still isolated.
TERDUT_TEST_DSN says where it is; `make test-db` starts one locally and
ci.yaml runs one as a service container. An unset DSN fails the suite
rather than skipping it: a run that quietly tests nothing is worse than
one that does not run.

TestMigration_BackfillCarriesAckAndComments is deleted along with the
migrations it replayed. What it protected -- an upgrade not losing
acknowledgements and comments -- now belongs to scripts/sqlite-to-postgres.go,
which is build-tagged so the SQLite driver stays out of the server
binary. Both are meant to be deleted once this install has migrated.

The chart loses the PVC, the data volume and the python backup sidecar,
and requires database.dsnSecret.name: it provisions no database and
cannot guess where the credentials live, so a render without it is meant
to fail. Backups move to where Postgres actually runs. The other half of
that -- the postgresql CR, the k8up pg_dump annotation and the network
policy -- is a change to the wrapper chart in Ryuvia/charts and is not in
here.

Verified rather than assumed: the gate is green with -race against
Postgres 17, govulncheck and gitleaks are clean, and the migration script
was run end to end against a SQLite database built at the old schema and
seeded in every table. Ids survive, so incidents keep their numbers and
every foreign key still points where it did; the identity sequences are
moved past the copied ids, and a webhook after the migration opened
incident 12 rather than colliding at 1.
niklas merged commit 21f0eec807 into main 2026-09-20 08:54:24 +00:00
Sign in to join this conversation.