Skip to content

Commit

Permalink
Fix viewFilter operand for dateTime fields (#7306)
Browse files Browse the repository at this point in the history
  • Loading branch information
Weiko authored Sep 27, 2024
1 parent 942281f commit ae6777f
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { InjectRepository } from '@nestjs/typeorm';

import chalk from 'chalk';
import { Command } from 'nest-commander';
import { Any, Repository } from 'typeorm';

import {
ActiveWorkspacesCommandOptions,
ActiveWorkspacesCommandRunner,
} from 'src/database/commands/active-workspaces.command';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service';
import {
FieldMetadataEntity,
FieldMetadataType,
} from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
import { ViewFilterWorkspaceEntity } from 'src/modules/view/standard-objects/view-filter.workspace-entity';

@Command({
name: 'upgrade-0.30:fix-view-filter-operand-for-date-time',
description: 'Fix the view filter operand for date time fields',
})
export class FixViewFilterOperandForDateTimeCommand extends ActiveWorkspacesCommandRunner {
constructor(
@InjectRepository(Workspace, 'core')
protected readonly workspaceRepository: Repository<Workspace>,
@InjectRepository(FieldMetadataEntity, 'metadata')
private readonly fieldMetadataRepository: Repository<FieldMetadataEntity>,
private readonly dataSourceService: DataSourceService,
private readonly twentyORMGlobalManager: TwentyORMGlobalManager,
) {
super(workspaceRepository);
}

async executeActiveWorkspacesCommand(
_passedParam: string[],
_options: ActiveWorkspacesCommandOptions,
workspaceIds: string[],
): Promise<void> {
for (const workspaceId of workspaceIds) {
try {
const dataSourceMetadata =
await this.dataSourceService.getLastDataSourceMetadataFromWorkspaceId(
workspaceId,
);

if (!dataSourceMetadata) {
throw new Error(
`Could not find dataSourceMetadata for workspace ${workspaceId}`,
);
}

const viewFilterRepository =
await this.twentyORMGlobalManager.getRepositoryForWorkspace<ViewFilterWorkspaceEntity>(
workspaceId,
'viewFilter',
);

const dateTimeFieldMetadata = await this.fieldMetadataRepository.find({
where: {
workspaceId,
type: FieldMetadataType.DATE_TIME,
},
});

const dateTimeFieldMetadataIds = dateTimeFieldMetadata.map(
(fieldMetadata) => fieldMetadata.id,
);

const lessThanUpdatedResult = await viewFilterRepository.update(
{
operand: 'lessThan',
fieldMetadataId: Any(dateTimeFieldMetadataIds),
},
{
operand: 'isBefore',
},
);

const greaterThanUpdatedResult = await viewFilterRepository.update(
{
operand: 'greaterThan',
fieldMetadataId: Any(dateTimeFieldMetadataIds),
},
{
operand: 'isAfter',
},
);

this.logger.log(
`Updated ${(lessThanUpdatedResult.affected ?? 0) + (greaterThanUpdatedResult.affected ?? 0)} view filters for workspace ${workspaceId}`,
);
} catch (error) {
this.logger.log(
chalk.red(
`Error running command for workspace ${workspaceId}: ${error}`,
),
);
continue;
} finally {
this.logger.log(
chalk.green(`Finished running command for workspace ${workspaceId}.`),
);
}

this.logger.log(chalk.green(`Command completed!`));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Repository } from 'typeorm';

import { ActiveWorkspacesCommandRunner } from 'src/database/commands/active-workspaces.command';
import { FixEmailFieldsToEmailsCommand } from 'src/database/commands/upgrade-version/0-30/0-30-fix-email-field-migration.command';
import { FixViewFilterOperandForDateTimeCommand } from 'src/database/commands/upgrade-version/0-30/0-30-fix-view-filter-operand-for-date-time.command';
import { MigrateEmailFieldsToEmailsCommand } from 'src/database/commands/upgrade-version/0-30/0-30-migrate-email-fields-to-emails.command';
import { MigratePhoneFieldsToPhonesCommand } from 'src/database/commands/upgrade-version/0-30/0-30-migrate-phone-fields-to-phones.command';
import { SetStaleMessageSyncBackToPendingCommand } from 'src/database/commands/upgrade-version/0-30/0-30-set-stale-message-sync-back-to-pending';
Expand All @@ -28,6 +29,7 @@ export class UpgradeTo0_30Command extends ActiveWorkspacesCommandRunner {
private readonly setStaleMessageSyncBackToPendingCommand: SetStaleMessageSyncBackToPendingCommand,
private readonly fixEmailFieldsToEmailsCommand: FixEmailFieldsToEmailsCommand,
private readonly migratePhoneFieldsToPhones: MigratePhoneFieldsToPhonesCommand,
private readonly fixViewFilterOperandForDateTimeCommand: FixViewFilterOperandForDateTimeCommand,
) {
super(workspaceRepository);
}
Expand Down Expand Up @@ -65,5 +67,10 @@ export class UpgradeTo0_30Command extends ActiveWorkspacesCommandRunner {
options,
workspaceIds,
);
await this.fixViewFilterOperandForDateTimeCommand.executeActiveWorkspacesCommand(
passedParam,
options,
workspaceIds,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';

import { FixEmailFieldsToEmailsCommand } from 'src/database/commands/upgrade-version/0-30/0-30-fix-email-field-migration.command';
import { FixViewFilterOperandForDateTimeCommand } from 'src/database/commands/upgrade-version/0-30/0-30-fix-view-filter-operand-for-date-time.command';
import { MigrateEmailFieldsToEmailsCommand } from 'src/database/commands/upgrade-version/0-30/0-30-migrate-email-fields-to-emails.command';
import { MigratePhoneFieldsToPhonesCommand } from 'src/database/commands/upgrade-version/0-30/0-30-migrate-phone-fields-to-phones.command';
import { SetStaleMessageSyncBackToPendingCommand } from 'src/database/commands/upgrade-version/0-30/0-30-set-stale-message-sync-back-to-pending';
Expand Down Expand Up @@ -36,6 +37,7 @@ import { ViewModule } from 'src/modules/view/view.module';
SetStaleMessageSyncBackToPendingCommand,
FixEmailFieldsToEmailsCommand,
MigratePhoneFieldsToPhonesCommand,
FixViewFilterOperandForDateTimeCommand,
],
})
export class UpgradeTo0_30CommandModule {}

0 comments on commit ae6777f

Please sign in to comment.