From 3cf2ada84e82cbcbb41a466576ef2ca9abaa0deb Mon Sep 17 00:00:00 2001 From: Muhammad Ibrahim Date: Sat, 4 Oct 2025 09:05:36 +0100 Subject: [PATCH] migration: Add machine_id column to hosts table - Adds machine_id as unique identifier for hosts - Migrates existing hosts with 'migrated-' prefix - Removes unique constraint from friendly_name - Adds indexes for performance --- .../migration.sql | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 backend/prisma/migrations/20251004090459_add_machine_id_to_hosts/migration.sql diff --git a/backend/prisma/migrations/20251004090459_add_machine_id_to_hosts/migration.sql b/backend/prisma/migrations/20251004090459_add_machine_id_to_hosts/migration.sql new file mode 100644 index 0000000..026a0fa --- /dev/null +++ b/backend/prisma/migrations/20251004090459_add_machine_id_to_hosts/migration.sql @@ -0,0 +1,17 @@ +-- Add machine_id column as nullable first +ALTER TABLE "hosts" ADD COLUMN "machine_id" TEXT; + +-- Generate machine_ids for existing hosts using their API ID as a fallback +UPDATE "hosts" SET "machine_id" = 'migrated-' || "api_id" WHERE "machine_id" IS NULL; + +-- Remove the unique constraint from friendly_name +ALTER TABLE "hosts" DROP CONSTRAINT IF EXISTS "hosts_friendly_name_key"; + +-- Now make machine_id NOT NULL and add unique constraint +ALTER TABLE "hosts" ALTER COLUMN "machine_id" SET NOT NULL; +ALTER TABLE "hosts" ADD CONSTRAINT "hosts_machine_id_key" UNIQUE ("machine_id"); + +-- Create indexes for better query performance +CREATE INDEX "hosts_machine_id_idx" ON "hosts"("machine_id"); +CREATE INDEX "hosts_friendly_name_idx" ON "hosts"("friendly_name"); +