From fa2ba89a3b04d6e63872a40949e5a53934f328c8 Mon Sep 17 00:00:00 2001 From: WhyAsh5114 Date: Tue, 15 Oct 2024 17:55:08 +0530 Subject: [PATCH 1/2] fix: update type to include decimals as well --- prisma/schema/workout.prisma | 2 +- src/lib/trpc/routes/workouts.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/prisma/schema/workout.prisma b/prisma/schema/workout.prisma index eb99bd00..3426cc82 100644 --- a/prisma/schema/workout.prisma +++ b/prisma/schema/workout.prisma @@ -12,7 +12,7 @@ model Workout { id String @id @default(cuid()) /// @zod.string.cuid2() user User @relation(fields: [userId], references: [id], onDelete: Cascade) workoutOfMesocycle WorkoutOfMesocycle? - userBodyweight Int + userBodyweight Float startedAt DateTime endedAt DateTime workoutExercises WorkoutExercise[] diff --git a/src/lib/trpc/routes/workouts.ts b/src/lib/trpc/routes/workouts.ts index bcd42846..5b689d40 100644 --- a/src/lib/trpc/routes/workouts.ts +++ b/src/lib/trpc/routes/workouts.ts @@ -250,7 +250,7 @@ export const workouts = t.router({ } } }); - const lastBodyweight = data?.workoutsOfMesocycle.map((v) => v.workout.userBodyweight).filter((b) => b !== null)[0]; + const lastBodyweight = data?.workoutsOfMesocycle.map((wm) => wm.workout.userBodyweight)[0]; const userBodyweight = lastBodyweight ?? null; const todaysWorkoutData: TodaysWorkoutData = { From ac34343781dfcb1a350c70e7050ca21eecd851f6 Mon Sep 17 00:00:00 2001 From: WhyAsh5114 Date: Tue, 15 Oct 2024 19:00:55 +0530 Subject: [PATCH 2/2] fix: use float for now --- .../migration.sql | 36 +++++++++++++ .../migration.sql | 37 +++++++++++++ src/lib/zodSchemas/index.ts | 52 +++++++++---------- 3 files changed, 99 insertions(+), 26 deletions(-) create mode 100644 prisma/migrations/20241015125503_change_bodyweight_to_decimal_type/migration.sql create mode 100644 prisma/migrations/20241015132840_change_bodyweight_to_float_for_now/migration.sql diff --git a/prisma/migrations/20241015125503_change_bodyweight_to_decimal_type/migration.sql b/prisma/migrations/20241015125503_change_bodyweight_to_decimal_type/migration.sql new file mode 100644 index 00000000..ef3c602f --- /dev/null +++ b/prisma/migrations/20241015125503_change_bodyweight_to_decimal_type/migration.sql @@ -0,0 +1,36 @@ +/* + Warnings: + + - Changed the type of `userBodyweight` on the `Workout` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required. + +*/ + +-- Step 1: Add a new column with the DECIMAL type +BEGIN; +ALTER TABLE "Workout" +ADD COLUMN "newUserBodyweight" DECIMAL(5, 2); +COMMIT; + +-- Step 2: Migrate the existing data from the old column to the new one +BEGIN; +UPDATE "Workout" +SET "newUserBodyweight" = CAST("userBodyweight" AS DECIMAL(5, 2)); +COMMIT; + +-- Step 3: Ensure no null values exist in the new column before making it required +BEGIN; +ALTER TABLE "Workout" +ALTER COLUMN "newUserBodyweight" SET NOT NULL; +COMMIT; + +-- Step 4: Drop the old column +BEGIN; +ALTER TABLE "Workout" +DROP COLUMN "userBodyweight"; +COMMIT; + +-- Step 5: Rename the new column to the original name +BEGIN; +ALTER TABLE "Workout" +RENAME COLUMN "newUserBodyweight" TO "userBodyweight"; +COMMIT; diff --git a/prisma/migrations/20241015132840_change_bodyweight_to_float_for_now/migration.sql b/prisma/migrations/20241015132840_change_bodyweight_to_float_for_now/migration.sql new file mode 100644 index 00000000..6ccab8ab --- /dev/null +++ b/prisma/migrations/20241015132840_change_bodyweight_to_float_for_now/migration.sql @@ -0,0 +1,37 @@ +/* + Warnings: + + - Changed the type of `userBodyweight` on the `Workout` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required. + +*/ + +-- Step 1: Add a new column with the FLOAT type +BEGIN; +ALTER TABLE "Workout" +ADD COLUMN "tempUserBodyweight" FLOAT; +COMMIT; + +-- Step 2: Migrate the existing data from DECIMAL(5, 2) to FLOAT +BEGIN; +UPDATE "Workout" +SET "tempUserBodyweight" = "userBodyweight"; +COMMIT; + +-- Step 3: Ensure no null values exist in the new column before making it required +BEGIN; +ALTER TABLE "Workout" +ALTER COLUMN "tempUserBodyweight" SET NOT NULL; +COMMIT; + +-- Step 4: Drop the DECIMAL column +BEGIN; +ALTER TABLE "Workout" +DROP COLUMN "userBodyweight"; +COMMIT; + +-- Step 5: Rename the new column to the original name +BEGIN; +ALTER TABLE "Workout" +RENAME COLUMN "tempUserBodyweight" TO "userBodyweight"; +COMMIT; + diff --git a/src/lib/zodSchemas/index.ts b/src/lib/zodSchemas/index.ts index 08ced606..4b869689 100644 --- a/src/lib/zodSchemas/index.ts +++ b/src/lib/zodSchemas/index.ts @@ -278,7 +278,7 @@ export type WorkoutOfMesocycle = z.infer export const WorkoutSchema = z.object({ id: z.string().cuid2(), - userBodyweight: z.number().int(), + userBodyweight: z.number(), startedAt: z.coerce.date(), endedAt: z.coerce.date(), userId: z.string(), @@ -1791,7 +1791,7 @@ export const WorkoutWhereInputSchema: z.ZodType = z.ob OR: z.lazy(() => WorkoutWhereInputSchema).array().optional(), NOT: z.union([ z.lazy(() => WorkoutWhereInputSchema),z.lazy(() => WorkoutWhereInputSchema).array() ]).optional(), id: z.union([ z.lazy(() => StringFilterSchema),z.string() ]).optional(), - userBodyweight: z.union([ z.lazy(() => IntFilterSchema),z.number() ]).optional(), + userBodyweight: z.union([ z.lazy(() => FloatFilterSchema),z.number() ]).optional(), startedAt: z.union([ z.lazy(() => DateTimeFilterSchema),z.coerce.date() ]).optional(), endedAt: z.union([ z.lazy(() => DateTimeFilterSchema),z.coerce.date() ]).optional(), userId: z.union([ z.lazy(() => StringFilterSchema),z.string() ]).optional(), @@ -1819,7 +1819,7 @@ export const WorkoutWhereUniqueInputSchema: z.ZodType WorkoutWhereInputSchema),z.lazy(() => WorkoutWhereInputSchema).array() ]).optional(), OR: z.lazy(() => WorkoutWhereInputSchema).array().optional(), NOT: z.union([ z.lazy(() => WorkoutWhereInputSchema),z.lazy(() => WorkoutWhereInputSchema).array() ]).optional(), - userBodyweight: z.union([ z.lazy(() => IntFilterSchema),z.number().int() ]).optional(), + userBodyweight: z.union([ z.lazy(() => FloatFilterSchema),z.number() ]).optional(), startedAt: z.union([ z.lazy(() => DateTimeFilterSchema),z.coerce.date() ]).optional(), endedAt: z.union([ z.lazy(() => DateTimeFilterSchema),z.coerce.date() ]).optional(), userId: z.union([ z.lazy(() => StringFilterSchema),z.string() ]).optional(), @@ -1846,7 +1846,7 @@ export const WorkoutScalarWhereWithAggregatesInputSchema: z.ZodType WorkoutScalarWhereWithAggregatesInputSchema).array().optional(), NOT: z.union([ z.lazy(() => WorkoutScalarWhereWithAggregatesInputSchema),z.lazy(() => WorkoutScalarWhereWithAggregatesInputSchema).array() ]).optional(), id: z.union([ z.lazy(() => StringWithAggregatesFilterSchema),z.string() ]).optional(), - userBodyweight: z.union([ z.lazy(() => IntWithAggregatesFilterSchema),z.number() ]).optional(), + userBodyweight: z.union([ z.lazy(() => FloatWithAggregatesFilterSchema),z.number() ]).optional(), startedAt: z.union([ z.lazy(() => DateTimeWithAggregatesFilterSchema),z.coerce.date() ]).optional(), endedAt: z.union([ z.lazy(() => DateTimeWithAggregatesFilterSchema),z.coerce.date() ]).optional(), userId: z.union([ z.lazy(() => StringWithAggregatesFilterSchema),z.string() ]).optional(), @@ -3061,7 +3061,7 @@ export const WorkoutOfMesocycleUncheckedUpdateManyInputSchema: z.ZodType = z.object({ id: z.string().cuid2().optional(), - userBodyweight: z.number().int(), + userBodyweight: z.number(), startedAt: z.coerce.date(), endedAt: z.coerce.date(), user: z.lazy(() => UserCreateNestedOneWithoutWorkoutsInputSchema), @@ -3071,7 +3071,7 @@ export const WorkoutCreateInputSchema: z.ZodType = z. export const WorkoutUncheckedCreateInputSchema: z.ZodType = z.object({ id: z.string().cuid2().optional(), - userBodyweight: z.number().int(), + userBodyweight: z.number(), startedAt: z.coerce.date(), endedAt: z.coerce.date(), userId: z.string(), @@ -3081,7 +3081,7 @@ export const WorkoutUncheckedCreateInputSchema: z.ZodType = z.object({ id: z.union([ z.string().cuid2(),z.lazy(() => StringFieldUpdateOperationsInputSchema) ]).optional(), - userBodyweight: z.union([ z.number().int(),z.lazy(() => IntFieldUpdateOperationsInputSchema) ]).optional(), + userBodyweight: z.union([ z.number(),z.lazy(() => FloatFieldUpdateOperationsInputSchema) ]).optional(), startedAt: z.union([ z.coerce.date(),z.lazy(() => DateTimeFieldUpdateOperationsInputSchema) ]).optional(), endedAt: z.union([ z.coerce.date(),z.lazy(() => DateTimeFieldUpdateOperationsInputSchema) ]).optional(), user: z.lazy(() => UserUpdateOneRequiredWithoutWorkoutsNestedInputSchema).optional(), @@ -3091,7 +3091,7 @@ export const WorkoutUpdateInputSchema: z.ZodType = z. export const WorkoutUncheckedUpdateInputSchema: z.ZodType = z.object({ id: z.union([ z.string().cuid2(),z.lazy(() => StringFieldUpdateOperationsInputSchema) ]).optional(), - userBodyweight: z.union([ z.number().int(),z.lazy(() => IntFieldUpdateOperationsInputSchema) ]).optional(), + userBodyweight: z.union([ z.number(),z.lazy(() => FloatFieldUpdateOperationsInputSchema) ]).optional(), startedAt: z.union([ z.coerce.date(),z.lazy(() => DateTimeFieldUpdateOperationsInputSchema) ]).optional(), endedAt: z.union([ z.coerce.date(),z.lazy(() => DateTimeFieldUpdateOperationsInputSchema) ]).optional(), userId: z.union([ z.string(),z.lazy(() => StringFieldUpdateOperationsInputSchema) ]).optional(), @@ -3101,7 +3101,7 @@ export const WorkoutUncheckedUpdateInputSchema: z.ZodType = z.object({ id: z.string().cuid2().optional(), - userBodyweight: z.number().int(), + userBodyweight: z.number(), startedAt: z.coerce.date(), endedAt: z.coerce.date(), userId: z.string() @@ -3109,14 +3109,14 @@ export const WorkoutCreateManyInputSchema: z.ZodType = z.object({ id: z.union([ z.string().cuid2(),z.lazy(() => StringFieldUpdateOperationsInputSchema) ]).optional(), - userBodyweight: z.union([ z.number().int(),z.lazy(() => IntFieldUpdateOperationsInputSchema) ]).optional(), + userBodyweight: z.union([ z.number(),z.lazy(() => FloatFieldUpdateOperationsInputSchema) ]).optional(), startedAt: z.union([ z.coerce.date(),z.lazy(() => DateTimeFieldUpdateOperationsInputSchema) ]).optional(), endedAt: z.union([ z.coerce.date(),z.lazy(() => DateTimeFieldUpdateOperationsInputSchema) ]).optional(), }).strict(); export const WorkoutUncheckedUpdateManyInputSchema: z.ZodType = z.object({ id: z.union([ z.string().cuid2(),z.lazy(() => StringFieldUpdateOperationsInputSchema) ]).optional(), - userBodyweight: z.union([ z.number().int(),z.lazy(() => IntFieldUpdateOperationsInputSchema) ]).optional(), + userBodyweight: z.union([ z.number(),z.lazy(() => FloatFieldUpdateOperationsInputSchema) ]).optional(), startedAt: z.union([ z.coerce.date(),z.lazy(() => DateTimeFieldUpdateOperationsInputSchema) ]).optional(), endedAt: z.union([ z.coerce.date(),z.lazy(() => DateTimeFieldUpdateOperationsInputSchema) ]).optional(), userId: z.union([ z.string(),z.lazy(() => StringFieldUpdateOperationsInputSchema) ]).optional(), @@ -6950,7 +6950,7 @@ export const MesocycleCreateManyUserInputEnvelopeSchema: z.ZodType = z.object({ id: z.string().cuid2().optional(), - userBodyweight: z.number().int(), + userBodyweight: z.number(), startedAt: z.coerce.date(), endedAt: z.coerce.date(), workoutOfMesocycle: z.lazy(() => WorkoutOfMesocycleCreateNestedOneWithoutWorkoutInputSchema).optional(), @@ -6959,7 +6959,7 @@ export const WorkoutCreateWithoutUserInputSchema: z.ZodType = z.object({ id: z.string().cuid2().optional(), - userBodyweight: z.number().int(), + userBodyweight: z.number(), startedAt: z.coerce.date(), endedAt: z.coerce.date(), workoutOfMesocycle: z.lazy(() => WorkoutOfMesocycleUncheckedCreateNestedOneWithoutWorkoutInputSchema).optional(), @@ -7100,7 +7100,7 @@ export const WorkoutScalarWhereInputSchema: z.ZodType WorkoutScalarWhereInputSchema).array().optional(), NOT: z.union([ z.lazy(() => WorkoutScalarWhereInputSchema),z.lazy(() => WorkoutScalarWhereInputSchema).array() ]).optional(), id: z.union([ z.lazy(() => StringFilterSchema),z.string() ]).optional(), - userBodyweight: z.union([ z.lazy(() => IntFilterSchema),z.number() ]).optional(), + userBodyweight: z.union([ z.lazy(() => FloatFilterSchema),z.number() ]).optional(), startedAt: z.union([ z.lazy(() => DateTimeFilterSchema),z.coerce.date() ]).optional(), endedAt: z.union([ z.lazy(() => DateTimeFilterSchema),z.coerce.date() ]).optional(), userId: z.union([ z.lazy(() => StringFilterSchema),z.string() ]).optional(), @@ -7252,7 +7252,7 @@ export const UserUncheckedUpdateWithoutSessionsInputSchema: z.ZodType = z.object({ id: z.string().cuid2().optional(), - userBodyweight: z.number().int(), + userBodyweight: z.number(), startedAt: z.coerce.date(), endedAt: z.coerce.date(), user: z.lazy(() => UserCreateNestedOneWithoutWorkoutsInputSchema), @@ -7261,7 +7261,7 @@ export const WorkoutCreateWithoutWorkoutOfMesocycleInputSchema: z.ZodType = z.object({ id: z.string().cuid2().optional(), - userBodyweight: z.number().int(), + userBodyweight: z.number(), startedAt: z.coerce.date(), endedAt: z.coerce.date(), userId: z.string(), @@ -7321,7 +7321,7 @@ export const WorkoutUpdateToOneWithWhereWithoutWorkoutOfMesocycleInputSchema: z. export const WorkoutUpdateWithoutWorkoutOfMesocycleInputSchema: z.ZodType = z.object({ id: z.union([ z.string().cuid2(),z.lazy(() => StringFieldUpdateOperationsInputSchema) ]).optional(), - userBodyweight: z.union([ z.number().int(),z.lazy(() => IntFieldUpdateOperationsInputSchema) ]).optional(), + userBodyweight: z.union([ z.number(),z.lazy(() => FloatFieldUpdateOperationsInputSchema) ]).optional(), startedAt: z.union([ z.coerce.date(),z.lazy(() => DateTimeFieldUpdateOperationsInputSchema) ]).optional(), endedAt: z.union([ z.coerce.date(),z.lazy(() => DateTimeFieldUpdateOperationsInputSchema) ]).optional(), user: z.lazy(() => UserUpdateOneRequiredWithoutWorkoutsNestedInputSchema).optional(), @@ -7330,7 +7330,7 @@ export const WorkoutUpdateWithoutWorkoutOfMesocycleInputSchema: z.ZodType = z.object({ id: z.union([ z.string().cuid2(),z.lazy(() => StringFieldUpdateOperationsInputSchema) ]).optional(), - userBodyweight: z.union([ z.number().int(),z.lazy(() => IntFieldUpdateOperationsInputSchema) ]).optional(), + userBodyweight: z.union([ z.number(),z.lazy(() => FloatFieldUpdateOperationsInputSchema) ]).optional(), startedAt: z.union([ z.coerce.date(),z.lazy(() => DateTimeFieldUpdateOperationsInputSchema) ]).optional(), endedAt: z.union([ z.coerce.date(),z.lazy(() => DateTimeFieldUpdateOperationsInputSchema) ]).optional(), userId: z.union([ z.string(),z.lazy(() => StringFieldUpdateOperationsInputSchema) ]).optional(), @@ -7585,7 +7585,7 @@ export const WorkoutExerciseScalarWhereInputSchema: z.ZodType = z.object({ id: z.string().cuid2().optional(), - userBodyweight: z.number().int(), + userBodyweight: z.number(), startedAt: z.coerce.date(), endedAt: z.coerce.date(), user: z.lazy(() => UserCreateNestedOneWithoutWorkoutsInputSchema), @@ -7594,7 +7594,7 @@ export const WorkoutCreateWithoutWorkoutExercisesInputSchema: z.ZodType = z.object({ id: z.string().cuid2().optional(), - userBodyweight: z.number().int(), + userBodyweight: z.number(), startedAt: z.coerce.date(), endedAt: z.coerce.date(), userId: z.string(), @@ -7649,7 +7649,7 @@ export const WorkoutUpdateToOneWithWhereWithoutWorkoutExercisesInputSchema: z.Zo export const WorkoutUpdateWithoutWorkoutExercisesInputSchema: z.ZodType = z.object({ id: z.union([ z.string().cuid2(),z.lazy(() => StringFieldUpdateOperationsInputSchema) ]).optional(), - userBodyweight: z.union([ z.number().int(),z.lazy(() => IntFieldUpdateOperationsInputSchema) ]).optional(), + userBodyweight: z.union([ z.number(),z.lazy(() => FloatFieldUpdateOperationsInputSchema) ]).optional(), startedAt: z.union([ z.coerce.date(),z.lazy(() => DateTimeFieldUpdateOperationsInputSchema) ]).optional(), endedAt: z.union([ z.coerce.date(),z.lazy(() => DateTimeFieldUpdateOperationsInputSchema) ]).optional(), user: z.lazy(() => UserUpdateOneRequiredWithoutWorkoutsNestedInputSchema).optional(), @@ -7658,7 +7658,7 @@ export const WorkoutUpdateWithoutWorkoutExercisesInputSchema: z.ZodType = z.object({ id: z.union([ z.string().cuid2(),z.lazy(() => StringFieldUpdateOperationsInputSchema) ]).optional(), - userBodyweight: z.union([ z.number().int(),z.lazy(() => IntFieldUpdateOperationsInputSchema) ]).optional(), + userBodyweight: z.union([ z.number(),z.lazy(() => FloatFieldUpdateOperationsInputSchema) ]).optional(), startedAt: z.union([ z.coerce.date(),z.lazy(() => DateTimeFieldUpdateOperationsInputSchema) ]).optional(), endedAt: z.union([ z.coerce.date(),z.lazy(() => DateTimeFieldUpdateOperationsInputSchema) ]).optional(), userId: z.union([ z.string(),z.lazy(() => StringFieldUpdateOperationsInputSchema) ]).optional(), @@ -8259,7 +8259,7 @@ export const MesocycleCreateManyUserInputSchema: z.ZodType = z.object({ id: z.string().cuid2().optional(), - userBodyweight: z.number().int(), + userBodyweight: z.number(), startedAt: z.coerce.date(), endedAt: z.coerce.date() }).strict(); @@ -8393,7 +8393,7 @@ export const MesocycleUncheckedUpdateManyWithoutUserInputSchema: z.ZodType = z.object({ id: z.union([ z.string().cuid2(),z.lazy(() => StringFieldUpdateOperationsInputSchema) ]).optional(), - userBodyweight: z.union([ z.number().int(),z.lazy(() => IntFieldUpdateOperationsInputSchema) ]).optional(), + userBodyweight: z.union([ z.number(),z.lazy(() => FloatFieldUpdateOperationsInputSchema) ]).optional(), startedAt: z.union([ z.coerce.date(),z.lazy(() => DateTimeFieldUpdateOperationsInputSchema) ]).optional(), endedAt: z.union([ z.coerce.date(),z.lazy(() => DateTimeFieldUpdateOperationsInputSchema) ]).optional(), workoutOfMesocycle: z.lazy(() => WorkoutOfMesocycleUpdateOneWithoutWorkoutNestedInputSchema).optional(), @@ -8402,7 +8402,7 @@ export const WorkoutUpdateWithoutUserInputSchema: z.ZodType = z.object({ id: z.union([ z.string().cuid2(),z.lazy(() => StringFieldUpdateOperationsInputSchema) ]).optional(), - userBodyweight: z.union([ z.number().int(),z.lazy(() => IntFieldUpdateOperationsInputSchema) ]).optional(), + userBodyweight: z.union([ z.number(),z.lazy(() => FloatFieldUpdateOperationsInputSchema) ]).optional(), startedAt: z.union([ z.coerce.date(),z.lazy(() => DateTimeFieldUpdateOperationsInputSchema) ]).optional(), endedAt: z.union([ z.coerce.date(),z.lazy(() => DateTimeFieldUpdateOperationsInputSchema) ]).optional(), workoutOfMesocycle: z.lazy(() => WorkoutOfMesocycleUncheckedUpdateOneWithoutWorkoutNestedInputSchema).optional(), @@ -8411,7 +8411,7 @@ export const WorkoutUncheckedUpdateWithoutUserInputSchema: z.ZodType = z.object({ id: z.union([ z.string().cuid2(),z.lazy(() => StringFieldUpdateOperationsInputSchema) ]).optional(), - userBodyweight: z.union([ z.number().int(),z.lazy(() => IntFieldUpdateOperationsInputSchema) ]).optional(), + userBodyweight: z.union([ z.number(),z.lazy(() => FloatFieldUpdateOperationsInputSchema) ]).optional(), startedAt: z.union([ z.coerce.date(),z.lazy(() => DateTimeFieldUpdateOperationsInputSchema) ]).optional(), endedAt: z.union([ z.coerce.date(),z.lazy(() => DateTimeFieldUpdateOperationsInputSchema) ]).optional(), }).strict();