Skip to content

Commit

Permalink
db-work: migrate ids to strings
Browse files Browse the repository at this point in the history
  • Loading branch information
konstrybakov committed Oct 6, 2024
1 parent efa41e0 commit 39bf6db
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 19 deletions.
2 changes: 1 addition & 1 deletion app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Toaster } from '@/components/ui/sonner'
import { ClerkProvider } from '@clerk/nextjs'
import type { Metadata } from 'next'
import '../globals.css'
import './globals.css'
import { cn } from '@/lib/utils/cn'
import { Menu } from './_components/menu'
import { sans } from './font'
Expand Down
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"rules": {
"correctness": {
"noUnusedImports": {
"level": "error"
"level": "off"
}
}
},
Expand Down
99 changes: 99 additions & 0 deletions prisma/migrations/20241006041445_make_ids_string/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
Warnings:
- The primary key for the `categories` table will be changed. If it partially fails, the table could be left without primary key constraint.
- The primary key for the `examples` table will be changed. If it partially fails, the table could be left without primary key constraint.
- The primary key for the `practice_attempts` table will be changed. If it partially fails, the table could be left without primary key constraint.
- The primary key for the `word_progress` table will be changed. If it partially fails, the table could be left without primary key constraint.
- The primary key for the `words` table will be changed. If it partially fails, the table could be left without primary key constraint.
- Made the column `practice_session_id` on table `practice_attempts` required. This step will fail if there are existing NULL values in that column.
*/
-- DropForeignKey
ALTER TABLE "_CategoryToWord" DROP CONSTRAINT "_CategoryToWord_A_fkey";

-- DropForeignKey
ALTER TABLE "_CategoryToWord" DROP CONSTRAINT "_CategoryToWord_B_fkey";

-- DropForeignKey
ALTER TABLE "examples" DROP CONSTRAINT "examples_word_id_fkey";

-- DropForeignKey
ALTER TABLE "practice_attempts" DROP CONSTRAINT "practice_attempts_practice_session_id_fkey";

-- DropForeignKey
ALTER TABLE "practice_attempts" DROP CONSTRAINT "practice_attempts_word_id_fkey";

-- DropForeignKey
ALTER TABLE "practice_attempts" DROP CONSTRAINT "practice_attempts_word_progress_id_fkey";

-- DropForeignKey
ALTER TABLE "word_progress" DROP CONSTRAINT "word_progress_word_id_fkey";

-- AlterTable
ALTER TABLE "PracticeSession" ALTER COLUMN "words" SET DATA TYPE TEXT[];

-- AlterTable
ALTER TABLE "_CategoryToWord" ALTER COLUMN "A" SET DATA TYPE TEXT,
ALTER COLUMN "B" SET DATA TYPE TEXT;

-- AlterTable
ALTER TABLE "categories" DROP CONSTRAINT "categories_pkey",
ALTER COLUMN "id" DROP DEFAULT,
ALTER COLUMN "id" SET DATA TYPE TEXT,
ADD CONSTRAINT "categories_pkey" PRIMARY KEY ("id");
DROP SEQUENCE "categories_id_seq";

-- AlterTable
ALTER TABLE "examples" DROP CONSTRAINT "examples_pkey",
ALTER COLUMN "id" DROP DEFAULT,
ALTER COLUMN "id" SET DATA TYPE TEXT,
ALTER COLUMN "word_id" SET DATA TYPE TEXT,
ADD CONSTRAINT "examples_pkey" PRIMARY KEY ("id");
DROP SEQUENCE "examples_id_seq";

-- AlterTable
ALTER TABLE "practice_attempts" DROP CONSTRAINT "practice_attempts_pkey",
ALTER COLUMN "id" DROP DEFAULT,
ALTER COLUMN "id" SET DATA TYPE TEXT,
ALTER COLUMN "word_progress_id" SET DATA TYPE TEXT,
ALTER COLUMN "word_id" SET DATA TYPE TEXT,
ALTER COLUMN "practice_session_id" SET NOT NULL,
ADD CONSTRAINT "practice_attempts_pkey" PRIMARY KEY ("id");
DROP SEQUENCE "practice_attempts_id_seq";

-- AlterTable
ALTER TABLE "word_progress" DROP CONSTRAINT "word_progress_pkey",
ALTER COLUMN "id" DROP DEFAULT,
ALTER COLUMN "id" SET DATA TYPE TEXT,
ALTER COLUMN "word_id" SET DATA TYPE TEXT,
ADD CONSTRAINT "word_progress_pkey" PRIMARY KEY ("id");
DROP SEQUENCE "word_progress_id_seq";

