Skip to content

Commit f6aecb1

Browse files
Ian Oberstclaude
andcommitted
test(db): exercise the advance in the pgschema re-apply test
Address review on the runtime-partition re-apply test: - Run scripts/reconcile-schema-after-pgschema.sql after apply, as every pgschema caller does. Without it the declared children stay detached and the maintenance pass only creates months, never advancing a catch-all. Assert both catch-alls are attached, then that the pass moved their bounds and created each parent's 2026_07 monthly. - Assert the plan mentions no runtime monthly, not only that it has no DROP TABLE. - Tolerate a missing trailing comma when reading .pgschemaignore's `!` exemptions. - AGENTS.md: pgschema must run from the repository root so it reads .pgschemaignore. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Ian Oberst <[email protected]>
1 parent f87fdb0 commit f6aecb1

2 files changed

Lines changed: 51 additions & 10 deletions

File tree

‎AGENTS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ description. See [PR #803](https://github.com/block/buzz/pull/803).
602602
4. **Worktrees: `cd` in the same command** — shell CWD doesn't persist between tool calls. Use `cd /path && cargo build` as one command.
603603
5. **Desktop crate excluded from root workspace** — `cargo test` at repo root does NOT run desktop tests. Use `cargo test --manifest-path desktop/src-tauri/Cargo.toml` explicitly.
604604
6. **React render perf: `React.memo` is all-or-nothing** — it only skips a re-render when *every* prop is reference-stable; one unstable prop (inline arrow/JSX, or a hook returning a fresh `{}`/`[]`/`Map` each render) defeats it. Two repeat offenders: (a) React Query results (`useMutation`/`useQuery`) are a **new object each render** — depend on the stable method (`mutation.mutateAsync`), not the object; (b) derived `Map`/array state that recomputes on a version bump — wrap in a content-equality ref cache (`shared/hooks/useStableReference.ts`). When chasing interaction lag, **measure with DevTools closed and no perf probes** (an open Web Inspector + per-keystroke `console.log` inflate the numbers), and isolate by removing one suspect at a time rather than guessing.
605-
7. **`pgschema` omits seed DML and some storage parameters** — `schema/schema.sql` is an ordered `\i` manifest: `schema/tables/public/<table>.sql` holds only that table's `CREATE TABLE` and `CREATE INDEX` statements (one table per file, the shape SchemaBot reads), and types, functions, partitions, triggers, and seed rows live in the sibling `schema/` directories. Fresh desired-state bootstraps use `./bin/pgschema apply`, which does not execute `INSERT` statements or preserve every table storage parameter from `schema/schema.sql`. Put each unsupported invariant in `scripts/reconcile-schema-after-pgschema.sql` as an idempotent convergence statement plus a live catalog or data assertion. Every `pgschema apply` caller must run that script. A string assertion against `schema.sql` alone does not prove the pgschema-created database has the intended state.
605+
7. **`pgschema` omits seed DML and some storage parameters** — `schema/schema.sql` is an ordered `\i` manifest: `schema/tables/public/<table>.sql` holds only that table's `CREATE TABLE` and `CREATE INDEX` statements (one table per file, the shape SchemaBot reads), and types, functions, partitions, triggers, and seed rows live in the sibling `schema/` directories. Fresh desired-state bootstraps use `./bin/pgschema apply`, which does not execute `INSERT` statements or preserve every table storage parameter from `schema/schema.sql`. Put each unsupported invariant in `scripts/reconcile-schema-after-pgschema.sql` as an idempotent convergence statement plus a live catalog or data assertion. Every `pgschema apply` caller must run that script, and must run pgschema from the repository root so it reads `.pgschemaignore`; from any other directory a re-apply plans to drop the partitions the relay created at runtime. A string assertion against `schema.sql` alone does not prove the pgschema-created database has the intended state.
606606

607607
---
608608

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

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,7 +1583,12 @@ mod postgres_tests {
15831583
.expect("read .pgschemaignore");
15841584
let exempt: BTreeSet<String> = ignore
15851585
.lines()
1586-
.filter_map(|line| line.trim().strip_prefix("\"!")?.strip_suffix("\","))
1586+
.filter_map(|line| {
1587+
line.trim()
1588+
.trim_end_matches(',')
1589+
.strip_prefix("\"!")?
1590+
.strip_suffix('"')
1591+
})
15871592
.map(str::to_owned)
15881593
.collect();
15891594
assert_eq!(
@@ -2832,11 +2837,12 @@ mod postgres_tests {
28322837
}
28332838
}
28342839

2835-
/// pgschema does not compare partition bounds, so re-applying
2836-
/// `schema/schema.sql` to a database a relay has used plans
2840+
/// Re-applying `schema/schema.sql` to a database a relay has used plans
28372841
/// `DROP TABLE IF EXISTS ... CASCADE` for every monthly the partition
28382842
/// manager created at runtime, and their rows go with them.
2839-
/// `.pgschemaignore` keeps those monthlies out of pgschema's view.
2843+
/// `.pgschemaignore` keeps those monthlies out of pgschema's view, and
2844+
/// pgschema does not compare partition bounds, so the advanced catch-all
2845+
/// is left alone too.
28402846
#[tokio::test]
28412847
#[ignore = "requires Postgres"]
28422848
async fn pgschema_reapply_keeps_runtime_partitions() {
@@ -2865,11 +2871,34 @@ mod postgres_tests {
28652871
String::from_utf8_lossy(&apply.stderr),
28662872
);
28672873

2868-
// The relay's default startup pass, which advances the static layout's
2869-
// catch-all into dated monthlies schema/partitions/ does not declare.
28702874
let pool = PgPool::connect(&format!("{base_prefix}/{database}"))
28712875
.await
28722876
.expect("connect probe database");
2877+
// Every pgschema caller attaches the declared children this way.
2878+
sqlx::raw_sql(include_str!(
2879+
"../../../../scripts/reconcile-schema-after-pgschema.sql"
2880+
))
2881+
.execute(&pool)
2882+
.await
2883+
.expect("reconcile schema after pgschema");
2884+
async fn catch_all_bounds(pool: &PgPool) -> Vec<(String, Option<String>)> {
2885+
sqlx::query_as(
2886+
"SELECT relname::text, pg_get_expr(relpartbound, oid) FROM pg_class \
2887+
WHERE relname IN ('events_p_future', 'delivery_log_p_future') \
2888+
ORDER BY relname",
2889+
)
2890+
.fetch_all(pool)
2891+
.await
2892+
.expect("read catch-all bounds")
2893+
}
2894+
let declared_bounds = catch_all_bounds(&pool).await;
2895+
assert!(
2896+
declared_bounds.len() == 2 && declared_bounds.iter().all(|(_, bound)| bound.is_some()),
2897+
"reconcile did not attach both catch-alls: {declared_bounds:?}"
2898+
);
2899+
2900+
// The relay's default startup pass advances each catch-all into dated
2901+
// monthlies schema/partitions/ does not declare.
28732902
maintain_partitions(
28742903
&pool,
28752904
PARTITION_MANAGER_MONTHS_AHEAD,
@@ -2880,6 +2909,10 @@ mod postgres_tests {
28802909
)
28812910
.await
28822911
.expect("maintain partitions");
2912+
let advanced_bounds = catch_all_bounds(&pool).await;
2913+
for (declared, advanced) in declared_bounds.iter().zip(&advanced_bounds) {
2914+
assert_ne!(declared, advanced, "catch-all did not advance");
2915+
}
28832916
let children: Vec<(String, String)> = sqlx::query_as(
28842917
"SELECT p.relname::text, c.relname::text FROM pg_inherits i \
28852918
JOIN pg_class c ON c.oid = i.inhrelid \
@@ -2898,9 +2931,11 @@ mod postgres_tests {
28982931
})
28992932
.collect();
29002933
for parent in ["events", "delivery_log"] {
2934+
// The first month the declared catch-all served.
2935+
let first = format!("{parent}_p2026_07");
29012936
assert!(
2902-
runtime.iter().any(|(p, _)| p == parent),
2903-
"maintenance created no runtime {parent} monthly: {children:?}"
2937+
runtime.iter().any(|(_, child)| *child == first),
2938+
"maintenance did not advance into {first}: {children:?}"
29042939
);
29052940
}
29062941

@@ -2913,8 +2948,14 @@ mod postgres_tests {
29132948
);
29142949
assert!(
29152950
!plan_sql.contains("DROP TABLE"),
2916-
"re-applying schema.sql would drop tables; runtime monthlies {runtime:?}:\n{plan_sql}"
2951+
"re-applying schema.sql would drop tables:\n{plan_sql}"
29172952
);
2953+
for (_, child) in &runtime {
2954+
assert!(
2955+
!plan_sql.contains(child.as_str()),
2956+
"re-applying schema.sql would change runtime monthly {child}:\n{plan_sql}"
2957+
);
2958+
}
29182959

29192960
sqlx::query(AssertSqlSafe(format!(
29202961
"DROP DATABASE {database} WITH (FORCE)"

0 commit comments

Comments
 (0)