Fix 'name' column wrongly added in standard objects (#7428)

## Context
Name shouldn't be added to all tables, especially standard objects
because they already have their own labelIdentifierFieldMetadata
specified in the workspace-entity schema. This PR removes this column
from the "base" list of columns to add when creating a new object/table
and moves it to the object-metadata service that is, as of today, only
used for custom objects. Also had to modify the migration-runner to
handle column creation in a table creation migration (this was available
in the migration definition already but was not doing anything)

This also fixes an issue in standard objects that already have a "name"
field defined with a different field type, this is even more important
when the said field is a composite field. For example people already has
a FULL_NAME name field which clashes with the default TEXT name field
meaning it was only creating 1 field metadata for 'name' but 3 columns
were created: `name, nameFirstName, nameLastName`. This inconsistency
with metadata (which is our source of truth everywhere) brought some
issues (lately, converting back typeorm response to gql (including
composition) was broken).
This commit is contained in:
Weiko
2024-10-04 18:31:19 +02:00
committed by GitHub
parent ebe28def02
commit 2f223f3294
10 changed files with 74 additions and 72 deletions

View File

@ -1,55 +0,0 @@
import { TableColumnOptions } from 'typeorm';
import { FieldActorSource } from 'src/engine/metadata-modules/field-metadata/composite-types/actor.composite-type';
export const customTableDefaultColumns = (
tableName: string,
): TableColumnOptions[] => [
{
name: 'id',
type: 'uuid',
isPrimary: true,
default: 'public.uuid_generate_v4()',
},
{
name: 'createdAt',
type: 'timestamptz',
default: 'now()',
},
{
name: 'updatedAt',
type: 'timestamptz',
default: 'now()',
},
{
name: 'deletedAt',
type: 'timestamptz',
isNullable: true,
},
{
name: 'position',
type: 'float',
isNullable: true,
},
{
name: 'name',
type: 'text',
isNullable: false,
default: "'Untitled'",
},
{
name: 'createdBySource',
type: 'enum',
enumName: `${tableName}_createdBySource_enum`,
enum: Object.values(FieldActorSource),
isNullable: false,
default: `'${FieldActorSource.MANUAL}'`,
},
{ name: 'createdByWorkspaceMemberId', type: 'uuid', isNullable: true },
{
name: 'createdByName',
type: 'text',
isNullable: false,
default: "''",
},
];

View File

@ -0,0 +1,10 @@
import { TableColumnOptions } from 'typeorm';
export const tableDefaultColumns = (): TableColumnOptions[] => [
{
name: 'id',
type: 'uuid',
isPrimary: true,
default: 'public.uuid_generate_v4()',
},
];

View File

@ -26,10 +26,10 @@ import { WorkspaceMigrationService } from 'src/engine/metadata-modules/workspace
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
import { WorkspaceMigrationEnumService } from 'src/engine/workspace-manager/workspace-migration-runner/services/workspace-migration-enum.service';
import { convertOnDeleteActionToOnDelete } from 'src/engine/workspace-manager/workspace-migration-runner/utils/convert-on-delete-action-to-on-delete.util';
import { tableDefaultColumns } from 'src/engine/workspace-manager/workspace-migration-runner/utils/table-default-column.util';
import { isDefined } from 'src/utils/is-defined';
import { WorkspaceMigrationTypeService } from './services/workspace-migration-type.service';
import { customTableDefaultColumns } from './utils/custom-table-default-column.util';
@Injectable()
export class WorkspaceMigrationRunnerService {
@ -121,7 +121,12 @@ export class WorkspaceMigrationRunnerService {
) {
switch (tableMigration.action) {
case WorkspaceMigrationTableActionType.CREATE:
await this.createTable(queryRunner, schemaName, tableMigration.name);
await this.createTable(
queryRunner,
schemaName,
tableMigration.name,
tableMigration.columns,
);
break;
case WorkspaceMigrationTableActionType.ALTER: {
if (tableMigration.newName) {
@ -244,16 +249,26 @@ export class WorkspaceMigrationRunnerService {
queryRunner: QueryRunner,
schemaName: string,
tableName: string,
columns?: WorkspaceMigrationColumnAction[],
) {
await queryRunner.createTable(
new Table({
name: tableName,
schema: schemaName,
columns: customTableDefaultColumns(tableName),
columns: tableDefaultColumns(),
}),
true,
);
if (columns && columns.length > 0) {
await this.handleColumnChanges(
queryRunner,
schemaName,
tableName,
columns,
);
}
// Enable totalCount for the table
await queryRunner.query(`
COMMENT ON TABLE "${schemaName}"."${tableName}" IS '@graphql({"totalCount": {"enabled": true}})';