Skip to content

Commit

Permalink
Add foreign keys and remove unused indices and columns
Browse files Browse the repository at this point in the history
  • Loading branch information
nsilvestri committed Mar 18, 2024
1 parent d023599 commit 9faacc3
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 25 deletions.
83 changes: 83 additions & 0 deletions prisma/migrations/20240317234533_add_foreign_keys/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
Warnings:
- You are about to drop the column `caseId` on the `Algorithm` table. All the data in the column will be lost.
*/
-- DropIndex
DROP INDEX "Account_userId_idx";

-- DropIndex
DROP INDEX "Algorithm_caseId_idx";

-- DropIndex
DROP INDEX "AlgorithmsForCase_algorithmId_idx";

-- DropIndex
DROP INDEX "AlgorithmsForCase_caseId_idx";

-- DropIndex
DROP INDEX "Case_puzzleId_idx";

-- DropIndex
DROP INDEX "Method_puzzleId_idx";

-- DropIndex
DROP INDEX "Method_visualizationId_idx";

-- DropIndex
DROP INDEX "Puzzle_visualizationId_idx";

-- DropIndex
DROP INDEX "Session_userId_idx";

-- DropIndex
DROP INDEX "Set_puzzleId_idx";

-- DropIndex
DROP INDEX "Set_visualizationId_idx";

-- AlterTable
ALTER TABLE "Algorithm" DROP COLUMN "caseId";

-- AddForeignKey
ALTER TABLE "Account" ADD CONSTRAINT "Account_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Session" ADD CONSTRAINT "Session_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Puzzle" ADD CONSTRAINT "Puzzle_visualizationId_fkey" FOREIGN KEY ("visualizationId") REFERENCES "Visualization"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Set" ADD CONSTRAINT "Set_puzzleId_fkey" FOREIGN KEY ("puzzleId") REFERENCES "Puzzle"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Set" ADD CONSTRAINT "Set_visualizationId_fkey" FOREIGN KEY ("visualizationId") REFERENCES "Visualization"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Case" ADD CONSTRAINT "Case_puzzleId_fkey" FOREIGN KEY ("puzzleId") REFERENCES "Puzzle"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "AlgorithmsForCase" ADD CONSTRAINT "AlgorithmsForCase_algorithmId_fkey" FOREIGN KEY ("algorithmId") REFERENCES "Algorithm"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "AlgorithmsForCase" ADD CONSTRAINT "AlgorithmsForCase_caseId_fkey" FOREIGN KEY ("caseId") REFERENCES "Case"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Method" ADD CONSTRAINT "Method_puzzleId_fkey" FOREIGN KEY ("puzzleId") REFERENCES "Puzzle"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Method" ADD CONSTRAINT "Method_visualizationId_fkey" FOREIGN KEY ("visualizationId") REFERENCES "Visualization"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_CaseToSet" ADD CONSTRAINT "_CaseToSet_A_fkey" FOREIGN KEY ("A") REFERENCES "Case"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_CaseToSet" ADD CONSTRAINT "_CaseToSet_B_fkey" FOREIGN KEY ("B") REFERENCES "Set"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_MethodToSet" ADD CONSTRAINT "_MethodToSet_A_fkey" FOREIGN KEY ("A") REFERENCES "Method"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_MethodToSet" ADD CONSTRAINT "_MethodToSet_B_fkey" FOREIGN KEY ("B") REFERENCES "Set"("id") ON DELETE CASCADE ON UPDATE CASCADE;
28 changes: 3 additions & 25 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ generator client {
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
directUrl = env("DIRECT_URL")
relationMode = "prisma"
provider = "postgresql"
url = env("DATABASE_URL")
directUrl = env("DIRECT_URL")
}

model Account {
Expand All @@ -29,7 +28,6 @@ model Account {
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@unique([provider, providerAccountId])
@@index([userId])
}

model Session {
Expand All @@ -38,8 +36,6 @@ model Session {
userId String
expires DateTime
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@index([userId])
}

model User {
Expand Down Expand Up @@ -72,8 +68,6 @@ model Puzzle {
sets Set[]
cases Case[]
methods Method[]
@@index([visualizationId])
}

model Set {
Expand All @@ -87,9 +81,6 @@ model Set {
cases Case[]
methods Method[]
methodId String?
@@index([puzzleId])
@@index([visualizationId])
}

model Case {
Expand All @@ -99,20 +90,13 @@ model Case {
puzzle Puzzle @relation(fields: [puzzleId], references: [id])
puzzleId String
sets Set[]
algorithms Algorithm[]
algorithmsForCase AlgorithmsForCase[] // TODO: rename to "algorithms" when that namespace is available
@@index([puzzleId])
}

model Algorithm {
id String @id @default(cuid())
moves String
algorithmsForCase AlgorithmsForCase[] // TODO: rename to "cases" when Case.algorithms is available
Case Case? @relation(fields: [caseId], references: [id])
caseId String?
@@index([caseId])
}

model AlgorithmsForCase {
Expand All @@ -121,9 +105,6 @@ model AlgorithmsForCase {
algorithmId String
case Case @relation(fields: [caseId], references: [id])
caseId String
@@index([algorithmId])
@@index([caseId])
}

model Visualization {
Expand All @@ -144,7 +125,4 @@ model Method {
puzzleId String
visualization Visualization @relation(fields: [visualizationId], references: [id])
visualizationId String
@@index([puzzleId])
@@index([visualizationId])
}

0 comments on commit 9faacc3

Please sign in to comment.