Skip to content

Commit fcfafdc

Browse files
committed
fix(pocketbase/migrations): make probe_runs migration robust to partial applies
R2-A.11: two robustness improvements to 1777165230_create_probe_runs.js: - CREATE INDEX statements now use IF NOT EXISTS so a partial-apply doesn't trip the next migration run on the index DDL step. The collection-presence gate above already covers saveCollection idempotency; this covers the per-index path in case PB ever runs index DDL separately from the schema commit. - Document why the up-migration's findCollectionByNameOrId catch must remain broad: PB JSVM does not expose typed error discrimination (no ErrCollectionNotFound), so we cannot narrow without a runtime feature change. The down-migration's catch is already narrowed because it operates on a resolved-or-skip path.
1 parent 02ee62d commit fcfafdc

1 file changed

Lines changed: 18 additions & 3 deletions

File tree

showcase/pocketbase/pb_migrations/1777165230_create_probe_runs.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,20 @@ migrate(
3232
// Idempotency: re-running the migration after a partial apply (the
3333
// exact failure mode that motivated the 1776789100 reconcile pattern)
3434
// must be a no-op. Skip when the collection already exists.
35+
//
36+
// R2-A.11: PB JSVM does NOT expose typed error discrimination
37+
// (no ErrCollectionNotFound), so we have to catch broadly here.
38+
// The cost is low: a real permission/IO error against the dao
39+
// would fall through to createCollection, which would surface its
40+
// own error. The down-migration is narrowed (it operates on a
41+
// resolved-or-skip path).
3542
try {
3643
dao.findCollectionByNameOrId("probe_runs");
3744
return;
3845
} catch (e) {
39-
// Not present — fall through to create.
46+
// Not present (or PB JSVM threw something equivalent) — fall
47+
// through to create. We can't tighten further without typed
48+
// errors from the runtime.
4049
}
4150
const c = new Collection({
4251
name: "probe_runs",
@@ -78,10 +87,16 @@ migrate(
7887
// for probe X", served directly by this composite index without
7988
// a sort step. Mirrors the status_history pattern in
8089
// 1776789000.
81-
"CREATE INDEX idx_probe_runs_probe_started ON probe_runs (probe_id, started_at DESC)",
90+
//
91+
// R2-A.11: IF NOT EXISTS so a partial-apply on the indexes step
92+
// doesn't trip the next migration run. The collection-presence
93+
// gate above already covers the saveCollection idempotency; this
94+
// covers the per-index path in case PB ever runs index DDL
95+
// separately from the schema commit.
96+
"CREATE INDEX IF NOT EXISTS idx_probe_runs_probe_started ON probe_runs (probe_id, started_at DESC)",
8297
// Standalone started_at index covers cross-probe time-range
8398
// queries (retention sweeps, "last 24h across all probes").
84-
"CREATE INDEX idx_probe_runs_started ON probe_runs (started_at DESC)",
99+
"CREATE INDEX IF NOT EXISTS idx_probe_runs_started ON probe_runs (started_at DESC)",
85100
],
86101
// Public read mirrors `status` / `status_history` — the dashboard
87102
// pulls run history without an authenticated session. Writes stay

0 commit comments

Comments
 (0)