fix: added migration script to fix range in rating question (#2298)

Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
This commit is contained in:
Dhruwang Jariwala
2024-03-21 01:50:10 +05:30
committed by GitHub
parent 7b2cf9f3d8
commit f22e3fd549
2 changed files with 59 additions and 1 deletions

View File

@@ -0,0 +1,57 @@
// migration script to convert range field in rating question from string to number
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
async function main() {
await prisma.$transaction(
async (tx) => {
const surveys = await tx.survey.findMany({
select: {
id: true,
questions: true,
},
});
if (surveys.length === 0) {
// stop the migration if there are no surveys
return;
}
for (const survey of surveys) {
let updateNeeded = false;
const updatedSurvey = structuredClone(survey) as any;
if (updatedSurvey.questions.length > 0) {
for (const question of updatedSurvey.questions) {
if (question.type === "rating" && typeof question.range === "string") {
const parsedRange = parseInt(question.range);
if (!isNaN(parsedRange)) {
updateNeeded = true;
question.range = parsedRange;
} else {
throw new Error(`Invalid range value for question Id ${question.id}: ${question.range}`);
}
}
}
}
if (updateNeeded) {
// Save the translated survey
await tx.survey.update({
where: { id: survey.id },
data: { ...updatedSurvey },
});
}
}
},
{
timeout: 50000,
}
);
}
main()
.catch(async (e) => {
console.error(e);
process.exit(1);
})
.finally(async () => await prisma.$disconnect());

View File

@@ -25,7 +25,8 @@
"predev": "pnpm generate",
"data-migration:v1.6": "ts-node ./migrations/20240207041922_advanced_targeting/data-migration.ts",
"data-migration:mls": "ts-node ./migrations/20240318050527_add_languages_and_survey_languages/data-migration.ts",
"data-migration:mls-fix": "ts-node ./migrations/20240318050527_add_languages_and_survey_languages/data-migration-fix.ts"
"data-migration:mls-fix": "ts-node ./migrations/20240318050527_add_languages_and_survey_languages/data-migration-fix.ts",
"data-migration:mls-range-fix": "ts-node ./migrations/20240318050527_add_languages_and_survey_languages/data-migration-range-fix.ts"
},
"dependencies": {
"@prisma/client": "^5.11.0",