Skip to content

Commit

Permalink
Chore: upgrade prisma and update referential actions (#1192)
Browse files Browse the repository at this point in the history
* upgrade Prisma to v4; use nexus-plugin-prisma fork; rename db relations and indexes according to Prisma default convention; fix Nexus scalar typings

* add explicit uuid casts to

* remove old prisma patches

* add a couple of cascade deletes for foreign key relations

* fix tests: no more orphaned actions
  • Loading branch information
mipyykko committed May 25, 2023
1 parent b598d17 commit 2922719
Show file tree
Hide file tree
Showing 49 changed files with 4,998 additions and 1,431 deletions.
30 changes: 16 additions & 14 deletions backend/api/routes/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,20 +254,22 @@ export class CompletionController extends Controller {
user: User,
): Promise<Completion | null> => {
return (
await this.ctx.prisma.user
.findUnique({
where: {
id: user.id,
},
})
.completions({
where: {
course_id: course.completions_handled_by_id ?? course.id,
},
orderBy: { created_at: "asc" },
take: 1,
})
)?.[0]
(
await this.ctx.prisma.user
.findUnique({
where: {
id: user.id,
},
})
.completions({
where: {
course_id: course.completions_handled_by_id ?? course.id,
},
orderBy: { created_at: "asc" },
take: 1,
})
)?.[0] ?? null
)
}

