Fix select field options update (#5736)

Update of select fields options was failing if we deleted an option that
was used for at least one row: former code would not update the value to
null but leave it to the no-longer-allowed value.
This commit is contained in:
Marie
2024-06-05 11:06:22 +02:00
committed by GitHub
parent 1bbfc0b715
commit 6d869297b5
2 changed files with 40 additions and 6 deletions

View File

@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common';
import { QueryRunner, TableColumn } from 'typeorm';
import { v4 } from 'uuid';
import { isDefined } from 'class-validator';
import {
WorkspaceMigrationColumnAlter,
@ -117,10 +118,17 @@ export class WorkspaceMigrationEnumService {
private migrateEnumValue(
value: string,
renamedEnumValues?: WorkspaceMigrationRenamedEnum[],
allEnumValues?: string[],
) {
return (
renamedEnumValues?.find((enumVal) => enumVal?.from === value)?.to || value
);
if (renamedEnumValues?.find((enumVal) => enumVal?.from === value)?.to) {
return renamedEnumValues?.find((enumVal) => enumVal?.from === value)?.to;
}
if (allEnumValues?.includes(value)) {
return value;
}
return null;
}
private async migrateEnumValues(
@ -147,11 +155,19 @@ export class WorkspaceMigrationEnumService {
.slice(1, -1)
.split(',')
.map((v: string) => v.trim())
.map((v: string) => this.migrateEnumValue(v, renamedEnumValues))
.filter((v: string) => enumValues.includes(v)),
.map((v: string) =>
this.migrateEnumValue(v, renamedEnumValues, enumValues),
)
.filter((v: string | null) => isDefined(v)),
);
} else if (typeof val === 'string') {
val = `'${this.migrateEnumValue(val, renamedEnumValues)}'`;
const migratedValue = this.migrateEnumValue(
val,
renamedEnumValues,
enumValues,
);
val = isDefined(migratedValue) ? `'${migratedValue}'` : null;
}
await queryRunner.query(`