CREATE TYPE "public"."publishing_channel_status" AS ENUM('CONNECTED', 'EXPIRED', 'REVOKED', 'ERROR');--> statement-breakpoint CREATE TABLE "publishing_channel_accounts" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "workspace_id" uuid NOT NULL, "platform" varchar(32) NOT NULL, "external_account_id" varchar(255) NOT NULL, "display_name" varchar(180) NOT NULL, "avatar_url" text, "status" "publishing_channel_status" DEFAULT 'CONNECTED' NOT NULL, "access_token_ciphertext" text, "refresh_token_ciphertext" text, "token_expires_at" timestamp with time zone, "scopes" jsonb DEFAULT '[]'::jsonb NOT NULL, "metadata" jsonb DEFAULT '{}'::jsonb NOT NULL, "connected_by_id" uuid NOT NULL, "last_verified_at" timestamp with time zone DEFAULT now() NOT NULL, "error_code" varchar(100), "error_message" text, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "publishing_oauth_states" ( "state_hash" varchar(64) PRIMARY KEY NOT NULL, "workspace_id" uuid NOT NULL, "platform" varchar(32) NOT NULL, "code_verifier_ciphertext" text, "return_path" text DEFAULT '/?publishing=connected' NOT NULL, "created_by_id" uuid NOT NULL, "expires_at" timestamp with time zone NOT NULL, "consumed_at" timestamp with time zone, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "publishing_webhook_events" ( "event_key" varchar(320) PRIMARY KEY NOT NULL, "event_id" varchar(255) NOT NULL, "platform" varchar(32) NOT NULL, "delivery_id" uuid, "status" varchar(32) NOT NULL, "provider_occurred_at" timestamp with time zone NOT NULL, "payload" jsonb DEFAULT '{}'::jsonb NOT NULL, "applied" boolean DEFAULT false NOT NULL, "processed_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint ALTER TABLE "deliveries" ADD COLUMN "channel_account_id" uuid;--> statement-breakpoint ALTER TABLE "deliveries" ADD COLUMN "last_provider_event_at" timestamp with time zone;--> statement-breakpoint ALTER TABLE "publishing_channel_accounts" ADD CONSTRAINT "publishing_channel_accounts_workspace_id_workspaces_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "public"."workspaces"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "publishing_channel_accounts" ADD CONSTRAINT "publishing_channel_accounts_connected_by_id_users_id_fk" FOREIGN KEY ("connected_by_id") REFERENCES "public"."users"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint ALTER TABLE "publishing_oauth_states" ADD CONSTRAINT "publishing_oauth_states_workspace_id_workspaces_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "public"."workspaces"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "publishing_oauth_states" ADD CONSTRAINT "publishing_oauth_states_created_by_id_users_id_fk" FOREIGN KEY ("created_by_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "publishing_webhook_events" ADD CONSTRAINT "publishing_webhook_events_delivery_id_deliveries_id_fk" FOREIGN KEY ("delivery_id") REFERENCES "public"."deliveries"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint CREATE UNIQUE INDEX "publishing_channel_accounts_external_unique" ON "publishing_channel_accounts" USING btree ("workspace_id","platform","external_account_id");--> statement-breakpoint CREATE INDEX "publishing_channel_accounts_workspace_idx" ON "publishing_channel_accounts" USING btree ("workspace_id","platform");--> statement-breakpoint CREATE INDEX "publishing_channel_accounts_status_idx" ON "publishing_channel_accounts" USING btree ("workspace_id","status");--> statement-breakpoint CREATE INDEX "publishing_oauth_states_expiry_idx" ON "publishing_oauth_states" USING btree ("expires_at");--> statement-breakpoint CREATE INDEX "publishing_oauth_states_workspace_idx" ON "publishing_oauth_states" USING btree ("workspace_id","platform");--> statement-breakpoint CREATE INDEX "publishing_webhook_events_delivery_idx" ON "publishing_webhook_events" USING btree ("delivery_id","provider_occurred_at");--> statement-breakpoint CREATE INDEX "publishing_webhook_events_platform_idx" ON "publishing_webhook_events" USING btree ("platform","processed_at");--> statement-breakpoint ALTER TABLE "deliveries" ADD CONSTRAINT "deliveries_channel_account_id_publishing_channel_accounts_id_fk" FOREIGN KEY ("channel_account_id") REFERENCES "public"."publishing_channel_accounts"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint CREATE INDEX "deliveries_channel_account_idx" ON "deliveries" USING btree ("channel_account_id");