Code maintainability fixes
This commit is contained in:
42
server/src/scripts/migratePlans.ts
Normal file
42
server/src/scripts/migratePlans.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
async function migrate() {
|
||||
console.log('Starting migration...');
|
||||
const plans = await prisma.workoutPlan.findMany();
|
||||
console.log(`Found ${plans.length} plans.`);
|
||||
|
||||
for (const plan of plans) {
|
||||
if (plan.exercises) {
|
||||
try {
|
||||
const steps = JSON.parse(plan.exercises);
|
||||
console.log(`Migrating plan ${plan.name} (${plan.id}) with ${steps.length} steps.`);
|
||||
|
||||
let order = 0;
|
||||
for (const step of steps) {
|
||||
await prisma.planExercise.create({
|
||||
data: {
|
||||
planId: plan.id,
|
||||
exerciseId: step.exerciseId,
|
||||
order: order++,
|
||||
isWeighted: step.isWeighted || false
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(`Error parsing JSON for plan ${plan.id}:`, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
console.log('Migration complete.');
|
||||
}
|
||||
|
||||
migrate()
|
||||
.catch((e) => {
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
})
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect();
|
||||
});
|
||||
Reference in New Issue
Block a user