Repository: twentyhq/twenty
File: packages/twenty-server/src/database/commands/upgrade-version-command/1-16/1-16-backfill-updated-by-field.command.ts (TypeScript)

Code before:
      WORKFLOW_RUN_STANDARD_FIELD_IDS.updatedBy,
    [STANDARD_OBJECT_IDS.dashboard]: DASHBOARD_STANDARD_FIELD_IDS.updatedBy,
  };

  constructor(
    @InjectRepository(WorkspaceEntity)
    protected readonly workspaceRepository: Repository<WorkspaceEntity>,
    protected readonly twentyORMGlobalManager: GlobalWorkspaceOrmManager,
    protected readonly dataSourceService: DataSourceService,
    @InjectRepository(ObjectMetadataEntity)
    private readonly objectMetadataRepository: Repository<ObjectMetadataEntity>,
    private readonly fieldMetadataService: FieldMetadataService,
    private readonly workspaceCacheService: WorkspaceCacheService,
  ) {
    super(workspaceRepository, twentyORMGlobalManager, dataSourceService);
  }

  override async runOnWorkspace({
    workspaceId,
    options,
  }: RunOnWorkspaceArgs): Promise<void> {
    this.logger.log(
      `Starting backfill of updatedBy field for workspace ${workspaceId}`,
    );

    const standardObjectMetadataList = await this.objectMetadataRepository.find(
      {
        where: {
          workspaceId,
          standardId: In(Object.keys(this.objectToUpdatedByStandardIdMap)),
        },
        relations: ['fields'],
      },
    );

    const customObjectMetadataList = await this.objectMetadataRepository.find({
      where: {
        workspaceId,
        isCustom: true,
      },
      relations: ['fields'],
    });

    const objectMetadataList = [
      ...standardObjectMetadataList,
      ...customObjectMetadataList,
    ];

    if (objectMetadataList.length === 0) {
      this.logger.log(
        `No objects found that need updatedBy field for workspace ${workspaceId}`,

Review comment (performance):
***Suggestion:*** Unless I'm mistaken we could avoid making two and if it's not any find queries here by reading the flat object and flat field cache entries

Code after:
    [STANDARD_OBJECT_IDS.note]: NOTE_STANDARD_FIELD_IDS.updatedBy,
    [STANDARD_OBJECT_IDS.task]: TASK_STANDARD_FIELD_IDS.updatedBy,
    [STANDARD_OBJECT_IDS.opportunity]: OPPORTUNITY_STANDARD_FIELD_IDS.updatedBy,
    [STANDARD_OBJECT_IDS.workflow]: WORKFLOW_STANDARD_FIELD_IDS.updatedBy,
    [STANDARD_OBJECT_IDS.workflowRun]:
      WORKFLOW_RUN_STANDARD_FIELD_IDS.updatedBy,
    [STANDARD_OBJECT_IDS.dashboard]: DASHBOARD_STANDARD_FIELD_IDS.updatedBy,
  };

  constructor(
    @InjectRepository(WorkspaceEntity)
    protected readonly workspaceRepository: Repository<WorkspaceEntity>,
    protected readonly twentyORMGlobalManager: GlobalWorkspaceOrmManager,
    protected readonly dataSourceService: DataSourceService,
    private readonly fieldMetadataService: FieldMetadataService,
    private readonly workspaceCacheService: WorkspaceCacheService,
    private readonly applicationService: ApplicationService,
  ) {
    super(workspaceRepository, twentyORMGlobalManager, dataSourceService);
  }

  override async runOnWorkspace({
    workspaceId,
    options,
  }: RunOnWorkspaceArgs): Promise<void> {
    this.logger.log(
      `Starting backfill of updatedBy field for workspace ${workspaceId}`,
    );

    const { twentyStandardFlatApplication, workspaceCustomFlatApplication } =
      await this.applicationService.findWorkspaceTwentyStandardAndCustomApplicationOrThrow(
        {
          workspaceId,
        },
      );

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

    const objectMetadataList = Object.values(
      flatObjectMetadataMaps.byId,
    ).filter(isDefined);

    if (objectMetadataList.length === 0) {
      this.logger.log(
        `No objects found that need updatedBy field for workspace ${workspaceId}`,
      );