From 91307c87cc73100261ed8baf98ed946445874b6c Mon Sep 17 00:00:00 2001 From: Jason Woltje Date: Sun, 15 Feb 2026 00:29:37 -0600 Subject: [PATCH] fix(database): add missing federation table migrations Federation models (FederationConnection, FederatedIdentity, FederationMessage) and their enums were defined in the Prisma schema but never had CREATE TABLE migrations. This caused the 20260203_add_federation_event_subscriptions migration to fail with "relation federation_messages does not exist". Adds new migration 20260202200000 to create the 3 missing enums, 3 missing tables, all indexes, and foreign keys. Removes the now-redundant ALTER TABLE from the 20260203 migration since event_type is created with the table. Co-Authored-By: Claude Opus 4.6 --- .../migration.sql | 118 ++++++++++++++++++ .../migration.sql | 6 - 2 files changed, 118 insertions(+), 6 deletions(-) create mode 100644 apps/api/prisma/migrations/20260202200000_add_federation_tables/migration.sql diff --git a/apps/api/prisma/migrations/20260202200000_add_federation_tables/migration.sql b/apps/api/prisma/migrations/20260202200000_add_federation_tables/migration.sql new file mode 100644 index 0000000..3fb6308 --- /dev/null +++ b/apps/api/prisma/migrations/20260202200000_add_federation_tables/migration.sql @@ -0,0 +1,118 @@ +-- CreateEnum +CREATE TYPE "FederationConnectionStatus" AS ENUM ('PENDING', 'ACTIVE', 'SUSPENDED', 'DISCONNECTED'); + +-- CreateEnum +CREATE TYPE "FederationMessageType" AS ENUM ('QUERY', 'COMMAND', 'EVENT'); + +-- CreateEnum +CREATE TYPE "FederationMessageStatus" AS ENUM ('PENDING', 'DELIVERED', 'FAILED', 'TIMEOUT'); + +-- CreateTable +CREATE TABLE "federation_connections" ( + "id" UUID NOT NULL, + "workspace_id" UUID NOT NULL, + "remote_instance_id" TEXT NOT NULL, + "remote_url" TEXT NOT NULL, + "remote_public_key" TEXT NOT NULL, + "remote_capabilities" JSONB NOT NULL DEFAULT '{}', + "status" "FederationConnectionStatus" NOT NULL DEFAULT 'PENDING', + "metadata" JSONB NOT NULL DEFAULT '{}', + "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMPTZ NOT NULL, + "connected_at" TIMESTAMPTZ, + "disconnected_at" TIMESTAMPTZ, + + CONSTRAINT "federation_connections_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "federated_identities" ( + "id" UUID NOT NULL, + "local_user_id" UUID NOT NULL, + "remote_user_id" TEXT NOT NULL, + "remote_instance_id" TEXT NOT NULL, + "oidc_subject" TEXT NOT NULL, + "email" TEXT NOT NULL, + "metadata" JSONB NOT NULL DEFAULT '{}', + "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMPTZ NOT NULL, + + CONSTRAINT "federated_identities_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "federation_messages" ( + "id" UUID NOT NULL, + "workspace_id" UUID NOT NULL, + "connection_id" UUID NOT NULL, + "message_type" "FederationMessageType" NOT NULL, + "message_id" TEXT NOT NULL, + "correlation_id" TEXT, + "query" TEXT, + "command_type" TEXT, + "event_type" TEXT, + "payload" JSONB DEFAULT '{}', + "response" JSONB DEFAULT '{}', + "status" "FederationMessageStatus" NOT NULL DEFAULT 'PENDING', + "error" TEXT, + "signature" TEXT NOT NULL, + "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMPTZ NOT NULL, + "delivered_at" TIMESTAMPTZ, + + CONSTRAINT "federation_messages_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "federation_connections_workspace_id_remote_instance_id_key" ON "federation_connections"("workspace_id", "remote_instance_id"); + +-- CreateIndex +CREATE INDEX "federation_connections_workspace_id_idx" ON "federation_connections"("workspace_id"); + +-- CreateIndex +CREATE INDEX "federation_connections_workspace_id_status_idx" ON "federation_connections"("workspace_id", "status"); + +-- CreateIndex +CREATE INDEX "federation_connections_remote_instance_id_idx" ON "federation_connections"("remote_instance_id"); + +-- CreateIndex +CREATE UNIQUE INDEX "federated_identities_local_user_id_remote_instance_id_key" ON "federated_identities"("local_user_id", "remote_instance_id"); + +-- CreateIndex +CREATE INDEX "federated_identities_local_user_id_idx" ON "federated_identities"("local_user_id"); + +-- CreateIndex +CREATE INDEX "federated_identities_remote_instance_id_idx" ON "federated_identities"("remote_instance_id"); + +-- CreateIndex +CREATE INDEX "federated_identities_oidc_subject_idx" ON "federated_identities"("oidc_subject"); + +-- CreateIndex +CREATE UNIQUE INDEX "federation_messages_message_id_key" ON "federation_messages"("message_id"); + +-- CreateIndex +CREATE INDEX "federation_messages_workspace_id_idx" ON "federation_messages"("workspace_id"); + +-- CreateIndex +CREATE INDEX "federation_messages_connection_id_idx" ON "federation_messages"("connection_id"); + +-- CreateIndex +CREATE INDEX "federation_messages_message_id_idx" ON "federation_messages"("message_id"); + +-- CreateIndex +CREATE INDEX "federation_messages_correlation_id_idx" ON "federation_messages"("correlation_id"); + +-- CreateIndex +CREATE INDEX "federation_messages_event_type_idx" ON "federation_messages"("event_type"); + +-- AddForeignKey +ALTER TABLE "federation_connections" ADD CONSTRAINT "federation_connections_workspace_id_fkey" FOREIGN KEY ("workspace_id") REFERENCES "workspaces"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "federated_identities" ADD CONSTRAINT "federated_identities_local_user_id_fkey" FOREIGN KEY ("local_user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "federation_messages" ADD CONSTRAINT "federation_messages_connection_id_fkey" FOREIGN KEY ("connection_id") REFERENCES "federation_connections"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "federation_messages" ADD CONSTRAINT "federation_messages_workspace_id_fkey" FOREIGN KEY ("workspace_id") REFERENCES "workspaces"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/apps/api/prisma/migrations/20260203_add_federation_event_subscriptions/migration.sql b/apps/api/prisma/migrations/20260203_add_federation_event_subscriptions/migration.sql index 0c7974d..9c08d09 100644 --- a/apps/api/prisma/migrations/20260203_add_federation_event_subscriptions/migration.sql +++ b/apps/api/prisma/migrations/20260203_add_federation_event_subscriptions/migration.sql @@ -1,9 +1,3 @@ --- Add eventType column to federation_messages table -ALTER TABLE "federation_messages" ADD COLUMN "event_type" TEXT; - --- Add index for eventType -CREATE INDEX "federation_messages_event_type_idx" ON "federation_messages"("event_type"); - -- CreateTable CREATE TABLE "federation_event_subscriptions" ( "id" UUID NOT NULL,