Fallback to default value when migrating value from enum (#6517)
When migrating the option values of a select type, if the field is non nullable (for now, only available for opportunity's "stage" standard field), we fallback to the (potentially updated) default value instead of nullifying the value to avoid getting a database error. --------- Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
@ -1,14 +1,15 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
import { isDefined } from 'class-validator';
|
||||
import { QueryRunner, TableColumn } from 'typeorm';
|
||||
import { v4 } from 'uuid';
|
||||
import { isDefined } from 'class-validator';
|
||||
|
||||
import { serializeDefaultValue } from 'src/engine/metadata-modules/field-metadata/utils/serialize-default-value';
|
||||
import { unserializeDefaultValue } from 'src/engine/metadata-modules/field-metadata/utils/unserialize-default-value';
|
||||
import {
|
||||
WorkspaceMigrationColumnAlter,
|
||||
WorkspaceMigrationRenamedEnum,
|
||||
} from 'src/engine/metadata-modules/workspace-migration/workspace-migration.entity';
|
||||
import { serializeDefaultValue } from 'src/engine/metadata-modules/field-metadata/utils/serialize-default-value';
|
||||
|
||||
@Injectable()
|
||||
export class WorkspaceMigrationEnumService {
|
||||
@ -111,11 +112,17 @@ export class WorkspaceMigrationEnumService {
|
||||
`);
|
||||
}
|
||||
|
||||
private migrateEnumValue(
|
||||
value: string,
|
||||
renamedEnumValues?: WorkspaceMigrationRenamedEnum[],
|
||||
allEnumValues?: string[],
|
||||
) {
|
||||
private migrateEnumValue({
|
||||
value,
|
||||
renamedEnumValues,
|
||||
allEnumValues,
|
||||
defaultValueFallback,
|
||||
}: {
|
||||
value: string;
|
||||
renamedEnumValues?: WorkspaceMigrationRenamedEnum[];
|
||||
allEnumValues?: string[];
|
||||
defaultValueFallback?: string;
|
||||
}) {
|
||||
if (renamedEnumValues?.find((enumVal) => enumVal?.from === value)?.to) {
|
||||
return renamedEnumValues?.find((enumVal) => enumVal?.from === value)?.to;
|
||||
}
|
||||
@ -124,6 +131,10 @@ export class WorkspaceMigrationEnumService {
|
||||
return value;
|
||||
}
|
||||
|
||||
if (isDefined(defaultValueFallback)) {
|
||||
return defaultValueFallback;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -152,16 +163,23 @@ export class WorkspaceMigrationEnumService {
|
||||
.split(',')
|
||||
.map((v: string) => v.trim())
|
||||
.map((v: string) =>
|
||||
this.migrateEnumValue(v, renamedEnumValues, enumValues),
|
||||
this.migrateEnumValue({
|
||||
value: v,
|
||||
renamedEnumValues: renamedEnumValues,
|
||||
allEnumValues: enumValues,
|
||||
}),
|
||||
)
|
||||
.filter((v: string | null) => isDefined(v)),
|
||||
);
|
||||
} else if (typeof val === 'string') {
|
||||
const migratedValue = this.migrateEnumValue(
|
||||
val,
|
||||
renamedEnumValues,
|
||||
enumValues,
|
||||
);
|
||||
const migratedValue = this.migrateEnumValue({
|
||||
value: val,
|
||||
renamedEnumValues: renamedEnumValues,
|
||||
allEnumValues: enumValues,
|
||||
defaultValueFallback: columnDefinition.isNullable
|
||||
? null
|
||||
: unserializeDefaultValue(columnDefinition.defaultValue),
|
||||
});
|
||||
|
||||
val = isDefined(migratedValue) ? `'${migratedValue}'` : null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user