Repository: twentyhq/twenty
File: packages/twenty-server/src/database/commands/upgrade-version-command/1-15/1-15-fix-nan-position-values-in-notes.command.ts (TypeScript)

Code before:

@Command({
  name: 'upgrade:1-15:fix-nan-position-values-in-notes',
  description: 'Fix NaN position values in notes by replacing them with 2',
})
export class FixNanPositionValuesInNotesCommand extends ActiveOrSuspendedWorkspacesMigrationCommandRunner {
  protected readonly logger = new Logger(
    FixNanPositionValuesInNotesCommand.name,
  );

  constructor(
    @InjectRepository(WorkspaceEntity)
    protected readonly workspaceRepository: Repository<WorkspaceEntity>,
    protected readonly globalWorkspaceOrmManager: GlobalWorkspaceOrmManager,
    protected readonly dataSourceService: DataSourceService,
  ) {
    super(workspaceRepository, globalWorkspaceOrmManager, dataSourceService);
  }

  override async runOnWorkspace({
    workspaceId,
    options,
    dataSource,
  }: RunOnWorkspaceArgs): Promise<void> {
    const isDryRun = options.dryRun || false;

    if (!isDefined(dataSource)) {
      throw new Error(
        `Could not find data source for workspace ${workspaceId}, should never occur`,
      );
    }

    const schemaName = getWorkspaceSchemaName(workspaceId);

    if (isDryRun) {
      this.logger.log('Dry run mode: No changes will be applied');
    }

    try {
      // Count NaN position values in notes
      const countResult = await dataSource.query(
        `SELECT COUNT(*) as count FROM "${schemaName}"."note" WHERE "position" = 'NaN'::float8`,
        [],
        undefined,
        { shouldBypassPermissionChecks: true },
      );

      const nanCount = parseInt(countResult[0]?.count || '0', 10);

      this.logger.log(
        `Found ${nanCount} note(s) with NaN position values in workspace ${workspaceId}`,

Review comment (suggestion):
note: could this really be undefined? I think we should throw in ActiveOrSuspendedWorkspacesMigrationCommandRunner then 🤔 
No need to change in this PR, just a thought

Code after:
@Command({
  name: 'upgrade:1-15:fix-nan-position-values-in-notes',
  description: 'Fix NaN position values in notes by replacing them with 2',
})
export class FixNanPositionValuesInNotesCommand extends ActiveOrSuspendedWorkspacesMigrationCommandRunner {
  protected readonly logger = new Logger(
    FixNanPositionValuesInNotesCommand.name,
  );

  constructor(
    @InjectRepository(WorkspaceEntity)
    protected readonly workspaceRepository: Repository<WorkspaceEntity>,
    protected readonly globalWorkspaceOrmManager: GlobalWorkspaceOrmManager,
    protected readonly dataSourceService: DataSourceService,
  ) {
    super(workspaceRepository, globalWorkspaceOrmManager, dataSourceService);
  }

  override async runOnWorkspace({
    workspaceId,
    options,
    dataSource,
  }: RunOnWorkspaceArgs): Promise<void> {
    const isDryRun = options.dryRun || false;

    if (!isDefined(dataSource)) {
      throw new Error(
        `Could not find data source for workspace ${workspaceId}, should never occur`,
      );
    }

    if (isDryRun) {
      this.logger.log('Dry run mode: No changes will be applied');
    }

    try {
      // Count NaN position values in notes
      const noteRepository = await this.globalWorkspaceOrmManager.getRepository(
        workspaceId,
        'note',
        { shouldBypassPermissionChecks: true },
      );

      const nanCount = await noteRepository.count({
        where: { position: 'NaN' },
      });

      this.logger.log(
        `Found ${nanCount} note(s) with NaN position values in workspace ${workspaceId}`,
      );