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:
    this.logger.log(`Migrating attachments for workspace ${workspaceId}`);

    if (isMigrated) {
      this.logger.log(`Attachment migration already completed. Skipping...`);

      return;
    }

    if (options.dryRun) {
      this.logger.log(
        `Would have migrated attachments for workspace ${workspaceId}. Skipping...`,
      );

      return;
    }

    const queryRunner = this.coreDataSource.createQueryRunner();

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

    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'],

Review comment (performance):
Lets use `WorkspaceCacheService` here and `flatObjectMetadataMaps` instead

Code after:
  override async runOnWorkspace({
    workspaceId,
    options,
  }: RunOnWorkspaceArgs): Promise<void> {
    const isMigrated = await this.featureFlagService.isFeatureEnabled(
      FeatureFlagKey.IS_ATTACHMENT_MIGRATED,
      workspaceId,
    );

    this.logger.log(`Migrating attachments for workspace ${workspaceId}`);

    if (isMigrated) {
      this.logger.log(`Attachment migration already completed. Skipping...`);

      return;
    }

    if (options.dryRun) {
      this.logger.log(
        `Would have migrated attachments for workspace ${workspaceId}. Skipping...`,
      );

      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,