Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: populate join column for new relation #9974

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@
"tslib": "^2.3.0",
"tsup": "^8.2.4",
"type-fest": "4.10.1",
"typescript": "5.3.3",
"typescript": "5.7.2",
"use-context-selector": "^2.0.0",
"use-debounce": "^10.0.0",
"uuid": "^9.0.0",
Expand Down Expand Up @@ -356,7 +356,7 @@
"resolutions": {
"graphql": "16.8.0",
"type-fest": "4.10.1",
"typescript": "5.3.3",
"typescript": "5.7.2",
"prosemirror-model": "1.23.0"
},
"version": "0.2.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/twenty-front/.swcrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"jsc": {
"target": "es2017",
"target": "es2018",
"parser": {
"syntax": "typescript",
"tsx": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/twenty-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"rimraf": "^5.0.5",
"twenty-emails": "workspace:*",
"twenty-shared": "workspace:*",
"typescript": "5.3.3"
"typescript": "5.7.2"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: TypeScript 5.7.2 requires Node.js >= 18.18.0, but the engine specification on line 84 requires ^18.17.1. This could lead to compatibility issues.

Suggested change
"typescript": "5.7.2"
"node": "^18.18.0",

},
"engines": {
"node": "^18.17.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { Repository } from 'typeorm';
import { ActiveWorkspacesCommandRunner } from 'src/database/commands/active-workspaces.command';
import { BaseCommandOptions } from 'src/database/commands/base.command';
import { AddContextToActorCompositeTypeCommand } from 'src/database/commands/upgrade-version/0-41/0-41-add-context-to-actor-composite-type';
import { MigrateRelationsToFieldMetadataCommand } from 'src/database/commands/upgrade-version/0-41/0-41-migrate-relations-to-field-metadata.command';
import { RemoveDuplicateMcmasCommand } from 'src/database/commands/upgrade-version/0-41/0-41-remove-duplicate-mcmas';
import { SeedWorkflowViewsCommand } from 'src/database/commands/upgrade-version/0-41/0-41-seed-workflow-views.command';
import { MigrateRelationsToFieldMetadataCommand } from 'src/database/commands/upgrade-version/0-42/0-42-migrate-relations-to-field-metadata.command';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: The migration command has been moved from 0.41 to 0.42 but is still being used in the 0.41 upgrade. This could cause version compatibility issues if there are dependencies between migrations.

import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import { SyncWorkspaceMetadataCommand } from 'src/engine/workspace-manager/workspace-sync-metadata/commands/sync-workspace-metadata.command';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

import { AddContextToActorCompositeTypeCommand } from 'src/database/commands/upgrade-version/0-41/0-41-add-context-to-actor-composite-type';
import { MigrateRelationsToFieldMetadataCommand } from 'src/database/commands/upgrade-version/0-41/0-41-migrate-relations-to-field-metadata.command';
import { RemoveDuplicateMcmasCommand } from 'src/database/commands/upgrade-version/0-41/0-41-remove-duplicate-mcmas';
import { SeedWorkflowViewsCommand } from 'src/database/commands/upgrade-version/0-41/0-41-seed-workflow-views.command';
import { UpgradeTo0_41Command } from 'src/database/commands/upgrade-version/0-41/0-41-upgrade-version.command';
Expand Down Expand Up @@ -37,7 +36,6 @@ import { WorkspaceSyncMetadataModule } from 'src/engine/workspace-manager/worksp
SyncWorkspaceMetadataCommand,
SeedWorkflowViewsCommand,
UpgradeTo0_41Command,
MigrateRelationsToFieldMetadataCommand,
AddContextToActorCompositeTypeCommand,
RemoveDuplicateMcmasCommand,
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { InjectRepository } from '@nestjs/typeorm';
import chalk from 'chalk';
import { Command } from 'nest-commander';
import { FieldMetadataType } from 'twenty-shared';
import { Repository } from 'typeorm';
import { In, Repository } from 'typeorm';

import { RelationType } from 'src/engine/metadata-modules/field-metadata/interfaces/relation-type.interface';

Expand All @@ -18,9 +18,10 @@ import {
deduceRelationDirection,
RelationDirection,
} from 'src/engine/utils/deduce-relation-direction.util';
import { isFieldMetadataEntityOfType } from 'src/engine/utils/is-field-metadata-of-type.util';

@Command({
name: 'upgrade-0.41:migrate-relations-to-field-metadata',
name: 'upgrade-0.42:migrate-relations-to-field-metadata',
description: 'Migrate relations to field metadata',
})
export class MigrateRelationsToFieldMetadataCommand extends ActiveWorkspacesCommandRunner {
Expand Down Expand Up @@ -65,10 +66,13 @@ export class MigrateRelationsToFieldMetadataCommand extends ActiveWorkspacesComm
`Running command for workspace ${workspaceId} ${index + 1}/${total}`,
);

const fieldMetadataCollection = (await this.fieldMetadataRepository.find({
where: { workspaceId, type: FieldMetadataType.RELATION },
const fieldMetadataCollection = await this.fieldMetadataRepository.find({
where: {
workspaceId,
type: In([FieldMetadataType.RELATION, FieldMetadataType.UUID]),
},
relations: ['fromRelationMetadata', 'toRelationMetadata'],
})) as unknown as FieldMetadataEntity<FieldMetadataType.RELATION>[];
});

if (!fieldMetadataCollection.length) {
this.logger.log(
Expand All @@ -80,10 +84,25 @@ export class MigrateRelationsToFieldMetadataCommand extends ActiveWorkspacesComm
return;
}

const fieldMetadataToUpdateCollection = fieldMetadataCollection.map(
(fieldMetadata) => this.mapFieldMetadata(fieldMetadata),
const joinColumnFieldMetadataCollection = fieldMetadataCollection.filter(
(fieldMetadata) =>
isFieldMetadataEntityOfType(fieldMetadata, FieldMetadataType.UUID),
);

const fieldMetadataToUpdateCollection = fieldMetadataCollection
.filter((fieldMetadata) =>
isFieldMetadataEntityOfType(
fieldMetadata,
FieldMetadataType.RELATION,
),
)
.map((fieldMetadata) =>
this.updateRelationFieldMetadata(
joinColumnFieldMetadataCollection,
fieldMetadata,
),
);

if (fieldMetadataToUpdateCollection.length > 0) {
await this.fieldMetadataRepository.save(
fieldMetadataToUpdateCollection,
Expand All @@ -98,11 +117,16 @@ export class MigrateRelationsToFieldMetadataCommand extends ActiveWorkspacesComm
}
}

private mapFieldMetadata(
private updateRelationFieldMetadata(
joinColumnFieldMetadataCollection: FieldMetadataEntity<FieldMetadataType.UUID>[],
fieldMetadata: FieldMetadataEntity<FieldMetadataType.RELATION>,
): FieldMetadataEntity<FieldMetadataType.RELATION> {
const relationMetadata =
fieldMetadata.fromRelationMetadata ?? fieldMetadata.toRelationMetadata;
const joinColumnFieldMetadata = joinColumnFieldMetadataCollection.find(
(joinColumnFieldMetadata) =>
joinColumnFieldMetadata.name === `${fieldMetadata.name}Id`,
);
Comment on lines +126 to +129
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Join column matching relies on exact name pattern. Consider adding a more flexible matching mechanism or validation for edge cases


const relationDirection = deduceRelationDirection(
fieldMetadata,
Expand Down Expand Up @@ -132,6 +156,7 @@ export class MigrateRelationsToFieldMetadataCommand extends ActiveWorkspacesComm
settings: {
relationType,
onDelete: relationMetadata.onDeleteAction,
joinColumnName: joinColumnFieldMetadata?.name,
},
relationTargetFieldMetadataId,
relationTargetObjectMetadataId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { TypeOrmModule } from '@nestjs/typeorm';

import { FixBodyV2ViewFieldPositionCommand } from 'src/database/commands/upgrade-version/0-42/0-42-fix-body-v2-view-field-position.command';
import { LimitAmountOfViewFieldCommand } from 'src/database/commands/upgrade-version/0-42/0-42-limit-amount-of-view-field';
import { MigrateRelationsToFieldMetadataCommand } from 'src/database/commands/upgrade-version/0-42/0-42-migrate-relations-to-field-metadata.command';
import { MigrateRichTextFieldCommand } from 'src/database/commands/upgrade-version/0-42/0-42-migrate-rich-text-field.command';
import { UpgradeTo0_42Command } from 'src/database/commands/upgrade-version/0-42/0-42-upgrade-version.command';
import { TypeORMModule } from 'src/database/typeorm/typeorm.module';
import { FeatureFlag } from 'src/engine/core-modules/feature-flag/feature-flag.entity';
import { FeatureFlagModule } from 'src/engine/core-modules/feature-flag/feature-flag.module';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
Expand All @@ -23,7 +25,7 @@ import { WorkspaceSyncMetadataCommandsModule } from 'src/engine/workspace-manage
[ObjectMetadataEntity, FieldMetadataEntity],
'metadata',
),
WorkspaceSyncMetadataCommandsModule,
TypeORMModule,
WorkspaceMigrationRunnerModule,
WorkspaceMigrationModule,
WorkspaceMetadataVersionModule,
Expand All @@ -35,6 +37,7 @@ import { WorkspaceSyncMetadataCommandsModule } from 'src/engine/workspace-manage
MigrateRichTextFieldCommand,
FixBodyV2ViewFieldPositionCommand,
LimitAmountOfViewFieldCommand,
MigrateRelationsToFieldMetadataCommand,
],
})
export class UpgradeTo0_42CommandModule {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Injectable } from '@nestjs/common';

import { FieldMetadataType } from 'twenty-shared';
import {
DataSource,
FindOptionsRelations,
Expand All @@ -22,7 +23,7 @@ import { ObjectMetadataItemWithFieldMaps } from 'src/engine/metadata-modules/typ
import { ObjectMetadataMaps } from 'src/engine/metadata-modules/types/object-metadata-maps';
import { getObjectMetadataMapItemByNameSingular } from 'src/engine/metadata-modules/utils/get-object-metadata-map-item-by-name-singular.util';
import { formatResult } from 'src/engine/twenty-orm/utils/format-result.util';
import { isRelationFieldMetadata } from 'src/engine/utils/is-relation-field-metadata.util';
import { isFieldMetadataInterfaceOfType } from 'src/engine/utils/is-field-metadata-of-type.util';

@Injectable()
export class ProcessNestedRelationsV2Helper {
Expand Down Expand Up @@ -96,7 +97,12 @@ export class ProcessNestedRelationsV2Helper {
const sourceFieldMetadata =
parentObjectMetadataItem.fieldsByName[sourceFieldName];

if (!isRelationFieldMetadata(sourceFieldMetadata)) {
if (
!isFieldMetadataInterfaceOfType(
sourceFieldMetadata,
FieldMetadataType.RELATION,
)
) {
// TODO: Maybe we should throw an error here ?
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable, Logger } from '@nestjs/common';

import { isDefined } from 'twenty-shared';
import { FieldMetadataType, isDefined } from 'twenty-shared';

import { ObjectRecord } from 'src/engine/api/graphql/workspace-query-builder/interfaces/object-record.interface';
import { QueryResultFieldValue } from 'src/engine/api/graphql/workspace-query-runner/factories/query-result-getters/interfaces/query-result-field-value';
Expand All @@ -22,7 +22,7 @@ import { FeatureFlagKey } from 'src/engine/core-modules/feature-flag/enums/featu
import { FeatureFlagService } from 'src/engine/core-modules/feature-flag/services/feature-flag.service';
import { FileService } from 'src/engine/core-modules/file/services/file.service';
import { ObjectMetadataMaps } from 'src/engine/metadata-modules/types/object-metadata-maps';
import { isRelationFieldMetadata } from 'src/engine/utils/is-relation-field-metadata.util';
import { isFieldMetadataInterfaceOfType } from 'src/engine/utils/is-field-metadata-of-type.util';

// TODO: find a way to prevent conflict between handlers executing logic on object relations
// And this factory that is also executing logic on object relations
Expand Down Expand Up @@ -150,7 +150,12 @@ export class QueryResultGettersFactory {
objectMetadataMapItem.fieldsByName[recordFieldName],
)
.filter(isDefined)
.filter((fieldMetadata) => isRelationFieldMetadata(fieldMetadata));
.filter((fieldMetadata) =>
isFieldMetadataInterfaceOfType(
fieldMetadata,
FieldMetadataType.RELATION,
),
);

const relationFieldsProcessedMap = {} as Record<
string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
GraphQLFieldConfigMap,
GraphQLObjectType,
} from 'graphql';
import { FieldMetadataType } from 'twenty-shared';

import { WorkspaceBuildSchemaOptions } from 'src/engine/api/graphql/workspace-schema-builder/interfaces/workspace-build-schema-optionts.interface';
import { ObjectMetadataInterface } from 'src/engine/metadata-modules/field-metadata/interfaces/object-metadata.interface';
Expand All @@ -14,7 +15,7 @@ import { RelationTypeV2Factory } from 'src/engine/api/graphql/workspace-schema-b
import { TypeDefinitionsStorage } from 'src/engine/api/graphql/workspace-schema-builder/storages/type-definitions.storage';
import { getResolverArgs } from 'src/engine/api/graphql/workspace-schema-builder/utils/get-resolver-args.util';
import { objectContainsRelationField } from 'src/engine/api/graphql/workspace-schema-builder/utils/object-contains-relation-field';
import { isRelationFieldMetadata } from 'src/engine/utils/is-relation-field-metadata.util';
import { isFieldMetadataInterfaceOfType } from 'src/engine/utils/is-field-metadata-of-type.util';

import { ArgsFactory } from './args.factory';

Expand Down Expand Up @@ -107,7 +108,12 @@ export class ExtendObjectTypeDefinitionV2Factory {

for (const fieldMetadata of objectMetadata.fields) {
// Ignore non-relation fields as they are already defined
if (!isRelationFieldMetadata(fieldMetadata)) {
if (
!isFieldMetadataInterfaceOfType(
fieldMetadata,
FieldMetadataType.RELATION,
)
) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export interface TypeOptions<T = any> {
isArray?: boolean;
arrayDepth?: number;
defaultValue?: T;
settings?: FieldMetadataSettings<FieldMetadataType | 'default'>;
settings?: FieldMetadataSettings<FieldMetadataType>;
isIdField?: boolean;
}

Expand All @@ -55,7 +55,7 @@ const StringArrayScalarType = new GraphQLList(GraphQLString);
export class TypeMapperService {
mapToScalarType(
fieldMetadataType: FieldMetadataType,
settings?: FieldMetadataSettings<FieldMetadataType | 'default'>,
settings?: FieldMetadataSettings<FieldMetadataType>,
isIdField?: boolean,
): GraphQLScalarType | undefined {
if (isIdField || settings?.isForeignKey) {
Expand Down Expand Up @@ -90,7 +90,7 @@ export class TypeMapperService {

mapToFilterType(
fieldMetadataType: FieldMetadataType,
settings?: FieldMetadataSettings<FieldMetadataType | 'default'>,
settings?: FieldMetadataSettings<FieldMetadataType>,
isIdField?: boolean,
): GraphQLInputObjectType | GraphQLScalarType | undefined {
if (isIdField || settings?.isForeignKey) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ registerEnumType(FieldMetadataType, {
@Relation('object', () => ObjectMetadataDTO, {
nullable: true,
})
export class FieldMetadataDTO<
T extends FieldMetadataType | 'default' = 'default',
> {
export class FieldMetadataDTO<T extends FieldMetadataType = FieldMetadataType> {
@IsUUID()
@IsNotEmpty()
@IDField(() => UUIDScalarType)
Expand All @@ -75,7 +73,7 @@ export class FieldMetadataDTO<
@IsEnum(FieldMetadataType)
@IsNotEmpty()
@Field(() => FieldMetadataType)
type: FieldMetadataType;
type: T;

@IsString()
@IsNotEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class TextSettingsValidation {

@Injectable()
export class FieldMetadataValidationService<
T extends FieldMetadataType | 'default' = 'default',
T extends FieldMetadataType = FieldMetadataType,
> {
constructor() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { RelationMetadataEntity } from 'src/engine/metadata-modules/relation-met
'relationTargetObjectMetadataId',
])
export class FieldMetadataEntity<
T extends FieldMetadataType | 'default' = 'default',
T extends FieldMetadataType = FieldMetadataType,
> implements FieldMetadataInterface<T>
{
@PrimaryGeneratedColumn('uuid')
Expand All @@ -59,7 +59,7 @@ export class FieldMetadataEntity<
nullable: false,
type: 'varchar',
})
type: FieldMetadataType;
type: T;

@Column({ nullable: false })
name: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FieldMetadataType } from 'twenty-shared';
import { FieldMetadataType, IsExactly } from 'twenty-shared';

import {
FieldMetadataDefaultActor,
Expand Down Expand Up @@ -60,17 +60,14 @@ export type FieldMetadataFunctionDefaultValue = ExtractValueType<
FieldMetadataDefaultValueUuidFunction | FieldMetadataDefaultValueNowFunction
>;

type DefaultValueByFieldMetadata<T extends FieldMetadataType | 'default'> = [
T,
] extends [keyof FieldMetadataDefaultValueMapping]
? ExtractValueType<FieldMetadataDefaultValueMapping[T]> | null
: T extends 'default'
? ExtractValueType<UnionOfValues<FieldMetadataDefaultValueMapping>> | null
: never;

export type FieldMetadataDefaultValue<
T extends FieldMetadataType | 'default' = 'default',
> = DefaultValueByFieldMetadata<T>;
T extends FieldMetadataType = FieldMetadataType,
> =
IsExactly<T, FieldMetadataType> extends true
? ExtractValueType<UnionOfValues<FieldMetadataDefaultValueMapping>> | null
: T extends keyof FieldMetadataDefaultValueMapping
? ExtractValueType<FieldMetadataDefaultValueMapping[T]> | null
: never;

type FieldMetadataDefaultValueExtractedTypes = {
[K in keyof FieldMetadataDefaultValueMapping]: ExtractValueType<
Expand Down
Loading
Loading