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:
        .flatMap((field) => {
          const relatedObject = this.getRelatedObjectMetadata({
            field,
            flatObjectMetadataMaps,
            workspaceId,
          });

          if (!isDefined(relatedObject)) {
            return [];
          }

          const isStandardAppField = this.isTwentyStandardApplicationField({
            field,
            flatApplicationMaps,
            workspaceId,
          });
          const isStandardTarget =
            isStandardAppField &&
            attachmentTargetFieldUniversalIdentifiers.has(
              field.universalIdentifier,
            );
          const isCustomTarget =
            !isStandardAppField &&
            field.standardId === ATTACHMENT_STANDARD_FIELD_IDS.targetCustom;

          if (!isStandardTarget && !isCustomTarget) {
            return [];
          }

          return [field];
        });

      const fieldMigrations = attachmentTargetRelationFields.map((field) => {
        const newFieldName = `target${capitalize(field.name)}`;
        const relationSettings: RelationFieldMetadataSettings = field.settings;
        const oldJoinColumnName =
          relationSettings?.joinColumnName ??
          computeMorphOrRelationFieldJoinColumnName({ name: field.name });
        const newJoinColumnName = computeMorphOrRelationFieldJoinColumnName({
          name: newFieldName,
        });

        return {
          field,
          newFieldName,
          oldJoinColumnName,
          newJoinColumnName,
        };
      });

      for (const { oldJoinColumnName, newJoinColumnName } of fieldMigrations) {

Review comment (suggestion):
Quite though to read IMO, if I understand correctly could be replaced by
```ts
          if (isStandardTarget || isCustomTarget) {
return [field]
}
```

Code after:
            field,
            flatApplicationMaps,
            workspaceId,
          });
          const isStandardTarget =
            isStandardAppField &&
            attachmentTargetFieldUniversalIdentifiers.has(
              field.universalIdentifier,
            );
          const targetObjectMetadata = field.relationTargetObjectMetadataId
            ? flatObjectMetadataMaps.byId[field.relationTargetObjectMetadataId]
            : undefined;
          const isCustomTarget =
            !isStandardAppField && targetObjectMetadata?.isCustom === true;

          return isStandardTarget || isCustomTarget;
        });

      const fieldMigrations = attachmentTargetRelationFields.map((field) => {
        const newFieldName = `target${capitalize(field.name)}`;
        const relationSettings: RelationFieldMetadataSettings = field.settings;
        const oldJoinColumnName =
          relationSettings?.joinColumnName ??
          computeMorphOrRelationFieldJoinColumnName({ name: field.name });
        const newJoinColumnName = computeMorphOrRelationFieldJoinColumnName({
          name: newFieldName,
        });

        return {
          field,
          newFieldName,
          oldJoinColumnName,
          newJoinColumnName,
        };
      });

      for (const { oldJoinColumnName, newJoinColumnName } of fieldMigrations) {
        if (oldJoinColumnName === newJoinColumnName) {
          this.logger.log(
            `Column "${oldJoinColumnName}" already renamed. Skipping...`,
          );
          continue;
        }

        try {
          await queryRunner.query(
            `ALTER TABLE "${schemaName}"."${tableName}"
           RENAME COLUMN "${oldJoinColumnName}" TO "${newJoinColumnName}"`,
          );
          this.logger.log(
            `Renamed column "${oldJoinColumnName}" to "${newJoinColumnName}" for "${tableName}"`,