fix(migration): use PERFORM for add_continuous_aggregate_policy inside DO block#183
Conversation
…e DO block Bare SELECT inside a PL/pgSQL DO block is invalid when the result is discarded — Postgres raises 42601 "query has no destination for result data". Switch the four add_continuous_aggregate_policy calls to PERFORM so the migration applies cleanly on TimescaleDB. Same fix as #182, applied to the next migration in the sequence.
Greptile SummaryFixes the PL/pgSQL syntax error in the Confidence Score: 5/5Safe to merge — targeted four-line fix with no functional side effects and correct idempotency preserved. All four affected lines are correctly updated, the fix exactly matches the stated intent, no logic was altered beyond the keyword change, and the migration remains idempotent and guarded for non-TimescaleDB environments. No files require special attention. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Migration DO block starts] --> B{timescaledb extension exists?}
B -- No --> C[RAISE NOTICE: skipping]
B -- Yes --> D[CREATE MATERIALIZED VIEW pipeline_metrics_1m IF NOT EXISTS]
D --> E[PERFORM add_continuous_aggregate_policy pipeline_metrics_1m]
E --> F[CREATE MATERIALIZED VIEW pipeline_metrics_1h IF NOT EXISTS]
F --> G[PERFORM add_continuous_aggregate_policy pipeline_metrics_1h]
G --> H[CREATE MATERIALIZED VIEW node_metrics_1m IF NOT EXISTS]
H --> I[PERFORM add_continuous_aggregate_policy node_metrics_1m]
I --> J[CREATE MATERIALIZED VIEW node_metrics_1h IF NOT EXISTS]
J --> K[PERFORM add_continuous_aggregate_policy node_metrics_1h]
K --> L[RAISE NOTICE: success]
style E fill:#90EE90
style G fill:#90EE90
style I fill:#90EE90
style K fill:#90EE90
Reviews (1): Last reviewed commit: "fix(migration): use PERFORM for add_cont..." | Re-trigger Greptile |
Summary
Direct follow-up to #182 — same bug class, next migration in the sequence.
After #182 unblocked
20260329000001_timescaledb_compression, the next migration20260329000002_timescaledb_continuous_aggregatesfailed againsttimescale/timescaledb:2.16.0-pg16with the same PL/pgSQL syntax error:Inside a PL/pgSQL
DOblock, a bareSELECTis illegal unless its result is captured (SELECT ... INTO). To discard the result, you must usePERFORM. The fouradd_continuous_aggregate_policy(...)calls in this migration useSELECT, so the very first one aborts the whole block.The four
CREATE MATERIALIZED VIEW ... WITH (timescaledb.continuous)statements in the same block are fine — only theadd_continuous_aggregate_policycalls need fixing.Fix
Four-line change. Each
SELECT add_continuous_aggregate_policy(becomesPERFORM add_continuous_aggregate_policy(. View names, intervals, options, indentation untouched.Sweep status
I (well — the prompt author) checked the rest of
prisma/migrations/for any other bareSELECTof a TimescaleDB management function (add_compression_policy,add_continuous_aggregate_policy,add_retention_policy,add_reorder_policy,add_job,alter_job,create_hypertable,drop_chunks,reorder_chunk,refresh_continuous_aggregate) inside aDOblock. These four are the last remaining instances. After this PR merges, the migration set should apply cleanly on a fresh TimescaleDB database.Why this didn't surface earlier
Same as #179 / #180 / #181 / #182: every prior deployment ran on plain Postgres, where the
IF EXISTS (... timescaledb)guard makes this whole migration a no-op. Now that #182 cleared the previous migration under TimescaleDB, this one is next in line.Recovery instructions
For anyone whose DB is currently in the failed state on this migration:
Option A — wipe and reapply (cleanest if no real data, e.g. demo):
Option B — preserve data:
Drift warning for plain-Postgres users
Same caveat as the prior PRs: the migration's checksum changes. On next
prisma migrate deployyou may see a "migration was modified after applied" warning. Resolve with:One-time fix; the no-op behaviour on plain Postgres is unchanged.
Test plan
SELECT add_continuous_aggregate_policy(→PERFORM add_continuous_aggregate_policy(docker compose upagainsttimescale/timescaledb:2.16.0-pg16—20260329000002applies cleanly through to completionpipeline_metrics_1m,pipeline_metrics_1h,node_metrics_1m,node_metrics_1h).Prior PRs in this series
create_hypertableidincompress_orderbyto satisfy unique-index constraintcompress_segmentby/compress_orderbyoption stringsPERFORMforadd_compression_policyin the previous migration (_20260329000001_)