Repository: twentyhq/twenty
File: packages/twenty-server/src/database/commands/upgrade-version-command/1-17/1-17-migrate-attachment-to-morph-relations.command.ts (TypeScript)

Code before:

    try {
      const schemaName = getWorkspaceSchemaName(workspaceId);
      const tableName = 'attachment';

      const relatedObjectMetadata = await this.objectMetadataRepository.find({
        where: {
          workspaceId,
        },
      });
      const relatedObjectMetadataMap = new Map(
        relatedObjectMetadata.map((obj) => [obj.id, obj]),
      );

      const attachmentTargetFieldUniversalIdentifiers = new Set<string>([
        ATTACHMENT_STANDARD_FIELD_IDS.targetTask,
        ATTACHMENT_STANDARD_FIELD_IDS.targetNote,
        ATTACHMENT_STANDARD_FIELD_IDS.targetPerson,
        ATTACHMENT_STANDARD_FIELD_IDS.targetCompany,
        ATTACHMENT_STANDARD_FIELD_IDS.targetOpportunity,
        ATTACHMENT_STANDARD_FIELD_IDS.targetDashboard,
        ATTACHMENT_STANDARD_FIELD_IDS.targetWorkflow,
      ]);

      const attachmentObjectMetadata =
        await this.objectMetadataRepository.findOne({
          where: {
            workspaceId,
            universalIdentifier: STANDARD_OBJECT_IDS.attachment,
          },
          relations: ['fields'],
        });

      if (!attachmentObjectMetadata) {
        this.logger.error(
          `🟥 Attachment object metadata not found for workspace ${workspaceId}`,
        );

        return;
      }

      const attachmentTargetRelationFields = attachmentObjectMetadata.fields
        .filter((field) => field.type === 'RELATION')
        .filter((field) => {
          if (!field.relationTargetObjectMetadataId) {
            return false;
          }

          const relatedObject = relatedObjectMetadataMap.get(
            field.relationTargetObjectMetadataId,
          );

Review comment (performance):
Same use cache with `findFlatEntityFromUniversalIdentifier`

Code after:
      );

      return;
    }

    const queryRunner = this.coreDataSource.createQueryRunner();

    await queryRunner.connect();
    await queryRunner.startTransaction();

    try {
      const schemaName = getWorkspaceSchemaName(workspaceId);
      const tableName = 'attachment';

      const {
        flatObjectMetadataMaps,
        flatFieldMetadataMaps,
        flatApplicationMaps,
      } = await this.workspaceCacheService.getOrRecompute(workspaceId, [
        'flatObjectMetadataMaps',
        'flatFieldMetadataMaps',
        'flatApplicationMaps',
      ]);

      const attachmentTargetFieldUniversalIdentifiers = new Set<string>([
        STANDARD_OBJECTS.attachment.fields.targetTask.universalIdentifier,
        STANDARD_OBJECTS.attachment.fields.targetNote.universalIdentifier,
        STANDARD_OBJECTS.attachment.fields.targetPerson.universalIdentifier,
        STANDARD_OBJECTS.attachment.fields.targetCompany.universalIdentifier,
        STANDARD_OBJECTS.attachment.fields.targetOpportunity
          .universalIdentifier,
        STANDARD_OBJECTS.attachment.fields.targetDashboard.universalIdentifier,
        STANDARD_OBJECTS.attachment.fields.targetWorkflow.universalIdentifier,
      ]);

      const attachmentObjectMetadata =
        findFlatEntityByUniversalIdentifier<FlatObjectMetadata>({
          flatEntityMaps: flatObjectMetadataMaps,
          universalIdentifier: STANDARD_OBJECTS.attachment.universalIdentifier,
        });

      if (!attachmentObjectMetadata) {
        this.logger.error(
          `🟥 Attachment object metadata not found for workspace ${workspaceId}`,
        );

        return;
      }

      const attachmentFieldMetadatas = getFlatFieldsFromFlatObjectMetadata(
        attachmentObjectMetadata,