diff --git a/packages/db/prisma/migrations/20241105095800_rag_schema/migration.sql b/packages/db/prisma/migrations/20241105095800_rag_schema/migration.sql new file mode 100644 index 000000000..e2aebdc7b --- /dev/null +++ b/packages/db/prisma/migrations/20241105095800_rag_schema/migration.sql @@ -0,0 +1,49 @@ +/* + Warnings: + + - You are about to drop the column `data` on the `ingest_lesson_plan_part` table. All the data in the column will be lost. + - You are about to drop the column `valueText` on the `ingest_lesson_plan_part` table. All the data in the column will be lost. + - Added the required column `value_json` to the `ingest_lesson_plan_part` table without a default value. This is not possible if the table is not empty. + - Added the required column `value_text` to the `ingest_lesson_plan_part` table without a default value. This is not possible if the table is not empty. + +*/ +-- CreateSchema +CREATE SCHEMA IF NOT EXISTS "rag"; + +-- AlterTable -- updated this to be RENAME instead of DROP and ADD +ALTER TABLE "ingest"."ingest_lesson_plan_part" + RENAME COLUMN data TO value_json; + +ALTER TABLE "ingest"."ingest_lesson_plan_part" + RENAME COLUMN "valueText" TO value_text; + +-- CreateTable +CREATE TABLE "rag"."rag_lesson_plans" ( + "id" TEXT NOT NULL, + "oak_lesson_id" INTEGER NOT NULL, + "ingest_lesson_id" TEXT, + "lesson_plan" JSONB NOT NULL, + "subject_slug" TEXT NOT NULL, + "key_stage_slug" TEXT NOT NULL, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "rag_lesson_plans_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "rag"."rag_lesson_plan_parts" ( + "id" TEXT NOT NULL, + "rag_lesson_plan_id" TEXT NOT NULL, + "key" TEXT NOT NULL, + "value_text" TEXT NOT NULL, + "value_json" JSONB NOT NULL, + "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(3) NOT NULL, + "embedding" vector(256) NOT NULL, + + CONSTRAINT "rag_lesson_plan_parts_pkey" PRIMARY KEY ("id") +); + +-- AddForeignKey +ALTER TABLE "rag"."rag_lesson_plan_parts" ADD CONSTRAINT "rag_lesson_plan_parts_rag_lesson_plan_id_fkey" FOREIGN KEY ("rag_lesson_plan_id") REFERENCES "rag"."rag_lesson_plans"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/packages/db/prisma/schema.prisma b/packages/db/prisma/schema.prisma index 35c2d270c..1cef5a119 100644 --- a/packages/db/prisma/schema.prisma +++ b/packages/db/prisma/schema.prisma @@ -11,7 +11,7 @@ datasource db { url = env("PRISMA_ACCELERATE_DATABASE_URL") directUrl = env("DIRECT_DATABASE_URL") extensions = [vector, pg_trgm] - schemas = ["public", "ingest"] + schemas = ["public", "ingest", "rag"] } generator zod { @@ -985,7 +985,7 @@ enum QuizAnswerStatus { model Ingest { id String @id @default(cuid()) - config Json @default("{}") @map("config") @db.JsonB + config Json @map("config") @db.JsonB status String createdAt DateTime @default(now()) @map("created_at") updatedAt DateTime @updatedAt @map("updated_at") @@ -1063,8 +1063,8 @@ model IngestLessonPlanPart { ingestId String @map("ingest_id") batchId String? @map("batch_id") key String - valueJson Json @map("data") @db.JsonB - valueText String + valueJson Json @map("value_json") @db.JsonB + valueText String @map("value_text") embedding Unsupported("vector(256)")? createdAt DateTime @default(now()) @map("created_at") updatedAt DateTime @updatedAt @map("updated_at") @@ -1110,3 +1110,34 @@ model IngestError { @@map("ingest_error") @@schema("ingest") } + +model RagLessonPlan { + id String @id @default(cuid()) + oakLessonId Int @map("oak_lesson_id") + ingestLessonId String? @map("ingest_lesson_id") + lessonPlan Json @map("lesson_plan") @db.JsonB + subjectSlug String @map("subject_slug") + keyStageSlug String @map("key_stage_slug") + createdAt DateTime @default(now()) @map("created_at") + updatedAt DateTime @updatedAt @map("updated_at") + ragLessonPlanPart RagLessonPlanPart[] + + @@map("rag_lesson_plans") + @@schema("rag") +} + +model RagLessonPlanPart { + id String @id @default(cuid()) + ragLessonPlanId String @map("rag_lesson_plan_id") + key String + valueText String @map("value_text") + valueJson Json @map("value_json") @db.JsonB + createdAt DateTime @default(now()) @map("created_at") + updatedAt DateTime @updatedAt @map("updated_at") + + ragLessonPlan RagLessonPlan @relation(fields: [ragLessonPlanId], references: [id], onDelete: Cascade) + embedding Unsupported("vector(256)") + + @@map("rag_lesson_plan_parts") + @@schema("rag") +}