diff --git a/prisma/migrations/20250801035031_add_delete_casacde/migration.sql b/prisma/migrations/20250801035031_add_delete_casacde/migration.sql new file mode 100644 index 0000000..645377a --- /dev/null +++ b/prisma/migrations/20250801035031_add_delete_casacde/migration.sql @@ -0,0 +1,11 @@ +-- DropForeignKey +ALTER TABLE "Game" DROP CONSTRAINT "Game_libraryId_fkey"; + +-- DropIndex +DROP INDEX "GameTag_name_idx"; + +-- CreateIndex +CREATE INDEX "GameTag_name_idx" ON "GameTag" USING GIST ("name" gist_trgm_ops(siglen=32)); + +-- AddForeignKey +ALTER TABLE "Game" ADD CONSTRAINT "Game_libraryId_fkey" FOREIGN KEY ("libraryId") REFERENCES "Library"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/prisma/models/content.prisma b/prisma/models/content.prisma index 587f2a6..3ab28be 100644 --- a/prisma/models/content.prisma +++ b/prisma/models/content.prisma @@ -36,7 +36,7 @@ model Game { // These fields will not be optional in the next version // Any game without a library ID will be assigned one at startup, based on the defaults libraryId String? - library Library? @relation(fields: [libraryId], references: [id]) + library Library? @relation(fields: [libraryId], references: [id], onDelete: Cascade, onUpdate: Cascade) libraryPath String collections CollectionEntry[] diff --git a/server/plugins/05.library-init.ts b/server/plugins/05.library-init.ts index bba6090..9891b51 100644 --- a/server/plugins/05.library-init.ts +++ b/server/plugins/05.library-init.ts @@ -61,6 +61,14 @@ export default defineNitroPlugin(async () => { }); } + // Delete all games that don't have a library provider after the legacy handler + // (leftover from a bug) + await prisma.game.deleteMany({ + where: { + libraryId: null, + }, + }); + for (const library of libraries) { const constructor = libraryConstructors[library.backend]; try {