Remove relations for remotes (#5455)
For remotes, we will only create the foreign key, without the relation metadata. Expected behavior will be: - possible to create an activity. But the remote object will not be displayed in the relations of the activity - the remote objects should not be available in the search for relations Also switched the number settings to an enum, since we now have to handle `BigInt` case. --------- Co-authored-by: Thomas Trompette <thomast@twenty.com>
This commit is contained in:
@ -13,7 +13,7 @@ export const createWorkspaceMigrationsForCustomObjectRelations = (
|
||||
createdObjectMetadata: ObjectMetadataEntity,
|
||||
activityTargetObjectMetadata: ObjectMetadataEntity,
|
||||
attachmentObjectMetadata: ObjectMetadataEntity,
|
||||
eventObjectMetadata: ObjectMetadataEntity,
|
||||
timelineActivityObjectMetadata: ObjectMetadataEntity,
|
||||
favoriteObjectMetadata: ObjectMetadataEntity,
|
||||
): WorkspaceMigrationTableAction[] => [
|
||||
{
|
||||
@ -80,9 +80,9 @@ export const createWorkspaceMigrationsForCustomObjectRelations = (
|
||||
},
|
||||
],
|
||||
},
|
||||
// Add event relation
|
||||
// Add timeline activity relation
|
||||
{
|
||||
name: computeObjectTargetTable(eventObjectMetadata),
|
||||
name: computeObjectTargetTable(timelineActivityObjectMetadata),
|
||||
action: WorkspaceMigrationTableActionType.ALTER,
|
||||
columns: [
|
||||
{
|
||||
@ -96,7 +96,7 @@ export const createWorkspaceMigrationsForCustomObjectRelations = (
|
||||
],
|
||||
},
|
||||
{
|
||||
name: computeObjectTargetTable(eventObjectMetadata),
|
||||
name: computeObjectTargetTable(timelineActivityObjectMetadata),
|
||||
action: WorkspaceMigrationTableActionType.ALTER,
|
||||
columns: [
|
||||
{
|
||||
|
||||
@ -1,5 +1,3 @@
|
||||
import { DataSource } from 'typeorm';
|
||||
|
||||
import { computeColumnName } from 'src/engine/metadata-modules/field-metadata/utils/compute-column-name.util';
|
||||
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
|
||||
import {
|
||||
@ -10,55 +8,14 @@ import {
|
||||
} from 'src/engine/metadata-modules/workspace-migration/workspace-migration.entity';
|
||||
import { computeObjectTargetTable } from 'src/engine/utils/compute-object-target-table.util';
|
||||
|
||||
const buildCommentForRemoteObjectForeignKey = async (
|
||||
localObjectMetadataName: string,
|
||||
remoteObjectMetadataName: string,
|
||||
schema: string,
|
||||
workspaceDataSource: DataSource | undefined,
|
||||
): Promise<string> => {
|
||||
const existingComment = await workspaceDataSource?.query(
|
||||
`SELECT col_description('${schema}."${localObjectMetadataName}"'::regclass, 0)`,
|
||||
);
|
||||
|
||||
if (!existingComment[0]?.col_description) {
|
||||
return `@graphql({"totalCount":{"enabled": true},"foreign_keys":[{"local_name":"${localObjectMetadataName}Collection","local_columns":["${remoteObjectMetadataName}Id"],"foreign_name":"${remoteObjectMetadataName}","foreign_schema":"${schema}","foreign_table":"${remoteObjectMetadataName}","foreign_columns":["id"]}]})`;
|
||||
}
|
||||
|
||||
const commentWithoutGraphQL = existingComment[0].col_description
|
||||
.replace('@graphql(', '')
|
||||
.replace(')', '');
|
||||
const parsedComment = JSON.parse(commentWithoutGraphQL);
|
||||
|
||||
const foreignKey = {
|
||||
local_name: `${localObjectMetadataName}Collection`,
|
||||
local_columns: [`${remoteObjectMetadataName}Id`],
|
||||
foreign_name: `${remoteObjectMetadataName}`,
|
||||
foreign_schema: schema,
|
||||
foreign_table: remoteObjectMetadataName,
|
||||
foreign_columns: ['id'],
|
||||
};
|
||||
|
||||
if (parsedComment.foreign_keys) {
|
||||
parsedComment.foreign_keys.push(foreignKey);
|
||||
} else {
|
||||
parsedComment.foreign_keys = [foreignKey];
|
||||
}
|
||||
|
||||
return `@graphql(${JSON.stringify(parsedComment)})`;
|
||||
};
|
||||
|
||||
export const createWorkspaceMigrationsForRemoteObjectRelations = async (
|
||||
createdObjectMetadata: ObjectMetadataEntity,
|
||||
activityTargetObjectMetadata: ObjectMetadataEntity,
|
||||
attachmentObjectMetadata: ObjectMetadataEntity,
|
||||
eventObjectMetadata: ObjectMetadataEntity,
|
||||
timelineActivityObjectMetadata: ObjectMetadataEntity,
|
||||
favoriteObjectMetadata: ObjectMetadataEntity,
|
||||
schema: string,
|
||||
primaryKeyColumnType: string,
|
||||
workspaceDataSource: DataSource | undefined,
|
||||
): Promise<WorkspaceMigrationTableAction[]> => {
|
||||
const createdObjectName = createdObjectMetadata.nameSingular;
|
||||
|
||||
return [
|
||||
{
|
||||
name: computeObjectTargetTable(activityTargetObjectMetadata),
|
||||
@ -74,22 +31,6 @@ export const createWorkspaceMigrationsForRemoteObjectRelations = async (
|
||||
} satisfies WorkspaceMigrationColumnCreate,
|
||||
],
|
||||
},
|
||||
{
|
||||
name: computeObjectTargetTable(activityTargetObjectMetadata),
|
||||
action: WorkspaceMigrationTableActionType.ALTER,
|
||||
columns: [
|
||||
{
|
||||
action: WorkspaceMigrationColumnActionType.CREATE_COMMENT,
|
||||
comment: await buildCommentForRemoteObjectForeignKey(
|
||||
activityTargetObjectMetadata.nameSingular,
|
||||
createdObjectName,
|
||||
schema,
|
||||
workspaceDataSource,
|
||||
),
|
||||
},
|
||||
],
|
||||
},
|
||||
// Add attachment relation
|
||||
{
|
||||
name: computeObjectTargetTable(attachmentObjectMetadata),
|
||||
action: WorkspaceMigrationTableActionType.ALTER,
|
||||
@ -105,53 +46,7 @@ export const createWorkspaceMigrationsForRemoteObjectRelations = async (
|
||||
],
|
||||
},
|
||||
{
|
||||
name: computeObjectTargetTable(attachmentObjectMetadata),
|
||||
action: WorkspaceMigrationTableActionType.ALTER,
|
||||
columns: [
|
||||
{
|
||||
action: WorkspaceMigrationColumnActionType.CREATE_COMMENT,
|
||||
comment: await buildCommentForRemoteObjectForeignKey(
|
||||
attachmentObjectMetadata.nameSingular,
|
||||
createdObjectName,
|
||||
schema,
|
||||
workspaceDataSource,
|
||||
),
|
||||
},
|
||||
],
|
||||
},
|
||||
// Add event relation
|
||||
{
|
||||
name: computeObjectTargetTable(eventObjectMetadata),
|
||||
action: WorkspaceMigrationTableActionType.ALTER,
|
||||
columns: [
|
||||
{
|
||||
action: WorkspaceMigrationColumnActionType.CREATE,
|
||||
columnName: computeColumnName(createdObjectMetadata.nameSingular, {
|
||||
isForeignKey: true,
|
||||
}),
|
||||
columnType: primaryKeyColumnType,
|
||||
isNullable: true,
|
||||
} satisfies WorkspaceMigrationColumnCreate,
|
||||
],
|
||||
},
|
||||
{
|
||||
name: computeObjectTargetTable(eventObjectMetadata),
|
||||
action: WorkspaceMigrationTableActionType.ALTER,
|
||||
columns: [
|
||||
{
|
||||
action: WorkspaceMigrationColumnActionType.CREATE_COMMENT,
|
||||
comment: await buildCommentForRemoteObjectForeignKey(
|
||||
eventObjectMetadata.nameSingular,
|
||||
createdObjectName,
|
||||
schema,
|
||||
workspaceDataSource,
|
||||
),
|
||||
},
|
||||
],
|
||||
},
|
||||
// Add favorite relation
|
||||
{
|
||||
name: computeObjectTargetTable(favoriteObjectMetadata),
|
||||
name: computeObjectTargetTable(timelineActivityObjectMetadata),
|
||||
action: WorkspaceMigrationTableActionType.ALTER,
|
||||
columns: [
|
||||
{
|
||||
@ -169,14 +64,13 @@ export const createWorkspaceMigrationsForRemoteObjectRelations = async (
|
||||
action: WorkspaceMigrationTableActionType.ALTER,
|
||||
columns: [
|
||||
{
|
||||
action: WorkspaceMigrationColumnActionType.CREATE_COMMENT,
|
||||
comment: await buildCommentForRemoteObjectForeignKey(
|
||||
favoriteObjectMetadata.nameSingular,
|
||||
createdObjectName,
|
||||
schema,
|
||||
workspaceDataSource,
|
||||
),
|
||||
},
|
||||
action: WorkspaceMigrationColumnActionType.CREATE,
|
||||
columnName: computeColumnName(createdObjectMetadata.nameSingular, {
|
||||
isForeignKey: true,
|
||||
}),
|
||||
columnType: primaryKeyColumnType,
|
||||
isNullable: true,
|
||||
} satisfies WorkspaceMigrationColumnCreate,
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user