Skip to content

Commit 9eea4f0

Browse files
Ian Oberstclaude
andcommitted
test(db): assert events_pkey parity and run its checks on script edits
The admin parity test now runs the post-apply reconcile script on its desired-state probe, as the launchers do, and compares the primary key of events and its partitions with the migrated probe, per docs/database.md Rule 4. Add scripts/repair-events-pkey.sql and scripts/start-relay-for-tests.sh to the rust CI paths filter so edits to either run the drift test, and say in the repair script when each copy can be removed. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Ian Oberst <[email protected]>
1 parent 68161ab commit 9eea4f0

3 files changed

Lines changed: 44 additions & 3 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ jobs:
5353
- '.config/nextest.toml'
5454
- 'scripts/postgres-test-*.sh'
5555
- 'scripts/reconcile-schema-after-pgschema.sql'
56+
- 'scripts/repair-events-pkey.sql'
57+
- 'scripts/start-relay-for-tests.sh'
5658
- 'bin/pgschema'
5759
- 'bin/.pgschema-*.pkg'
5860
- 'scripts/check-postgres-test-discovery.py'

‎crates/buzz-db/src/runtime/migration.rs‎

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2733,7 +2733,11 @@ mod postgres_tests {
27332733
/// test-relay launchers take — and migrates another through 1–38, then
27342734
/// asserts the three admin tables have identical column definitions (name,
27352735
/// type, nullability, default) and identical index shapes, including each
2736-
/// key's catalog sort/null options (`pg_index.indoption`). Columns are keyed
2736+
/// key's catalog sort/null options (`pg_index.indoption`). It also asserts
2737+
/// `events` and its partitions carry the same primary key on both probes,
2738+
/// so the reconcile script's `events_pkey` rebuild cannot drift from the
2739+
/// migrations. Partitions are compared as a set of key definitions because
2740+
/// the two paths create different months. Columns are keyed
27372741
/// by name, not ordinal, because migrations append via `ALTER TABLE` while
27382742
/// `schema.sql` declares them inline — positions legitimately differ, shapes
27392743
/// must not.
@@ -2794,6 +2798,23 @@ mod postgres_tests {
27942798
.expect("read index shapes")
27952799
}
27962800

2801+
// Distinct (is the parent, key definition) pairs across events and its
2802+
// partitions.
2803+
async fn events_key_defs(pool: &PgPool) -> Vec<(bool, String)> {
2804+
sqlx::query_as(
2805+
"SELECT DISTINCT conrelid = 'events'::regclass, pg_get_constraintdef(oid) \
2806+
FROM pg_constraint \
2807+
WHERE contype = 'p' AND conrelid IN ( \
2808+
SELECT 'events'::regclass \
2809+
UNION ALL \
2810+
SELECT inhrelid FROM pg_inherits WHERE inhparent = 'events'::regclass) \
2811+
ORDER BY 1, 2",
2812+
)
2813+
.fetch_all(pool)
2814+
.await
2815+
.expect("read events key definitions")
2816+
}
2817+
27972818
let base_url = std::env::var("BUZZ_TEST_DATABASE_URL")
27982819
.or_else(|_| std::env::var("DATABASE_URL"))
27992820
.unwrap_or_else(|_| TEST_DB_URL.to_owned());
@@ -2814,8 +2835,8 @@ mod postgres_tests {
28142835
.await
28152836
.expect("create migrated probe database");
28162837

2817-
// Bootstrap the desired-state probe through the real pgschema binary, the
2818-
// same invocation the test-relay launchers use.
2838+
// Bootstrap the desired-state probe the way the test-relay launchers do:
2839+
// the real pgschema binary, then the post-apply reconcile script.
28192840
let apply = pgschema(&conn, &desired_db, &["apply", "--auto-approve"]);
28202841
assert!(
28212842
apply.status.success(),
@@ -2827,6 +2848,12 @@ mod postgres_tests {
28272848
let desired = PgPool::connect(&format!("{base_prefix}/{desired_db}"))
28282849
.await
28292850
.expect("connect desired-state probe database");
2851+
sqlx::raw_sql(include_str!(
2852+
"../../../../scripts/reconcile-schema-after-pgschema.sql"
2853+
))
2854+
.execute(&desired)
2855+
.await
2856+
.expect("post-apply reconciliation");
28302857
let migrated = PgPool::connect(&format!("{base_prefix}/{migrated_db}"))
28312858
.await
28322859
.expect("connect migrated probe database");
@@ -2855,6 +2882,13 @@ mod postgres_tests {
28552882
migration and schema.sql must both use a representable shape."
28562883
);
28572884
}
2885+
assert_eq!(
2886+
events_key_defs(&desired).await,
2887+
events_key_defs(&migrated).await,
2888+
"events primary-key parity mismatch: the pgschema-bootstrapped, reconciled \
2889+
desired state has drifted from the migrations; update the events_pkey block in \
2890+
scripts/reconcile-schema-after-pgschema.sql and scripts/repair-events-pkey.sql"
2891+
);
28582892

28592893
desired.close().await;
28602894
migrated.close().await;

‎scripts/repair-events-pkey.sql‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
-- script can run, so scripts/start-relay-for-tests.sh, the one launcher that
1010
-- reuses its database, runs this file before pgschema apply. It does nothing on
1111
-- an empty database or one whose key is already in order.
12+
--
13+
-- Both copies are temporary. The reconcile block, and the test that keeps the
14+
-- copies identical, can go once pgschema creates the key in declared order
15+
-- (1.13.3 does). This file and the launcher step can go once no local database
16+
-- reconciled before the reconcile block existed remains.
1217

1318
DO $$
1419
DECLARE

0 commit comments

Comments
 (0)