Fix enum defaultValue broken (#4251)
* Fix enum defaultValue broken * Fix * Fix
This commit is contained in:
@ -26,7 +26,7 @@ export type FieldMetadataItem = Omit<
|
||||
>;
|
||||
})
|
||||
| null;
|
||||
defaultValue?: unknown;
|
||||
defaultValue?: any;
|
||||
options?: {
|
||||
color: ThemeColor;
|
||||
id: string;
|
||||
|
||||
@ -105,7 +105,7 @@ export const SettingsObjectFieldEdit = () => {
|
||||
|
||||
const selectOptions = activeMetadataField.options?.map((option) => ({
|
||||
...option,
|
||||
isDefault: defaultValue === option.value,
|
||||
isDefault: defaultValue?.value === option.value,
|
||||
}));
|
||||
selectOptions?.sort(
|
||||
(optionA, optionB) => optionA.position - optionB.position,
|
||||
|
||||
@ -235,6 +235,10 @@ export class FieldMetadataService extends TypeOrmQueryService<FieldMetadataEntit
|
||||
|
||||
const updatedFieldMetadata = await super.updateOne(id, {
|
||||
...updatableFieldInput,
|
||||
defaultValue: updatableFieldInput.defaultValue
|
||||
? // Todo: we need to rework DefaultValue typing and format to be simpler, there is no need to have this complexity
|
||||
{ value: updatableFieldInput.defaultValue as unknown as string }
|
||||
: null,
|
||||
// If the name is updated, the targetColumnMap should be updated as well
|
||||
targetColumnMap: updatableFieldInput.name
|
||||
? generateTargetColumnMap(
|
||||
|
||||
@ -52,6 +52,7 @@ export class EnumColumnActionFactory extends ColumnActionAbstractFactory<EnumFie
|
||||
const defaultValue =
|
||||
alteredFieldMetadata.defaultValue?.value ?? options?.defaultValue;
|
||||
const serializedDefaultValue = serializeDefaultValue(defaultValue);
|
||||
|
||||
const enumOptions = alteredFieldMetadata.options
|
||||
? [
|
||||
...alteredFieldMetadata.options.map((option) => {
|
||||
|
||||
@ -3,6 +3,7 @@ import { Injectable } from '@nestjs/common';
|
||||
import { QueryRunner } from 'typeorm';
|
||||
|
||||
import { WorkspaceMigrationColumnAlter } from 'src/metadata/workspace-migration/workspace-migration.entity';
|
||||
import { serializeDefaultValue } from 'src/metadata/field-metadata/utils/serialize-default-value';
|
||||
|
||||
@Injectable()
|
||||
export class WorkspaceMigrationEnumService {
|
||||
@ -25,7 +26,7 @@ export class WorkspaceMigrationEnumService {
|
||||
}) ?? [];
|
||||
|
||||
if (!columnDefinition.isNullable && !columnDefinition.defaultValue) {
|
||||
columnDefinition.defaultValue = columnDefinition.enum?.[0];
|
||||
columnDefinition.defaultValue = serializeDefaultValue(enumValues[0]);
|
||||
}
|
||||
|
||||
// Create new enum type with new values
|
||||
@ -66,6 +67,7 @@ export class WorkspaceMigrationEnumService {
|
||||
tableName,
|
||||
columnDefinition.columnName,
|
||||
newEnumTypeName,
|
||||
columnDefinition.defaultValue,
|
||||
);
|
||||
|
||||
// Drop old enum type
|
||||
@ -138,7 +140,7 @@ export class WorkspaceMigrationEnumService {
|
||||
.map((e) => `'${e}'`)
|
||||
.join(', ')}]`;
|
||||
} else {
|
||||
defaultValue = this.getStringifyValue(columnDefinition.defaultValue);
|
||||
defaultValue = columnDefinition.defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
@ -157,9 +159,12 @@ export class WorkspaceMigrationEnumService {
|
||||
tableName: string,
|
||||
columnName: string,
|
||||
newEnumTypeName: string,
|
||||
newDefaultValue: string,
|
||||
) {
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "${schemaName}"."${tableName}" ALTER COLUMN "${columnName}" DROP DEFAULT, ALTER COLUMN "${columnName}" TYPE "${schemaName}"."${newEnumTypeName}" USING ("${columnName}"::text::"${schemaName}"."${newEnumTypeName}")`,
|
||||
`ALTER TABLE "${schemaName}"."${tableName}" ALTER COLUMN "${columnName}" DROP DEFAULT,
|
||||
ALTER COLUMN "${columnName}" TYPE "${schemaName}"."${newEnumTypeName}" USING ("${columnName}"::text::"${schemaName}"."${newEnumTypeName}"),
|
||||
ALTER COLUMN "${columnName}" SET DEFAULT ${newDefaultValue}`,
|
||||
);
|
||||
}
|
||||
|
||||
@ -184,8 +189,4 @@ export class WorkspaceMigrationEnumService {
|
||||
RENAME TO "${oldEnumTypeName}"
|
||||
`);
|
||||
}
|
||||
|
||||
private getStringifyValue(value: any) {
|
||||
return `'${value}'`;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user