Skip to content

Commit

Permalink
add a couple of cascade deletes for foreign key relations
Browse files Browse the repository at this point in the history
  • Loading branch information
mipyykko committed May 25, 2023
1 parent a1eadc8 commit e1da66b
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ export const updateExerciseCompletion = async (
n_points: Number(message.n_points),
completed: { set: message.completed },
attempted: {
set: message.attempted !== null ? message.attempted : false,
set: message.attempted ?? false,
},
exercise_completion_required_actions: {
create: createActions,
Expand Down
105 changes: 105 additions & 0 deletions backend/migrations/20230525091923_alter-tables-add-cascade-deletes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { Knex } from "knex"

export async function up(knex: Knex): Promise<void> {
await knex.raw(`
ALTER TABLE "exercise_completion_required_actions"
DROP CONSTRAINT "exercise_completion_required_actions_exercise_completion_i_fkey",
ADD CONSTRAINT "exercise_completion_required_actions_exercise_completion_i_fkey"
FOREIGN KEY ("exercise_completion_id") REFERENCES "exercise_completion"("id")
ON DELETE CASCADE;
`)
await knex.raw(`
ALTER TABLE "open_university_registration_link"
DROP CONSTRAINT "open_university_registration_link_course_id_fkey",
ADD CONSTRAINT "open_university_registration_link_course_id_fkey"
FOREIGN KEY ("course_id") REFERENCES "course"("id")
ON DELETE CASCADE;
`)
await knex.raw(`
ALTER TABLE "organization_translation"
DROP CONSTRAINT "organization_translation_organization_id_fkey",
ADD CONSTRAINT "organization_translation_organization_id_fkey"
FOREIGN KEY ("organization_id") REFERENCES "organization"("id")
ON DELETE CASCADE;
`)
await knex.raw(`
ALTER TABLE "study_module_translation"
DROP CONSTRAINT "study_module_translation_study_module_id_fkey",
ADD CONSTRAINT "study_module_translation_study_module_id_fkey"
FOREIGN KEY ("study_module_id") REFERENCES "study_module"("id")
ON DELETE CASCADE;
`)
await knex.raw(`
ALTER TABLE "course_alias"
DROP CONSTRAINT "course_alias_course_id_fkey",
ADD CONSTRAINT "course_alias_course_id_fkey"
FOREIGN KEY ("course_id") REFERENCES "course"("id")
ON DELETE CASCADE;
`)
await knex.raw(`
ALTER TABLE "course_translation"
DROP CONSTRAINT "course_translation_course_id_fkey",
ADD CONSTRAINT "course_translation_course_id_fkey"
FOREIGN KEY ("course_id") REFERENCES "course"("id")
ON DELETE CASCADE;
`)
await knex.raw(`
ALTER TABLE "course_variant"
DROP CONSTRAINT "course_variant_course_id_fkey",
ADD CONSTRAINT "course_variant_course_id_fkey"
FOREIGN KEY ("course_id") REFERENCES "course"("id")
ON DELETE CASCADE;
`)
}

export async function down(knex: Knex): Promise<void> {
await knex.raw(`
ALTER TABLE "exercise_completion_required_actions"
DROP CONSTRAINT "exercise_completion_required_actions_exercise_completion_i_fkey",
ADD CONSTRAINT "exercise_completion_required_actions_exercise_completion_i_fkey"
FOREIGN KEY ("exercise_completion_id") REFERENCES "exercise_completion"("id")
ON DELETE SET NULL;
`)
await knex.raw(`
ALTER TABLE "open_university_registration_link"
DROP CONSTRAINT "open_university_registration_link_course_id_fkey",
ADD CONSTRAINT "open_university_registration_link_course_id_fkey"
FOREIGN KEY ("course_id") REFERENCES "course"("id")
ON DELETE SET NULL;
`)
await knex.raw(`
ALTER TABLE "organization_translation"
DROP CONSTRAINT "organization_translation_organization_id_fkey",
ADD CONSTRAINT "organization_translation_organization_id_fkey"
FOREIGN KEY ("organization_id") REFERENCES "organization"("id")
ON DELETE SET NULL;
`)
await knex.raw(`
ALTER TABLE "study_module_translation"
DROP CONSTRAINT "study_module_translation_study_module_id_fkey",
ADD CONSTRAINT "study_module_translation_study_module_id_fkey"
FOREIGN KEY ("study_module_id") REFERENCES "study_module"("id")
ON DELETE SET NULL;
`)
await knex.raw(`
ALTER TABLE "course_alias"
DROP CONSTRAINT "course_alias_course_id_fkey",
ADD CONSTRAINT "course_alias_course_id_fkey"
FOREIGN KEY ("course_id") REFERENCES "course"("id")
ON DELETE SET NULL;
`)
await knex.raw(`
ALTER TABLE "course_translation"
DROP CONSTRAINT "course_translation_course_id_fkey",
ADD CONSTRAINT "course_translation_course_id_fkey"
FOREIGN KEY ("course_id") REFERENCES "course"("id")
ON DELETE SET NULL;
`)
await knex.raw(`
ALTER TABLE "course_variant"
DROP CONSTRAINT "course_variant_course_id_fkey",
ADD CONSTRAINT "course_variant_course_id_fkey"
FOREIGN KEY ("course_id") REFERENCES "course"("id")
ON DELETE SET NULL;
`)
}
14 changes: 7 additions & 7 deletions backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ model CourseAlias {
course_id String? @db.Uuid
created_at DateTime @default(now())
updated_at DateTime @default(now()) @updatedAt
course Course? @relation(fields: [course_id], references: [id], onUpdate: NoAction)
course Course? @relation(fields: [course_id], references: [id], onDelete: Cascade, onUpdate: NoAction)
@@index([course_id])
@@map("course_alias")
Expand Down Expand Up @@ -182,7 +182,7 @@ model CourseTranslation {
course_id String? @db.Uuid
created_at DateTime @default(now())
updated_at DateTime @default(now()) @updatedAt
course Course? @relation(fields: [course_id], references: [id], onUpdate: NoAction)
course Course? @relation(fields: [course_id], references: [id], onDelete: Cascade, onUpdate: NoAction)
@@index([course_id, language])
@@index([name])
Expand All @@ -197,7 +197,7 @@ model CourseVariant {
course_id String? @db.Uuid
created_at DateTime @default(now())
updated_at DateTime @default(now()) @updatedAt
course Course? @relation(fields: [course_id], references: [id], onUpdate: NoAction)
course Course? @relation(fields: [course_id], references: [id], onDelete: Cascade, onUpdate: NoAction)
@@index([course_id])
@@index([slug])
Expand Down Expand Up @@ -290,7 +290,7 @@ model ExerciseCompletionRequiredAction {
id String @id @default(dbgenerated("extensions.uuid_generate_v4()")) @db.Uuid
value String
exercise_completion_id String? @db.Uuid
exercise_completion ExerciseCompletion? @relation(fields: [exercise_completion_id], references: [id], onUpdate: NoAction)
exercise_completion ExerciseCompletion? @relation(fields: [exercise_completion_id], references: [id], onDelete: Cascade, onUpdate: NoAction)
@@map("exercise_completion_required_actions")
}
Expand Down Expand Up @@ -324,7 +324,7 @@ model OpenUniversityRegistrationLink {
tiers Json? @db.Json
created_at DateTime @default(now())
updated_at DateTime @default(now()) @updatedAt
course Course? @relation(fields: [course_id], references: [id], onUpdate: NoAction)
course Course? @relation(fields: [course_id], references: [id], onDelete: Cascade, onUpdate: NoAction)
@@index([course_id])
@@map("open_university_registration_link")
Expand Down Expand Up @@ -373,7 +373,7 @@ model OrganizationTranslation {
organization_id String? @db.Uuid
created_at DateTime @default(now())
updated_at DateTime @default(now()) @updatedAt
organization Organization? @relation(fields: [organization_id], references: [id], onUpdate: NoAction)
organization Organization? @relation(fields: [organization_id], references: [id], onDelete: Cascade, onUpdate: NoAction)
@@index([organization_id])
@@map("organization_translation")
Expand Down Expand Up @@ -429,7 +429,7 @@ model StudyModuleTranslation {
study_module_id String? @db.Uuid
created_at DateTime @default(now())
updated_at DateTime @default(now()) @updatedAt
study_module StudyModule? @relation(fields: [study_module_id], references: [id], onUpdate: NoAction)
study_module StudyModule? @relation(fields: [study_module_id], references: [id], onDelete: Cascade, onUpdate: NoAction)
@@index([study_module_id, language])
@@map("study_module_translation")
Expand Down
22 changes: 7 additions & 15 deletions backend/util/courseMutationHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ type CourseMutationFunction<
params: CreateMutationParams<Relation, CourseRelationType<Relation>, IdKey>,
) => Promise<CreateMutationReturn<Relation>>

// - given the data (a course field like course_translations) and the field name, returns the Prisma mutations to create, update, or delete data
// given the data (a course field like course_translations) and the field name,
// returns the Prisma mutations to create, update, or delete data
const createCourseMutationFunction =
<
Relation extends CourseRelation = CourseRelation,
Expand All @@ -96,16 +97,7 @@ const createCourseMutationFunction =
ctx: Context,
slug: string,
): CourseMutationFunction<Relation> =>
/*async <
Relation extends CourseRelation,
// RelationInstance extends CourseRelationType<Relation> = CourseRelationType<Relation>,
IdKey extends CourseRelationKey<Relation> = CourseRelationKey<Relation>,
>*/ async (
{ data, relation, id = "id" as IdKey } /*: CreateMutationParams<Relation>*/,
) => {
/*: Promise<
CreateMutationReturn<Relation>
> => {*/
async ({ data, relation, id = "id" as IdKey }) => {
if (!isNotNullOrUndefined(data)) {
return undefined
}
Expand All @@ -126,17 +118,17 @@ const createCourseMutationFunction =
const hasIdFilter = hasId<Relation>(id as IdKey)
const hasNotIdFilter = hasNotId<Relation>(id as IdKey)

const mutation = {} as NonNullable<CreateMutationReturn<Relation>> //CreateMutationReturn<Relation>
const mutation = {} as NonNullable<CreateMutationReturn<Relation>>
mutation.create = data
.filter(hasNotIdFilter) // (t) => !t.id
.filter(hasNotIdFilter)
.map((t) => Object.assign({}, t, { [id]: undefined }))
mutation.updateMany = data
.filter(isNotNullOrUndefined)
.filter(hasIdFilter) // (t) => !!t.id)
.filter(hasIdFilter)
.map((t) => {
return {
where: { [id]: t[id as IdKey] },
data: t, //{ ...t, id: undefined },
data: t,
}
})
mutation.deleteMany = filterNotIncluded(existing ?? [], data, id)
Expand Down

0 comments on commit e1da66b

Please sign in to comment.