Skip to content

Commit

Permalink
feat: add v2 migration flag to user schema
Browse files Browse the repository at this point in the history
  • Loading branch information
WhyAsh5114 committed Oct 16, 2024
1 parent f519aa8 commit fd2deec
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "User" ADD COLUMN "migratedFromV2" BOOL;
1 change: 1 addition & 0 deletions prisma/schema/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ model User {
exerciseSplits ExerciseSplit[]
mesocycles Mesocycle[]
workouts Workout[]
migratedFromV2 Boolean?
}

model Account {
Expand Down
11 changes: 9 additions & 2 deletions src/lib/trpc/routes/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ function toPascalCase(text: V2MuscleGroup) {
async function getPrismaAndMongoUser(userId: string) {
const prismaUser = await prisma.user.findUnique({
where: { id: userId },
select: { email: true, accounts: { select: { providerAccountId: true } } }
select: { email: true, accounts: { select: { providerAccountId: true } }, migratedFromV2: true }
});
if (!prismaUser) {
throw new TRPCError({ message: 'User not found in current database', code: 'BAD_REQUEST' });
}

if (prismaUser.migratedFromV2) {
throw new TRPCError({ message: 'Migration has already been performed', code: 'CONFLICT' });
}

const client = await clientPromise;
const mongoUser = await client.db().collection('users').findOne({ email: prismaUser?.email });
if (!mongoUser) {
Expand Down Expand Up @@ -451,6 +455,8 @@ export const users = t.router({
})
});

const updateUser = prisma.user.update({ where: { id: ctx.userId }, data: { migratedFromV2: true } });

await prisma.$transaction([
exerciseSplitCreate,
exerciseSplitDayCreate,
Expand All @@ -463,7 +469,8 @@ export const users = t.router({
workoutExerciseSetCreate,
workoutOfMesocycleCreate,
restDaysCreate,
restWorkoutsOfMesocycle
restWorkoutsOfMesocycle,
updateUser
]);
})
});
Loading

0 comments on commit fd2deec

Please sign in to comment.