diff --git a/.github/workflows/deploy-preview.yml b/.github/workflows/deploy-preview.yml index 0fc50677015..8ae87b2fa6d 100644 --- a/.github/workflows/deploy-preview.yml +++ b/.github/workflows/deploy-preview.yml @@ -57,6 +57,7 @@ jobs: env: DATABASE_URL: ${{ steps.create-branch.outputs.db_url_pooled }} DATABASE_URL_UNPOOLED: ${{ steps.create-branch.outputs.db_url }} + PGOPTIONS: -c lock_timeout=5s -c statement_timeout=60s run: | bun drizzle-kit check bun drizzle-kit migrate diff --git a/.github/workflows/deploy-production.yml b/.github/workflows/deploy-production.yml index 789a3f72d19..5f3ecedbcb0 100644 --- a/.github/workflows/deploy-production.yml +++ b/.github/workflows/deploy-production.yml @@ -13,6 +13,9 @@ jobs: name: Deploy Database Migrations runs-on: ubuntu-latest environment: production + concurrency: + group: production-database-migrations + cancel-in-progress: false steps: - name: Checkout repository @@ -38,7 +41,18 @@ jobs: env: DATABASE_URL: ${{ secrets.DATABASE_URL_UNPOOLED }} DATABASE_URL_UNPOOLED: ${{ secrets.DATABASE_URL_UNPOOLED }} - run: bun drizzle-kit migrate + # drizzle applies every pending migration in one transaction, so a + # migration that queues for a lock blocks all traffic behind it. + PGOPTIONS: -c lock_timeout=5s -c statement_timeout=60s + # Losing a lock race rolls back cleanly, so only that is retried. + run: | + for attempt in 1 2 3 4 5 6 7 8; do + if bun drizzle-kit migrate 2>&1 | tee migrate.log; then exit 0; fi + grep -qE "deadlock detected|lock timeout" migrate.log || exit 1 + echo "::warning::Migration lost a lock race (attempt $attempt), retrying" + sleep $((attempt * 5)) + done + exit 1 deploy-api: name: Deploy API to Vercel diff --git a/apps/api/src/lib/ingest/recordWebhookDelivery.ts b/apps/api/src/lib/ingest/recordWebhookDelivery.ts index 759e57fa800..1ed3b6f8af9 100644 --- a/apps/api/src/lib/ingest/recordWebhookDelivery.ts +++ b/apps/api/src/lib/ingest/recordWebhookDelivery.ts @@ -1,9 +1,9 @@ import { db } from "@superset/db/client"; +import type { integrationProvider } from "@superset/db/schema"; import { DrizzleQueryError, sql } from "drizzle-orm"; import { databaseErrorMessage } from "@/lib/databaseErrorMessage"; -/** The connector slug a delivery arrived for; free text since 0114. */ -type Provider = string; +type Provider = (typeof integrationProvider.enumValues)[number]; export interface RecordedDelivery { id: string; diff --git a/packages/db/drizzle/0119_plugin_oauth_clients_and_connectors.sql b/packages/db/drizzle/0119_plugin_oauth_clients_and_connectors.sql index 483721748c5..cd0e3b737fb 100644 --- a/packages/db/drizzle/0119_plugin_oauth_clients_and_connectors.sql +++ b/packages/db/drizzle/0119_plugin_oauth_clients_and_connectors.sql @@ -38,11 +38,14 @@ CREATE TABLE "connections" ( CONSTRAINT "connections_user_identity_present" CHECK (owner_kind <> 'user' OR external_user_id IS NOT NULL) ); --> statement-breakpoint -ALTER TABLE "plugin_connections" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint -DROP TABLE "plugin_connections" CASCADE;--> statement-breakpoint +-- Hand-ordered: this must run before the DROP TABLE below. That statement takes +-- ACCESS EXCLUSIVE on auth.organizations, and an in-flight insert into +-- automation_events holds automation_events while waiting on auth.organizations +-- for its foreign-key check, so the generated order deadlocks under traffic. ALTER TABLE "automation_events" DROP CONSTRAINT "automation_events_integration_connection_id_integration_connections_id_fk"; --> statement-breakpoint -ALTER TABLE "ingest"."webhook_events" ALTER COLUMN "provider" SET DATA TYPE text;--> statement-breakpoint +ALTER TABLE "plugin_connections" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint +DROP TABLE "plugin_connections" CASCADE;--> statement-breakpoint ALTER TABLE "connections" ADD CONSTRAINT "connections_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "auth"."organizations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "connections" ADD CONSTRAINT "connections_connected_by_user_id_users_id_fk" FOREIGN KEY ("connected_by_user_id") REFERENCES "auth"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint CREATE UNIQUE INDEX "plugin_oauth_clients_issuer_redirect_unique" ON "plugin_oauth_clients" USING btree ("issuer","redirect_uri");--> statement-breakpoint diff --git a/packages/db/drizzle/meta/0119_snapshot.json b/packages/db/drizzle/meta/0119_snapshot.json index 98e3fced17e..fe2d0b4bb6c 100644 --- a/packages/db/drizzle/meta/0119_snapshot.json +++ b/packages/db/drizzle/meta/0119_snapshot.json @@ -1,5 +1,5 @@ { - "id": "aedc55ed-1344-43c3-a89d-d002ea861018", + "id": "e2dbc465-8d48-4ac6-957e-cf51815f7afe", "prevId": "52dcd5f1-52d3-461e-8609-5e6452d3ecd0", "version": "7", "dialect": "postgresql", @@ -2564,7 +2564,8 @@ }, "provider": { "name": "provider", - "type": "text", + "type": "integration_provider", + "typeSchema": "public", "primaryKey": false, "notNull": true }, diff --git a/packages/db/src/schema/ingest.ts b/packages/db/src/schema/ingest.ts index 529c779ed69..3fb7505f6db 100644 --- a/packages/db/src/schema/ingest.ts +++ b/packages/db/src/schema/ingest.ts @@ -7,6 +7,8 @@ import { uniqueIndex, uuid, } from "drizzle-orm/pg-core"; +import { integrationProvider } from "./schema"; + export const ingestSchema = pgSchema("ingest"); /** @@ -28,7 +30,7 @@ export const webhookEvents = ingestSchema.table( id: uuid().primaryKey().defaultRandom(), // Source - provider: text().notNull(), + provider: integrationProvider().notNull(), eventId: text("event_id").notNull(), eventType: text("event_type"),