-- AlterTable
ALTER TABLE "words" DROP CONSTRAINT "words_pkey",
ALTER COLUMN "id" DROP DEFAULT,
ALTER COLUMN "id" SET DATA TYPE TEXT,
ADD CONSTRAINT "words_pkey" PRIMARY KEY ("id");
DROP SEQUENCE "words_id_seq";

-- AddForeignKey
ALTER TABLE "examples" ADD CONSTRAINT "examples_word_id_fkey" FOREIGN KEY ("word_id") REFERENCES "words"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "word_progress" ADD CONSTRAINT "word_progress_word_id_fkey" FOREIGN KEY ("word_id") REFERENCES "words"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "practice_attempts" ADD CONSTRAINT "practice_attempts_word_progress_id_fkey" FOREIGN KEY ("word_progress_id") REFERENCES "word_progress"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "practice_attempts" ADD CONSTRAINT "practice_attempts_word_id_fkey" FOREIGN KEY ("word_id") REFERENCES "words"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "practice_attempts" ADD CONSTRAINT "practice_attempts_practice_session_id_fkey" FOREIGN KEY ("practice_session_id") REFERENCES "PracticeSession"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_CategoryToWord" ADD CONSTRAINT "_CategoryToWord_A_fkey" FOREIGN KEY ("A") REFERENCES "categories"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "_CategoryToWord" ADD CONSTRAINT "_CategoryToWord_B_fkey" FOREIGN KEY ("B") REFERENCES "words"("id") ON DELETE CASCADE ON UPDATE CASCADE;
34 changes: 17 additions & 17 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ generator client {
}

model Word {
id Int @id @default(autoincrement())
id String @id @default(cuid())
word String
translation String
Expand Down Expand Up @@ -54,13 +54,13 @@ enum DifficultyCategory {
}

model Example {
id Int @id @default(autoincrement())
id String @id @default(cuid())
original String
translation String
word Word @relation(fields: [wordId], references: [id], onDelete: Cascade)
wordId Int @map("word_id")
word Word @relation(fields: [wordId], references: [id], onDelete: Cascade)
wordId String @map("word_id")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
Expand All @@ -69,7 +69,7 @@ model Example {
}

model Category {
id Int @id @default(autoincrement())
id String @id @default(cuid())
name String @unique
Expand All @@ -85,7 +85,7 @@ model Category {

// https://linear.app/well-then/issue/WLTH-106/wordprogress-can-be-missed-when-creating-word
model WordProgress {
id Int @id @default(autoincrement())
id String @id @default(cuid())
algorithm Algorithm @default(SM2)
Expand All @@ -98,8 +98,8 @@ model WordProgress {
learningStep Int @default(0) @map("learning_step")
phase Phase @default(Learning)
word Word @relation(fields: [wordId], references: [id], onDelete: Cascade)
wordId Int @unique @map("word_id")
word Word @relation(fields: [wordId], references: [id], onDelete: Cascade)
wordId String @unique @map("word_id")
userId String @map("user_id")
Expand All @@ -124,7 +124,7 @@ enum Phase {
}

model PracticeAttempt {
id Int @id @default(autoincrement())
id String @id @default(cuid())
reviewDate DateTime @default(now()) @map("review_date")
grade Int
Expand All @@ -138,15 +138,15 @@ model PracticeAttempt {
// HintUsed - boolean
wordProgress WordProgress @relation(fields: [wordProgressId], references: [id], onDelete: Cascade)
wordProgressId Int @map("word_progress_id")
wordProgressId String @map("word_progress_id")
word Word @relation(fields: [wordId], references: [id], onDelete: Cascade)
wordId Int @map("word_id")
word Word @relation(fields: [wordId], references: [id], onDelete: Cascade)
wordId String @map("word_id")
userId String @map("user_id")
practiceSession PracticeSession? @relation(fields: [practiceSessionId], references: [id])
practiceSessionId String? @map("practice_session_id")
practiceSession PracticeSession @relation(fields: [practiceSessionId], references: [id])
practiceSessionId String @map("practice_session_id")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
Expand All @@ -163,9 +163,9 @@ enum ReviewMethod {
model PracticeSession {
id String @id @default(cuid())
size Int? @default(10)
words Int[]
completed Boolean @default(false)
size Int? @default(10)
words String[]
completed Boolean @default(false)
phase Phase[]
practiceType PracticeSessionType[]
Expand Down

0 comments on commit 39bf6db

Please sign in to comment.