/*
  Warnings:

  - You are about to drop the `fm_parts` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the `fms` table. If the table is not empty, all the data it contains will be lost.
  - You are about to drop the column `fmId` on the `repairs` table. All the data in the column will be lost.

*/
-- Drop orphan part_events referencing the retired FM event types.
DELETE FROM "part_events" WHERE "type" IN ('FM_OPENED', 'FM_CLOSED');

-- DropIndex
DROP INDEX "fm_parts_partId_idx";

-- DropIndex
DROP INDEX "fms_status_openedAt_idx";

-- DropIndex
DROP INDEX "fms_hostId_idx";

-- DropIndex
DROP INDEX "fms_status_idx";

-- DropTable
PRAGMA foreign_keys=off;
DROP TABLE "fm_parts";
PRAGMA foreign_keys=on;

-- DropTable
PRAGMA foreign_keys=off;
DROP TABLE "fms";
PRAGMA foreign_keys=on;

-- RedefineTables
PRAGMA defer_foreign_keys=ON;
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_repairs" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "hostId" TEXT NOT NULL,
    "brokenPartId" TEXT NOT NULL,
    "replacementPartId" TEXT NOT NULL,
    "performedById" TEXT NOT NULL,
    "performedAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" DATETIME NOT NULL,
    CONSTRAINT "repairs_hostId_fkey" FOREIGN KEY ("hostId") REFERENCES "Host" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
    CONSTRAINT "repairs_brokenPartId_fkey" FOREIGN KEY ("brokenPartId") REFERENCES "Part" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
    CONSTRAINT "repairs_replacementPartId_fkey" FOREIGN KEY ("replacementPartId") REFERENCES "Part" ("id") ON DELETE RESTRICT ON UPDATE CASCADE,
    CONSTRAINT "repairs_performedById_fkey" FOREIGN KEY ("performedById") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);
INSERT INTO "new_repairs" ("brokenPartId", "createdAt", "hostId", "id", "performedAt", "performedById", "replacementPartId", "updatedAt") SELECT "brokenPartId", "createdAt", "hostId", "id", "performedAt", "performedById", "replacementPartId", "updatedAt" FROM "repairs";
DROP TABLE "repairs";
ALTER TABLE "new_repairs" RENAME TO "repairs";
CREATE INDEX "repairs_hostId_performedAt_idx" ON "repairs"("hostId", "performedAt" DESC);
CREATE INDEX "repairs_performedById_performedAt_idx" ON "repairs"("performedById", "performedAt" DESC);
CREATE INDEX "repairs_brokenPartId_idx" ON "repairs"("brokenPartId");
CREATE INDEX "repairs_replacementPartId_idx" ON "repairs"("replacementPartId");
PRAGMA foreign_keys=ON;
PRAGMA defer_foreign_keys=OFF;
