-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Changes from all commits
c321de5
2af4c0b
5d484e6
1cc8209
ae832e1
5b1db9c
61e878a
7cc53b8
0b42f67
5aa691e
256ad8f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"jsc": { | ||
"target": "es2017", | ||
"target": "es2018", | ||
"parser": { | ||
"syntax": "typescript", | ||
"tsx": true, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
||
|
@@ -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 { | ||
|
@@ -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( | ||
|
@@ -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, | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -132,6 +156,7 @@ export class MigrateRelationsToFieldMetadataCommand extends ActiveWorkspacesComm | |
settings: { | ||
relationType, | ||
onDelete: relationMetadata.onDeleteAction, | ||
joinColumnName: joinColumnFieldMetadata?.name, | ||
}, | ||
relationTargetFieldMetadataId, | ||
relationTargetObjectMetadataId, | ||
|
There was a problem hiding this comment.
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.