From c8f5bfc82ea52433e7296ea66d64f7e9da6b5047 Mon Sep 17 00:00:00 2001 From: chahinebrini Date: Thu, 18 Jun 2026 04:04:05 +0200 Subject: [PATCH] fix(backend): make user_devices migration idempotent for fresh DBs Add CREATE TABLE IF NOT EXISTS for rebreak.user_devices to the hardware_id migration so fresh databases can migrate despite the alphabetical ordering mismatch with 20260430_add_user_devices. Also apply Prettier formatting to mdm.ts. --- .../migration.sql | 20 +++++++++++++++++++ backend/server/db/mdm.ts | 4 +++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/backend/prisma/migrations/20250616_add_hardware_id_to_user_devices/migration.sql b/backend/prisma/migrations/20250616_add_hardware_id_to_user_devices/migration.sql index fa9ef99..49887e3 100644 --- a/backend/prisma/migrations/20250616_add_hardware_id_to_user_devices/migration.sql +++ b/backend/prisma/migrations/20250616_add_hardware_id_to_user_devices/migration.sql @@ -1,6 +1,26 @@ -- Hardware-ID für UserDevices -- Client liefert hardwaregebundene ID; Backend speichert sie separat zur bestehenden deviceId. +-- Die canonical CREATE TABLE für user_devices lebt in einer späteren Migration. +-- Wir erstellen die Tabelle hier idempotent voraus, damit frische Datenbanken +-- trotz falscher alphabetischer Reihenfolge sauber migrieren. +CREATE TABLE IF NOT EXISTS "rebreak"."user_devices" ( + "id" UUID PRIMARY KEY DEFAULT gen_random_uuid(), + "user_id" UUID NOT NULL, + "device_id" TEXT NOT NULL, + "platform" TEXT NOT NULL, + "model" TEXT, + "name" TEXT, + "last_seen_at" TIMESTAMP(3) WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, + "created_at" TIMESTAMP(3) WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP +); + +CREATE UNIQUE INDEX IF NOT EXISTS "user_devices_user_id_device_id_key" + ON "rebreak"."user_devices"("user_id", "device_id"); + +CREATE INDEX IF NOT EXISTS "user_devices_user_id_idx" + ON "rebreak"."user_devices"("user_id"); + ALTER TABLE "rebreak"."user_devices" ADD COLUMN IF NOT EXISTS "hardware_id" TEXT; diff --git a/backend/server/db/mdm.ts b/backend/server/db/mdm.ts index e32f11b..2d42366 100644 --- a/backend/server/db/mdm.ts +++ b/backend/server/db/mdm.ts @@ -104,7 +104,9 @@ export async function clearUserDeviceMdmId( /** * Load all iOS devices that have a NanoMDM UDID link. */ -export async function getLinkedUserDevices(): Promise { +export async function getLinkedUserDevices(): Promise< + UserDeviceMdmHealthRecord[] +> { const db = usePrisma(); return db.userDevice.findMany({ where: { platform: "ios", mdmId: { not: null } },