mirror of
https://github.com/formbricks/formbricks.git
synced 2025-12-30 10:19:51 -06:00
fix: added migration script to fix range in rating question (#2298)
Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
This commit is contained in:
committed by
GitHub
parent
7b2cf9f3d8
commit
f22e3fd549
@@ -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());
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user