recheckCompletion = async (
Expand Down
151 changes: 78 additions & 73 deletions backend/api/routes/progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,37 +311,39 @@ export class ProgressController extends Controller {
}

logger.info("Querying existing progresses and completions")
const beforeParentProgresses = await prisma.course
.findUnique({
where: {
id: BAIParentCourse,
},
})
.user_course_progresses({
distinct: ["user_id"],
orderBy: {
created_at: "asc",
},
include: {
user: true,
},
})
const beforeParentProgresses =
(await prisma.course
.findUnique({
where: {
id: BAIParentCourse,
},
})
.user_course_progresses({
distinct: ["user_id"],
orderBy: {
created_at: "asc",
},
include: {
user: true,
},
})) ?? []

const beforeCompletions = await prisma.course
.findUnique({
where: {
id: BAIParentCourse,
},
})
.completions({
distinct: ["user_id"],
orderBy: {
created_at: "asc",
},
include: {
user: true,
},
})
const beforeCompletions =
(await prisma.course
.findUnique({
where: {
id: BAIParentCourse,
},
})
.completions({
distinct: ["user_id"],
orderBy: {
created_at: "asc",
},
include: {
user: true,
},
})) ?? []

const getUsers = (arr: Array<object & { user: User | null }>) =>
arr?.map((e) => e.user).filter(notEmpty) ?? []
Expand Down Expand Up @@ -418,19 +420,20 @@ export class ProgressController extends Controller {
advancedBAICourse,
]) {
logger.info(`Handling ${course.slug}`)
const userCourseProgresses = await prisma.course
.findUnique({
where: {
id: course.id,
},
})
.user_course_progresses({
distinct: ["user_id"],
orderBy: { created_at: "asc" },
include: {
user: true,
},
})
const userCourseProgresses =
(await prisma.course
.findUnique({
where: {
id: course.id,
},
})
.user_course_progresses({
distinct: ["user_id"],
orderBy: { created_at: "asc" },
include: {
user: true,
},
})) ?? []

if (userCourseProgresses.length) {
logger.info(
Expand Down Expand Up @@ -479,37 +482,39 @@ export class ProgressController extends Controller {
}

logger.info("Querying updated progresses and completions")
const afterParentProgresses = await prisma.course
.findUnique({
where: {
id: BAIParentCourse,
},
})
.user_course_progresses({
distinct: ["user_id"],
orderBy: {
created_at: "asc",
},
include: {
user: true,
},
})
const afterParentProgresses =
(await prisma.course
.findUnique({
where: {
id: BAIParentCourse,
},
})
.user_course_progresses({
distinct: ["user_id"],
orderBy: {
created_at: "asc",
},
include: {
user: true,
},
})) ?? []

const afterCompletions = await prisma.course
.findUnique({
where: {
id: BAIParentCourse,
},
})
.completions({
distinct: ["user_id"],
orderBy: {
created_at: "asc",
},
include: {
user: true,
},
})
const afterCompletions =
(await prisma.course
.findUnique({
where: {
id: BAIParentCourse,
},
})
.completions({
distinct: ["user_id"],
orderBy: {
created_at: "asc",
},
include: {
user: true,
},
})) ?? []

const result = {
progresses: {
Expand Down
39 changes: 20 additions & 19 deletions backend/api/routes/storedData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ export class StoredDataController extends Controller {
try {
const existingStoredData = await prisma.storedData.findUnique({
where: {
user_id_course_id: {
user_id: user.id,
course_id_user_id: {
course_id: course.id,
user_id: user.id,
},
},
})
Expand All @@ -59,9 +59,9 @@ export class StoredDataController extends Controller {

await prisma.storedData.update({
where: {
user_id_course_id: {
user_id: user.id,
course_id_user_id: {
course_id: course.id,
user_id: user.id,
},
},
data: {
Expand Down Expand Up @@ -98,25 +98,26 @@ export class StoredDataController extends Controller {
return ownershipResult.error
}

const storedData = await prisma.course
.findUnique({
where: { id: course.id },
})
.stored_data({
include: {
user: {
include: {
completions: {
where: {
course_id: course.completions_handled_by_id ?? course.id,
const storedData =
(await prisma.course
.findUnique({
where: { id: course.id },
})
.stored_data({
include: {
user: {
include: {
completions: {
where: {
course_id: course.completions_handled_by_id ?? course.id,
},
orderBy: { created_at: "asc" },
take: 1,
},
orderBy: { created_at: "asc" },
take: 1,
},
},
},
},
})
})) ?? []

const mappedStoredData = storedData.map((data) => ({
user: omit(data.user, "completions"),
Expand Down
4 changes: 2 additions & 2 deletions backend/bin/fetchUserAppDatum.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DateTime } from "luxon"

import { Course, PrismaClient, UserCourseSetting } from "@prisma/client"
import { Course, Prisma, PrismaClient, UserCourseSetting } from "@prisma/client"

import { CONFIG_NAME } from "../config"
import { UserInfo } from "../domain/UserInfo"
Expand Down Expand Up @@ -212,7 +212,7 @@ const saveCourseVariant = async (p: any) => {
}

const saveOther = async (p: any) => {
const other: any = old.other ?? {}
const other = (old.other as Prisma.JsonObject) ?? {}
if (p.value == "t") {
p.value = true
} else if (p.value == "f") {
Expand Down
11 changes: 6 additions & 5 deletions backend/bin/importOrganizations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ const upsertOrganization = async (org: OrganizationInfo) => {
information: org.information,
organization: { connect: { id: organization.id } },
}
const organizationTranslations = await prisma.organization
.findUnique({ where: { id: organization.id } })
.organization_translations({
where: { language: translationDetails.language },
})
const organizationTranslations =
(await prisma.organization
.findUnique({ where: { id: organization.id } })
.organization_translations({
where: { language: translationDetails.language },
})) ?? []
const organizationTranslationId = organizationTranslations.length
? organizationTranslations[0].id
: null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class StartedCourseCount extends Template {
const conditions = [
Prisma.sql`course_id = ${
course.inherit_settings_from_id ?? course.id
}`,
}::uuid`,
]
if (courseInstanceLanguage) {
conditions.push(Prisma.sql`language = ${courseInstanceLanguage}`)
Expand Down Expand Up @@ -74,7 +74,7 @@ export class CompletedCourseCount extends Template {
const conditions = [
Prisma.sql`course_id = ${
course.completions_handled_by_id ?? course.id
}`,
}::uuid`,
]
if (completionLanguage) {
conditions.push(
Expand Down Expand Up @@ -124,7 +124,7 @@ export class AtLeastOneExerciseCount extends Template {
COUNT(DISTINCT user_id)
FROM exercise_completion ec
JOIN exercise e ON ec.exercise_id = e.id
WHERE course_id = ${course.id}
WHERE course_id = ${course.id}::uuid
AND attempted = true;
`
)?.[0]?.count
Expand Down Expand Up @@ -173,7 +173,7 @@ export class AtLeastOneExerciseButNotCompletedEmails extends Template {
const conditions = [
Prisma.sql`course_id = ${
course.completions_handled_by_id ?? course.id
}`,
}::uuid`,
Prisma.sql`user_id IS NOT NULL`,
]
if (completionLanguage) {
Expand All @@ -191,7 +191,7 @@ export class AtLeastOneExerciseButNotCompletedEmails extends Template {
JOIN exercise e ON ec.exercise_id = e.id
JOIN "user" u ON ec.user_id = u.id
WHERE
e.course_id = ${course.id}
e.course_id = ${course.id}::uuid
AND
ec.attempted = true
AND
Expand Down
Loading

0 comments on commit 2922719

Please sign in to comment.