fix: Increase multilanguage data migration timeout (#2278)

This commit is contained in:
Matti Nannt
2024-03-18 20:32:01 +01:00
committed by GitHub
parent 8b5328aa74
commit 63708ec92b

View File

@@ -6,82 +6,87 @@ import { translateSurvey } from "./lib/i18n";
const prisma = new PrismaClient();
async function main() {
await prisma.$transaction(async (tx) => {
const surveys = await tx.survey.findMany({
select: {
id: true,
questions: true,
thankYouCard: true,
welcomeCard: true,
},
});
if (!surveys) {
// stop the migration if there are no surveys
return;
}
for (const survey of surveys) {
if (survey.questions.length > 0 && typeof survey.questions[0].headline === "string") {
const translatedSurvey = translateSurvey(survey, []);
// Save the translated survey
await tx.survey.update({
where: { id: survey.id },
data: { ...translatedSurvey },
});
}
}
});
await prisma.$transaction(async (tx) => {
const environments = await tx.environment.findMany({
select: {
id: true,
attributeClasses: true,
},
});
if (!environments) {
// stop the migration if there are no environments
return;
}
for (const environment of environments) {
const languageAttributeClass = environment.attributeClasses.find((attributeClass) => {
return attributeClass.name === "language";
await prisma.$transaction(
async (tx) => {
// Translate Surveys
const surveys = await tx.survey.findMany({
select: {
id: true,
questions: true,
thankYouCard: true,
welcomeCard: true,
},
});
if (languageAttributeClass) {
// Update existing attributeClass if needed
if (
languageAttributeClass.type === AttributeType.automatic &&
languageAttributeClass.description === "The language used by the person"
) {
continue;
}
await tx.attributeClass.update({
where: { id: languageAttributeClass.id },
data: {
type: AttributeType.automatic,
description: "The language used by the person",
},
});
} else {
// Create new attributeClass
await tx.attributeClass.create({
data: {
name: "language",
type: AttributeType.automatic,
description: "The language used by the person",
environment: {
connect: { id: environment.id },
},
},
});
if (!surveys) {
// stop the migration if there are no surveys
return;
}
for (const survey of surveys) {
if (survey.questions.length > 0 && typeof survey.questions[0].headline === "string") {
const translatedSurvey = translateSurvey(survey, []);
// Save the translated survey
await tx.survey.update({
where: { id: survey.id },
data: { ...translatedSurvey },
});
}
}
// Add language attributeClass
const environments = await tx.environment.findMany({
select: {
id: true,
attributeClasses: true,
},
});
if (!environments) {
// stop the migration if there are no environments
return;
}
for (const environment of environments) {
const languageAttributeClass = environment.attributeClasses.find((attributeClass) => {
return attributeClass.name === "language";
});
if (languageAttributeClass) {
// Update existing attributeClass if needed
if (
languageAttributeClass.type === AttributeType.automatic &&
languageAttributeClass.description === "The language used by the person"
) {
continue;
}
await tx.attributeClass.update({
where: { id: languageAttributeClass.id },
data: {
type: AttributeType.automatic,
description: "The language used by the person",
},
});
} else {
// Create new attributeClass
await tx.attributeClass.create({
data: {
name: "language",
type: AttributeType.automatic,
description: "The language used by the person",
environment: {
connect: { id: environment.id },
},
},
});
}
}
},
{
timeout: 50000,
}
});
);
}
main()