Skip to content

Commit 453525c

Browse files
authored
fix: workspace health showing error for multi select (#5547)
Fix `workspace:health` command not working properly with `MULTI_SELECT` field metadata type.
1 parent 7b1bea3 commit 453525c

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

packages/twenty-server/src/engine/workspace-manager/workspace-health/interfaces/workspace-table-definition.interface.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface WorkspaceTableStructure {
88
isPrimaryKey: boolean;
99
isForeignKey: boolean;
1010
isUnique: boolean;
11+
isArray: boolean;
1112
onUpdateAction: string;
1213
onDeleteAction: string;
1314
}

packages/twenty-server/src/engine/workspace-manager/workspace-health/services/database-structure.service.ts

+34-8
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { isRelationFieldMetadataType } from 'src/engine/utils/is-relation-field-
2424
import { isFunctionDefaultValue } from 'src/engine/metadata-modules/field-metadata/utils/is-function-default-value.util';
2525
import { FieldMetadataDefaultValueFunctionNames } from 'src/engine/metadata-modules/field-metadata/dtos/default-value.input';
2626
import { compositeTypeDefintions } from 'src/engine/metadata-modules/field-metadata/composite-types';
27+
import { computeObjectTargetTable } from 'src/engine/utils/compute-object-target-table.util';
2728

2829
@Injectable()
2930
export class DatabaseStructureService {
@@ -74,9 +75,13 @@ export class DatabaseStructureService {
7475
c.table_schema AS "tableSchema",
7576
c.table_name AS "tableName",
7677
c.column_name AS "columnName",
77-
CASE
78-
WHEN (c.data_type = 'USER-DEFINED') THEN c.udt_name
79-
ELSE data_type
78+
CASE
79+
WHEN c.data_type = 'ARRAY' THEN
80+
(SELECT typname FROM pg_type WHERE oid = t.typelem)
81+
WHEN c.data_type = 'USER-DEFINED' THEN
82+
c.udt_name
83+
ELSE
84+
c.data_type
8085
END AS "dataType",
8186
c.is_nullable AS "isNullable",
8287
c.column_default AS "columnDefault",
@@ -92,10 +97,15 @@ export class DatabaseStructureService {
9297
WHEN uc.column_name IS NOT NULL THEN 'TRUE'
9398
ELSE 'FALSE'
9499
END AS "isUnique",
100+
CASE
101+
WHEN c.data_type = 'ARRAY' THEN 'TRUE'
102+
ELSE 'FALSE'
103+
END AS "isArray",
95104
rc.update_rule AS "onUpdateAction",
96105
rc.delete_rule AS "onDeleteAction"
97106
FROM
98107
information_schema.columns AS c
108+
LEFT JOIN pg_type t ON t.typname = c.udt_name
99109
LEFT JOIN
100110
information_schema.constraint_column_usage AS ccu
101111
ON c.column_name = ccu.column_name
@@ -132,10 +142,12 @@ export class DatabaseStructureService {
132142

133143
return results.map((item) => ({
134144
...item,
145+
dataType: item.isArray === 'TRUE' ? `${item.dataType}[]` : item.dataType,
135146
isNullable: item.isNullable === 'YES',
136147
isPrimaryKey: item.isPrimaryKey === 'TRUE',
137148
isForeignKey: item.isForeignKey === 'TRUE',
138149
isUnique: item.isUnique === 'TRUE',
150+
isArray: item.isArray === 'TRUE',
139151
}));
140152
}
141153

@@ -160,14 +172,18 @@ export class DatabaseStructureService {
160172
getPostgresDataTypes(fieldMetadata: FieldMetadataEntity): string[] {
161173
const mainDataSource = this.typeORMService.getMainDataSource();
162174

163-
const normalizer = (type: FieldMetadataType, columnName: string) => {
175+
const normalizer = (
176+
type: FieldMetadataType,
177+
isArray: boolean | undefined,
178+
columnName: string,
179+
) => {
164180
const typeORMType = fieldMetadataTypeToColumnType(type);
165181

166182
// Compute enum name to compare data type properly
167183
if (typeORMType === 'enum') {
168-
const objectName = fieldMetadata.object?.nameSingular;
184+
const objectName = computeObjectTargetTable(fieldMetadata.object);
169185

170-
return `${objectName}_${columnName}_enum`;
186+
return `${objectName}_${columnName}_enum${isArray ? '[]' : ''}`;
171187
}
172188

173189
return mainDataSource.driver.normalizeType({
@@ -185,11 +201,21 @@ export class DatabaseStructureService {
185201
}
186202

187203
return compositeType.properties.map((compositeProperty) =>
188-
normalizer(compositeProperty.type, compositeProperty.name),
204+
normalizer(
205+
compositeProperty.type,
206+
compositeProperty.isArray,
207+
compositeProperty.name,
208+
),
189209
);
190210
}
191211

192-
return [normalizer(fieldMetadata.type, fieldMetadata.name)];
212+
return [
213+
normalizer(
214+
fieldMetadata.type,
215+
fieldMetadata.type === FieldMetadataType.MULTI_SELECT,
216+
fieldMetadata.name,
217+
),
218+
];
193219
}
194220

195221
getFieldMetadataTypeFromPostgresDataType(

0 commit comments

Comments
 (0)