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:
@ -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(`
|
||||
|
||||
Reference in New Issue
Block a user