From f07e66d02f575c799033686e9ac596397767cb92 Mon Sep 17 00:00:00 2001 From: andyst-dev <150129844+andyst-dev@users.noreply.github.com> Date: Mon, 28 Sep 2026 09:09:55 +0200 Subject: [PATCH 1/2] fix(db): apply the retention swap migrations on Postgres 17 (#0122, #0125) `setup.local.sh` runs `db:migrate` against the `postgres:17` image the compose file pins, and migrations 0122 and 0125 rename `*_not_null` constraints that Postgres only catalogues from version 18 on. The rename therefore fails with "constraint does not exist" and the whole run rolls back, so a fresh local setup has not been able to migrate since those two landed. Each rename now runs only when the constraint exists, which leaves Postgres 18 with exactly the same rename and lets 17 finish with the same schema. `.env.local.example` was also missing `SERVER_ANTHROPIC_API_KEY`, which `apps/api/src/env.ts` requires, so the setup reported a failed "Validate .env" step on a fresh clone. The placeholder now sits with the other Anthropic one. --- ...0122_webhook_events_swap_to_short_retention.sql | 12 ++++++------ ...5_automation_events_swap_to_short_retention.sql | 14 +++++++------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/db/drizzle/0122_webhook_events_swap_to_short_retention.sql b/packages/db/drizzle/0122_webhook_events_swap_to_short_retention.sql index e2cbb095b13..6938c83260d 100644 --- a/packages/db/drizzle/0122_webhook_events_swap_to_short_retention.sql +++ b/packages/db/drizzle/0122_webhook_events_swap_to_short_retention.sql @@ -71,9 +71,9 @@ ALTER INDEX "ingest"."webhook_events_swap_received_at_idx" RENAME TO "webhook_ev -- Postgres names a not-null constraint after the table it was created on, so -- without these the table keeps "_swap_" in six constraint names and stops -- matching what a fresh CREATE TABLE from the schema builds. -ALTER TABLE "ingest"."webhook_events" RENAME CONSTRAINT "webhook_events_swap_id_not_null" TO "webhook_events_id_not_null";--> statement-breakpoint -ALTER TABLE "ingest"."webhook_events" RENAME CONSTRAINT "webhook_events_swap_provider_not_null" TO "webhook_events_provider_not_null";--> statement-breakpoint -ALTER TABLE "ingest"."webhook_events" RENAME CONSTRAINT "webhook_events_swap_event_id_not_null" TO "webhook_events_event_id_not_null";--> statement-breakpoint -ALTER TABLE "ingest"."webhook_events" RENAME CONSTRAINT "webhook_events_swap_status_not_null" TO "webhook_events_status_not_null";--> statement-breakpoint -ALTER TABLE "ingest"."webhook_events" RENAME CONSTRAINT "webhook_events_swap_retry_count_not_null" TO "webhook_events_retry_count_not_null";--> statement-breakpoint -ALTER TABLE "ingest"."webhook_events" RENAME CONSTRAINT "webhook_events_swap_received_at_not_null" TO "webhook_events_received_at_not_null"; +DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'webhook_events_swap_id_not_null') THEN ALTER TABLE "ingest"."webhook_events" RENAME CONSTRAINT "webhook_events_swap_id_not_null" TO "webhook_events_id_not_null"; END IF; END $do$;--> statement-breakpoint +DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'webhook_events_swap_provider_not_null') THEN ALTER TABLE "ingest"."webhook_events" RENAME CONSTRAINT "webhook_events_swap_provider_not_null" TO "webhook_events_provider_not_null"; END IF; END $do$;--> statement-breakpoint +DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'webhook_events_swap_event_id_not_null') THEN ALTER TABLE "ingest"."webhook_events" RENAME CONSTRAINT "webhook_events_swap_event_id_not_null" TO "webhook_events_event_id_not_null"; END IF; END $do$;--> statement-breakpoint +DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'webhook_events_swap_status_not_null') THEN ALTER TABLE "ingest"."webhook_events" RENAME CONSTRAINT "webhook_events_swap_status_not_null" TO "webhook_events_status_not_null"; END IF; END $do$;--> statement-breakpoint +DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'webhook_events_swap_retry_count_not_null') THEN ALTER TABLE "ingest"."webhook_events" RENAME CONSTRAINT "webhook_events_swap_retry_count_not_null" TO "webhook_events_retry_count_not_null"; END IF; END $do$;--> statement-breakpoint +DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'webhook_events_swap_received_at_not_null') THEN ALTER TABLE "ingest"."webhook_events" RENAME CONSTRAINT "webhook_events_swap_received_at_not_null" TO "webhook_events_received_at_not_null"; END IF; END $do$; diff --git a/packages/db/drizzle/0125_automation_events_swap_to_short_retention.sql b/packages/db/drizzle/0125_automation_events_swap_to_short_retention.sql index 2910349c1fa..a7c37146281 100644 --- a/packages/db/drizzle/0125_automation_events_swap_to_short_retention.sql +++ b/packages/db/drizzle/0125_automation_events_swap_to_short_retention.sql @@ -89,13 +89,13 @@ ALTER INDEX "automation_events_swap_resource_idx" RENAME TO "automation_events_r ALTER INDEX "automation_events_swap_received_at_idx" RENAME TO "automation_events_received_at_idx";--> statement-breakpoint ALTER TABLE "automation_events" RENAME CONSTRAINT "automation_events_swap_dedup_unique" TO "automation_events_dedup_unique";--> statement-breakpoint ALTER TABLE "automation_events" RENAME CONSTRAINT "automation_events_swap_organization_id_organizations_id_fk" TO "automation_events_organization_id_organizations_id_fk";--> statement-breakpoint -ALTER TABLE "automation_events" RENAME CONSTRAINT "automation_events_swap_id_not_null" TO "automation_events_id_not_null";--> statement-breakpoint -ALTER TABLE "automation_events" RENAME CONSTRAINT "automation_events_swap_organization_id_not_null" TO "automation_events_organization_id_not_null";--> statement-breakpoint -ALTER TABLE "automation_events" RENAME CONSTRAINT "automation_events_swap_provider_not_null" TO "automation_events_provider_not_null";--> statement-breakpoint -ALTER TABLE "automation_events" RENAME CONSTRAINT "automation_events_swap_event_type_not_null" TO "automation_events_event_type_not_null";--> statement-breakpoint -ALTER TABLE "automation_events" RENAME CONSTRAINT "automation_events_swap_external_event_id_not_null" TO "automation_events_external_event_id_not_null";--> statement-breakpoint -ALTER TABLE "automation_events" RENAME CONSTRAINT "automation_events_swap_title_not_null" TO "automation_events_title_not_null";--> statement-breakpoint -ALTER TABLE "automation_events" RENAME CONSTRAINT "automation_events_swap_received_at_not_null" TO "automation_events_received_at_not_null";--> statement-breakpoint +DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'automation_events_swap_id_not_null') THEN ALTER TABLE "automation_events" RENAME CONSTRAINT "automation_events_swap_id_not_null" TO "automation_events_id_not_null"; END IF; END $do$;--> statement-breakpoint +DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'automation_events_swap_organization_id_not_null') THEN ALTER TABLE "automation_events" RENAME CONSTRAINT "automation_events_swap_organization_id_not_null" TO "automation_events_organization_id_not_null"; END IF; END $do$;--> statement-breakpoint +DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'automation_events_swap_provider_not_null') THEN ALTER TABLE "automation_events" RENAME CONSTRAINT "automation_events_swap_provider_not_null" TO "automation_events_provider_not_null"; END IF; END $do$;--> statement-breakpoint +DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'automation_events_swap_event_type_not_null') THEN ALTER TABLE "automation_events" RENAME CONSTRAINT "automation_events_swap_event_type_not_null" TO "automation_events_event_type_not_null"; END IF; END $do$;--> statement-breakpoint +DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'automation_events_swap_external_event_id_not_null') THEN ALTER TABLE "automation_events" RENAME CONSTRAINT "automation_events_swap_external_event_id_not_null" TO "automation_events_external_event_id_not_null"; END IF; END $do$;--> statement-breakpoint +DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'automation_events_swap_title_not_null') THEN ALTER TABLE "automation_events" RENAME CONSTRAINT "automation_events_swap_title_not_null" TO "automation_events_title_not_null"; END IF; END $do$;--> statement-breakpoint +DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'automation_events_swap_received_at_not_null') THEN ALTER TABLE "automation_events" RENAME CONSTRAINT "automation_events_swap_received_at_not_null" TO "automation_events_received_at_not_null"; END IF; END $do$;--> statement-breakpoint -- What ON DELETE SET NULL would have done for the rows that were not carried. UPDATE "automation_runs" SET "event_id" = NULL WHERE "event_id" IS NOT NULL AND NOT EXISTS (SELECT 1 FROM "automation_events" e WHERE e."id" = "automation_runs"."event_id");--> statement-breakpoint From 2005bee9bd215d99a1b554f09b28e27964f5ade9 Mon Sep 17 00:00:00 2001 From: andyst-dev <150129844+andyst-dev@users.noreply.github.com> Date: Mon, 2 Nov 2026 08:56:06 +0100 Subject: [PATCH 2/2] fix(db): scope every constraint guard to its own table `conname` is not unique across relations, so a swap constraint with the same name on another table made the guard pass while the target table had none: the scoped `ALTER TABLE ... RENAME CONSTRAINT` then failed and took the whole migration down. All six guards in 0122 now also require `conrelid = 'ingest.webhook_events'::regclass`, and all seven in 0125 require the same for `automation_events`, resolved through the search path exactly like the unqualified `ALTER TABLE` they guard. `contype` is deliberately not added: Postgres 17 keeps column `NOT NULL` state in `pg_attribute`, not `pg_constraint` (#7899 review). --- ...0122_webhook_events_swap_to_short_retention.sql | 12 ++++++------ ...5_automation_events_swap_to_short_retention.sql | 14 +++++++------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/db/drizzle/0122_webhook_events_swap_to_short_retention.sql b/packages/db/drizzle/0122_webhook_events_swap_to_short_retention.sql index 6938c83260d..b7536b8ba7d 100644 --- a/packages/db/drizzle/0122_webhook_events_swap_to_short_retention.sql +++ b/packages/db/drizzle/0122_webhook_events_swap_to_short_retention.sql @@ -71,9 +71,9 @@ ALTER INDEX "ingest"."webhook_events_swap_received_at_idx" RENAME TO "webhook_ev -- Postgres names a not-null constraint after the table it was created on, so -- without these the table keeps "_swap_" in six constraint names and stops -- matching what a fresh CREATE TABLE from the schema builds. -DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'webhook_events_swap_id_not_null') THEN ALTER TABLE "ingest"."webhook_events" RENAME CONSTRAINT "webhook_events_swap_id_not_null" TO "webhook_events_id_not_null"; END IF; END $do$;--> statement-breakpoint -DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'webhook_events_swap_provider_not_null') THEN ALTER TABLE "ingest"."webhook_events" RENAME CONSTRAINT "webhook_events_swap_provider_not_null" TO "webhook_events_provider_not_null"; END IF; END $do$;--> statement-breakpoint -DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'webhook_events_swap_event_id_not_null') THEN ALTER TABLE "ingest"."webhook_events" RENAME CONSTRAINT "webhook_events_swap_event_id_not_null" TO "webhook_events_event_id_not_null"; END IF; END $do$;--> statement-breakpoint -DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'webhook_events_swap_status_not_null') THEN ALTER TABLE "ingest"."webhook_events" RENAME CONSTRAINT "webhook_events_swap_status_not_null" TO "webhook_events_status_not_null"; END IF; END $do$;--> statement-breakpoint -DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'webhook_events_swap_retry_count_not_null') THEN ALTER TABLE "ingest"."webhook_events" RENAME CONSTRAINT "webhook_events_swap_retry_count_not_null" TO "webhook_events_retry_count_not_null"; END IF; END $do$;--> statement-breakpoint -DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'webhook_events_swap_received_at_not_null') THEN ALTER TABLE "ingest"."webhook_events" RENAME CONSTRAINT "webhook_events_swap_received_at_not_null" TO "webhook_events_received_at_not_null"; END IF; END $do$; +DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'webhook_events_swap_id_not_null' AND conrelid = 'ingest.webhook_events'::regclass) THEN ALTER TABLE "ingest"."webhook_events" RENAME CONSTRAINT "webhook_events_swap_id_not_null" TO "webhook_events_id_not_null"; END IF; END $do$;--> statement-breakpoint +DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'webhook_events_swap_provider_not_null' AND conrelid = 'ingest.webhook_events'::regclass) THEN ALTER TABLE "ingest"."webhook_events" RENAME CONSTRAINT "webhook_events_swap_provider_not_null" TO "webhook_events_provider_not_null"; END IF; END $do$;--> statement-breakpoint +DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'webhook_events_swap_event_id_not_null' AND conrelid = 'ingest.webhook_events'::regclass) THEN ALTER TABLE "ingest"."webhook_events" RENAME CONSTRAINT "webhook_events_swap_event_id_not_null" TO "webhook_events_event_id_not_null"; END IF; END $do$;--> statement-breakpoint +DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'webhook_events_swap_status_not_null' AND conrelid = 'ingest.webhook_events'::regclass) THEN ALTER TABLE "ingest"."webhook_events" RENAME CONSTRAINT "webhook_events_swap_status_not_null" TO "webhook_events_status_not_null"; END IF; END $do$;--> statement-breakpoint +DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'webhook_events_swap_retry_count_not_null' AND conrelid = 'ingest.webhook_events'::regclass) THEN ALTER TABLE "ingest"."webhook_events" RENAME CONSTRAINT "webhook_events_swap_retry_count_not_null" TO "webhook_events_retry_count_not_null"; END IF; END $do$;--> statement-breakpoint +DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'webhook_events_swap_received_at_not_null' AND conrelid = 'ingest.webhook_events'::regclass) THEN ALTER TABLE "ingest"."webhook_events" RENAME CONSTRAINT "webhook_events_swap_received_at_not_null" TO "webhook_events_received_at_not_null"; END IF; END $do$; diff --git a/packages/db/drizzle/0125_automation_events_swap_to_short_retention.sql b/packages/db/drizzle/0125_automation_events_swap_to_short_retention.sql index a7c37146281..eed7ffc8761 100644 --- a/packages/db/drizzle/0125_automation_events_swap_to_short_retention.sql +++ b/packages/db/drizzle/0125_automation_events_swap_to_short_retention.sql @@ -89,13 +89,13 @@ ALTER INDEX "automation_events_swap_resource_idx" RENAME TO "automation_events_r ALTER INDEX "automation_events_swap_received_at_idx" RENAME TO "automation_events_received_at_idx";--> statement-breakpoint ALTER TABLE "automation_events" RENAME CONSTRAINT "automation_events_swap_dedup_unique" TO "automation_events_dedup_unique";--> statement-breakpoint ALTER TABLE "automation_events" RENAME CONSTRAINT "automation_events_swap_organization_id_organizations_id_fk" TO "automation_events_organization_id_organizations_id_fk";--> statement-breakpoint -DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'automation_events_swap_id_not_null') THEN ALTER TABLE "automation_events" RENAME CONSTRAINT "automation_events_swap_id_not_null" TO "automation_events_id_not_null"; END IF; END $do$;--> statement-breakpoint -DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'automation_events_swap_organization_id_not_null') THEN ALTER TABLE "automation_events" RENAME CONSTRAINT "automation_events_swap_organization_id_not_null" TO "automation_events_organization_id_not_null"; END IF; END $do$;--> statement-breakpoint -DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'automation_events_swap_provider_not_null') THEN ALTER TABLE "automation_events" RENAME CONSTRAINT "automation_events_swap_provider_not_null" TO "automation_events_provider_not_null"; END IF; END $do$;--> statement-breakpoint -DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'automation_events_swap_event_type_not_null') THEN ALTER TABLE "automation_events" RENAME CONSTRAINT "automation_events_swap_event_type_not_null" TO "automation_events_event_type_not_null"; END IF; END $do$;--> statement-breakpoint -DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'automation_events_swap_external_event_id_not_null') THEN ALTER TABLE "automation_events" RENAME CONSTRAINT "automation_events_swap_external_event_id_not_null" TO "automation_events_external_event_id_not_null"; END IF; END $do$;--> statement-breakpoint -DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'automation_events_swap_title_not_null') THEN ALTER TABLE "automation_events" RENAME CONSTRAINT "automation_events_swap_title_not_null" TO "automation_events_title_not_null"; END IF; END $do$;--> statement-breakpoint -DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'automation_events_swap_received_at_not_null') THEN ALTER TABLE "automation_events" RENAME CONSTRAINT "automation_events_swap_received_at_not_null" TO "automation_events_received_at_not_null"; END IF; END $do$;--> statement-breakpoint +DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'automation_events_swap_id_not_null' AND conrelid = 'automation_events'::regclass) THEN ALTER TABLE "automation_events" RENAME CONSTRAINT "automation_events_swap_id_not_null" TO "automation_events_id_not_null"; END IF; END $do$;--> statement-breakpoint +DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'automation_events_swap_organization_id_not_null' AND conrelid = 'automation_events'::regclass) THEN ALTER TABLE "automation_events" RENAME CONSTRAINT "automation_events_swap_organization_id_not_null" TO "automation_events_organization_id_not_null"; END IF; END $do$;--> statement-breakpoint +DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'automation_events_swap_provider_not_null' AND conrelid = 'automation_events'::regclass) THEN ALTER TABLE "automation_events" RENAME CONSTRAINT "automation_events_swap_provider_not_null" TO "automation_events_provider_not_null"; END IF; END $do$;--> statement-breakpoint +DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'automation_events_swap_event_type_not_null' AND conrelid = 'automation_events'::regclass) THEN ALTER TABLE "automation_events" RENAME CONSTRAINT "automation_events_swap_event_type_not_null" TO "automation_events_event_type_not_null"; END IF; END $do$;--> statement-breakpoint +DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'automation_events_swap_external_event_id_not_null' AND conrelid = 'automation_events'::regclass) THEN ALTER TABLE "automation_events" RENAME CONSTRAINT "automation_events_swap_external_event_id_not_null" TO "automation_events_external_event_id_not_null"; END IF; END $do$;--> statement-breakpoint +DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'automation_events_swap_title_not_null' AND conrelid = 'automation_events'::regclass) THEN ALTER TABLE "automation_events" RENAME CONSTRAINT "automation_events_swap_title_not_null" TO "automation_events_title_not_null"; END IF; END $do$;--> statement-breakpoint +DO $do$ BEGIN IF EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'automation_events_swap_received_at_not_null' AND conrelid = 'automation_events'::regclass) THEN ALTER TABLE "automation_events" RENAME CONSTRAINT "automation_events_swap_received_at_not_null" TO "automation_events_received_at_not_null"; END IF; END $do$;--> statement-breakpoint -- What ON DELETE SET NULL would have done for the rows that were not carried. UPDATE "automation_runs" SET "event_id" = NULL WHERE "event_id" IS NOT NULL AND NOT EXISTS (SELECT 1 FROM "automation_events" e WHERE e."id" = "automation_runs"."event_id");--> statement-breakpoint