Skip to content

Commit

Permalink
Fallback to default value when migrating value from enum
Browse files Browse the repository at this point in the history
  • Loading branch information
ijreilly committed Aug 21, 2024
1 parent 612a875 commit 4c65460
Showing 1 changed file with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { QueryRunner, TableColumn } from 'typeorm';
import { v4 } from 'uuid';

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,
Expand Down Expand Up @@ -80,15 +79,30 @@ export class WorkspaceMigrationEnumService {
}),
);

await this.migrateEnumValues(
queryRunner,
schemaName,
migrationColumn,
tableName,
oldColumnName,
enumValues,
renamedEnumValues,
);
if (columnDefinition.isNullable) {
await this.migrateEnumValues(
queryRunner,
schemaName,
migrationColumn,
tableName,
oldColumnName,
enumValues,
renamedEnumValues,
);
} else {
const defaultValue = columnDefinition.defaultValue?.replaceAll("'", '');

await this.migrateEnumValues(
queryRunner,
schemaName,
migrationColumn,
tableName,
oldColumnName,
enumValues,
renamedEnumValues,
defaultValue,
);
}

// Drop old column
await queryRunner.query(`
Expand Down Expand Up @@ -146,6 +160,7 @@ export class WorkspaceMigrationEnumService {
oldColumnName: string,
enumValues: string[],
renamedEnumValues?: WorkspaceMigrationRenamedEnum[],
defaultValueFallback?: string,
) {
const columnDefinition = migrationColumn.alteredColumnDefinition;

Expand Down Expand Up @@ -176,9 +191,7 @@ export class WorkspaceMigrationEnumService {
value: val,
renamedEnumValues: renamedEnumValues,
allEnumValues: enumValues,
defaultValueFallback: columnDefinition.isNullable
? null
: unserializeDefaultValue(columnDefinition.defaultValue),
defaultValueFallback: defaultValueFallback,
});

val = isDefined(migratedValue) ? `'${migratedValue}'` : null;
Expand Down

0 comments on commit 4c65460

Please sign in to comment.