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;
|
| null;
|
||||||
defaultValue?: unknown;
|
defaultValue?: any;
|
||||||
options?: {
|
options?: {
|
||||||
color: ThemeColor;
|
color: ThemeColor;
|
||||||
id: string;
|
id: string;
|
||||||
|
|||||||
@ -105,7 +105,7 @@ export const SettingsObjectFieldEdit = () => {
|
|||||||
|
|
||||||
const selectOptions = activeMetadataField.options?.map((option) => ({
|
const selectOptions = activeMetadataField.options?.map((option) => ({
|
||||||
...option,
|
...option,
|
||||||
isDefault: defaultValue === option.value,
|
isDefault: defaultValue?.value === option.value,
|
||||||
}));
|
}));
|
||||||
selectOptions?.sort(
|
selectOptions?.sort(
|
||||||
(optionA, optionB) => optionA.position - optionB.position,
|
(optionA, optionB) => optionA.position - optionB.position,
|
||||||
|
|||||||
@ -235,6 +235,10 @@ export class FieldMetadataService extends TypeOrmQueryService<FieldMetadataEntit
|
|||||||
|
|
||||||
const updatedFieldMetadata = await super.updateOne(id, {
|
const updatedFieldMetadata = await super.updateOne(id, {
|
||||||
...updatableFieldInput,
|
...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
|
// If the name is updated, the targetColumnMap should be updated as well
|
||||||
targetColumnMap: updatableFieldInput.name
|
targetColumnMap: updatableFieldInput.name
|
||||||
? generateTargetColumnMap(
|
? generateTargetColumnMap(
|
||||||
|
|||||||
@ -52,6 +52,7 @@ export class EnumColumnActionFactory extends ColumnActionAbstractFactory<EnumFie
|
|||||||
const defaultValue =
|
const defaultValue =
|
||||||
alteredFieldMetadata.defaultValue?.value ?? options?.defaultValue;
|
alteredFieldMetadata.defaultValue?.value ?? options?.defaultValue;
|
||||||
const serializedDefaultValue = serializeDefaultValue(defaultValue);
|
const serializedDefaultValue = serializeDefaultValue(defaultValue);
|
||||||
|
|
||||||
const enumOptions = alteredFieldMetadata.options
|
const enumOptions = alteredFieldMetadata.options
|
||||||
? [
|
? [
|
||||||
...alteredFieldMetadata.options.map((option) => {
|
...alteredFieldMetadata.options.map((option) => {
|
||||||
|
|||||||
@ -3,6 +3,7 @@ import { Injectable } from '@nestjs/common';
|
|||||||
import { QueryRunner } from 'typeorm';
|
import { QueryRunner } from 'typeorm';
|
||||||
|
|
||||||
import { WorkspaceMigrationColumnAlter } from 'src/metadata/workspace-migration/workspace-migration.entity';
|
import { WorkspaceMigrationColumnAlter } from 'src/metadata/workspace-migration/workspace-migration.entity';
|
||||||
|
import { serializeDefaultValue } from 'src/metadata/field-metadata/utils/serialize-default-value';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class WorkspaceMigrationEnumService {
|
export class WorkspaceMigrationEnumService {
|
||||||
@ -25,7 +26,7 @@ export class WorkspaceMigrationEnumService {
|
|||||||
}) ?? [];
|
}) ?? [];
|
||||||
|
|
||||||
if (!columnDefinition.isNullable && !columnDefinition.defaultValue) {
|
if (!columnDefinition.isNullable && !columnDefinition.defaultValue) {
|
||||||
columnDefinition.defaultValue = columnDefinition.enum?.[0];
|
columnDefinition.defaultValue = serializeDefaultValue(enumValues[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create new enum type with new values
|
// Create new enum type with new values
|
||||||
@ -66,6 +67,7 @@ export class WorkspaceMigrationEnumService {
|
|||||||
tableName,
|
tableName,
|
||||||
columnDefinition.columnName,
|
columnDefinition.columnName,
|
||||||
newEnumTypeName,
|
newEnumTypeName,
|
||||||
|
columnDefinition.defaultValue,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Drop old enum type
|
// Drop old enum type
|
||||||
@ -138,7 +140,7 @@ export class WorkspaceMigrationEnumService {
|
|||||||
.map((e) => `'${e}'`)
|
.map((e) => `'${e}'`)
|
||||||
.join(', ')}]`;
|
.join(', ')}]`;
|
||||||
} else {
|
} else {
|
||||||
defaultValue = this.getStringifyValue(columnDefinition.defaultValue);
|
defaultValue = columnDefinition.defaultValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,9 +159,12 @@ export class WorkspaceMigrationEnumService {
|
|||||||
tableName: string,
|
tableName: string,
|
||||||
columnName: string,
|
columnName: string,
|
||||||
newEnumTypeName: string,
|
newEnumTypeName: string,
|
||||||
|
newDefaultValue: string,
|
||||||
) {
|
) {
|
||||||
await queryRunner.query(
|
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}"
|
RENAME TO "${oldEnumTypeName}"
|
||||||
`);
|
`);
|
||||||
}
|
}
|
||||||
|
|
||||||
private getStringifyValue(value: any) {
|
|
||||||
return `'${value}'`;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user