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:
        `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'],
        });

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

        return;
      }


Review comment (suggestion):
***Remark:*** Please always use `STANDARD_OBJECTS` even though until we've totally deprecate standardId it might for some of them be a bridge to below variables

Code after:

    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,
        STANDARD_OBJECTS.attachment.fields.targetDashboard.universalIdentifier,
        STANDARD_OBJECTS.attachment.fields.targetWorkflow.universalIdentifier,
      ]);

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