Add simplify-search-vector-expression to upgrade 0-32 command (#7925)
## Context Moving this command to the global command runner for 0.32 upgrade. This should fix searchVector expression introduced in 0.31 to later handle soft-deleted + search filter properly. ## Test ``` git co v0.31.0 -- reset DB git co c--add-simplify-search-vector-expression-to-upgrade-0-32 -- migrate typeorm -- upgrade 0.32 command ``` search is working and expression is correctly set. Soft deleted records are not shown as before but it's now possible to override that behavior via filters. cc @ijreilly
This commit is contained in:
@ -0,0 +1,115 @@
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
|
||||
import chalk from 'chalk';
|
||||
import { Command } from 'nest-commander';
|
||||
import { Repository } from 'typeorm';
|
||||
|
||||
import {
|
||||
ActiveWorkspacesCommandOptions,
|
||||
ActiveWorkspacesCommandRunner,
|
||||
} from 'src/database/commands/active-workspaces.command';
|
||||
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
import {
|
||||
FieldMetadataEntity,
|
||||
FieldMetadataType,
|
||||
} from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
|
||||
import { SearchService } from 'src/engine/metadata-modules/search/search.service';
|
||||
import { SEARCH_FIELDS_FOR_CUSTOM_OBJECT } from 'src/engine/twenty-orm/custom.workspace-entity';
|
||||
import { WorkspaceMigrationRunnerService } from 'src/engine/workspace-manager/workspace-migration-runner/workspace-migration-runner.service';
|
||||
import {
|
||||
COMPANY_STANDARD_FIELD_IDS,
|
||||
CUSTOM_OBJECT_STANDARD_FIELD_IDS,
|
||||
OPPORTUNITY_STANDARD_FIELD_IDS,
|
||||
PERSON_STANDARD_FIELD_IDS,
|
||||
} from 'src/engine/workspace-manager/workspace-sync-metadata/constants/standard-field-ids';
|
||||
import { FieldTypeAndNameMetadata } from 'src/engine/workspace-manager/workspace-sync-metadata/utils/get-ts-vector-column-expression.util';
|
||||
import { SEARCH_FIELDS_FOR_COMPANY } from 'src/modules/company/standard-objects/company.workspace-entity';
|
||||
import { SEARCH_FIELDS_FOR_OPPORTUNITY } from 'src/modules/opportunity/standard-objects/opportunity.workspace-entity';
|
||||
import { SEARCH_FIELDS_FOR_PERSON } from 'src/modules/person/standard-objects/person.workspace-entity';
|
||||
|
||||
@Command({
|
||||
name: 'fix-0.32:simplify-search-vector-expression',
|
||||
description: 'Replace searchVector with simpler expression',
|
||||
})
|
||||
export class SimplifySearchVectorExpressionCommand extends ActiveWorkspacesCommandRunner {
|
||||
constructor(
|
||||
@InjectRepository(Workspace, 'core')
|
||||
protected readonly workspaceRepository: Repository<Workspace>,
|
||||
@InjectRepository(FieldMetadataEntity, 'metadata')
|
||||
private readonly fieldMetadataRepository: Repository<FieldMetadataEntity>,
|
||||
private readonly searchService: SearchService,
|
||||
private readonly workspaceMigrationRunnerService: WorkspaceMigrationRunnerService,
|
||||
) {
|
||||
super(workspaceRepository);
|
||||
}
|
||||
|
||||
async executeActiveWorkspacesCommand(
|
||||
_passedParam: string[],
|
||||
_options: ActiveWorkspacesCommandOptions,
|
||||
workspaceIds: string[],
|
||||
): Promise<void> {
|
||||
this.logger.log('Running command to fix migration');
|
||||
|
||||
for (const workspaceId of workspaceIds) {
|
||||
this.logger.log(`Running command for workspace ${workspaceId}`);
|
||||
|
||||
try {
|
||||
const searchVectorFields = await this.fieldMetadataRepository.findBy({
|
||||
workspaceId: workspaceId,
|
||||
type: FieldMetadataType.TS_VECTOR,
|
||||
});
|
||||
|
||||
for (const searchVectorField of searchVectorFields) {
|
||||
let fieldsUsedForSearch: FieldTypeAndNameMetadata[] = [];
|
||||
|
||||
switch (searchVectorField.standardId) {
|
||||
case CUSTOM_OBJECT_STANDARD_FIELD_IDS.searchVector: {
|
||||
fieldsUsedForSearch = SEARCH_FIELDS_FOR_CUSTOM_OBJECT;
|
||||
break;
|
||||
}
|
||||
case PERSON_STANDARD_FIELD_IDS.searchVector: {
|
||||
fieldsUsedForSearch = SEARCH_FIELDS_FOR_PERSON;
|
||||
break;
|
||||
}
|
||||
case COMPANY_STANDARD_FIELD_IDS.searchVector: {
|
||||
fieldsUsedForSearch = SEARCH_FIELDS_FOR_COMPANY;
|
||||
break;
|
||||
}
|
||||
case OPPORTUNITY_STANDARD_FIELD_IDS.searchVector: {
|
||||
fieldsUsedForSearch = SEARCH_FIELDS_FOR_OPPORTUNITY;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
throw new Error(
|
||||
`search vector has unexpected standardId: ${searchVectorField.standardId}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
await this.searchService.updateSearchVector(
|
||||
searchVectorField.objectMetadataId,
|
||||
fieldsUsedForSearch,
|
||||
workspaceId,
|
||||
);
|
||||
|
||||
await this.workspaceMigrationRunnerService.executeMigrationFromPendingMigrations(
|
||||
workspaceId,
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
this.logger.log(
|
||||
chalk.red(
|
||||
`Running command on workspace ${workspaceId} failed with error: ${error}`,
|
||||
),
|
||||
);
|
||||
continue;
|
||||
} finally {
|
||||
this.logger.log(
|
||||
chalk.green(`Finished running command for workspace ${workspaceId}.`),
|
||||
);
|
||||
}
|
||||
|
||||
this.logger.log(chalk.green(`Command completed!`));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -4,6 +4,7 @@ import { Command } from 'nest-commander';
|
||||
import { Repository } from 'typeorm';
|
||||
|
||||
import { ActiveWorkspacesCommandRunner } from 'src/database/commands/active-workspaces.command';
|
||||
import { SimplifySearchVectorExpressionCommand } from 'src/database/commands/upgrade-version/0-32/0-32-simplify-search-vector-expression';
|
||||
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
import { SyncWorkspaceMetadataCommand } from 'src/engine/workspace-manager/workspace-sync-metadata/commands/sync-workspace-metadata.command';
|
||||
|
||||
@ -23,6 +24,7 @@ export class UpgradeTo0_32Command extends ActiveWorkspacesCommandRunner {
|
||||
protected readonly workspaceRepository: Repository<Workspace>,
|
||||
private readonly syncWorkspaceMetadataCommand: SyncWorkspaceMetadataCommand,
|
||||
private readonly enforceUniqueConstraintsCommand: EnforceUniqueConstraintsCommand,
|
||||
private readonly simplifySearchVectorExpressionCommand: SimplifySearchVectorExpressionCommand,
|
||||
) {
|
||||
super(workspaceRepository);
|
||||
}
|
||||
@ -41,6 +43,12 @@ export class UpgradeTo0_32Command extends ActiveWorkspacesCommandRunner {
|
||||
workspaceIds,
|
||||
);
|
||||
|
||||
await this.simplifySearchVectorExpressionCommand.executeActiveWorkspacesCommand(
|
||||
passedParam,
|
||||
options,
|
||||
workspaceIds,
|
||||
);
|
||||
|
||||
await this.enforceUniqueConstraintsCommand.executeActiveWorkspacesCommand(
|
||||
passedParam,
|
||||
options,
|
||||
|
||||
@ -2,17 +2,30 @@ import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
|
||||
import { EnforceUniqueConstraintsCommand } from 'src/database/commands/upgrade-version/0-32/0-32-enforce-unique-constraints.command';
|
||||
import { SimplifySearchVectorExpressionCommand } from 'src/database/commands/upgrade-version/0-32/0-32-simplify-search-vector-expression';
|
||||
import { UpgradeTo0_32Command } from 'src/database/commands/upgrade-version/0-32/0-32-upgrade-version.command';
|
||||
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
import { FieldMetadataEntity } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
|
||||
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
|
||||
import { SearchModule } from 'src/engine/metadata-modules/search/search.module';
|
||||
import { WorkspaceMigrationRunnerModule } from 'src/engine/workspace-manager/workspace-migration-runner/workspace-migration-runner.module';
|
||||
import { WorkspaceSyncMetadataCommandsModule } from 'src/engine/workspace-manager/workspace-sync-metadata/commands/workspace-sync-metadata-commands.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
TypeOrmModule.forFeature([Workspace], 'core'),
|
||||
TypeOrmModule.forFeature([ObjectMetadataEntity], 'metadata'),
|
||||
TypeOrmModule.forFeature(
|
||||
[ObjectMetadataEntity, FieldMetadataEntity],
|
||||
'metadata',
|
||||
),
|
||||
WorkspaceSyncMetadataCommandsModule,
|
||||
SearchModule,
|
||||
WorkspaceMigrationRunnerModule,
|
||||
],
|
||||
providers: [
|
||||
UpgradeTo0_32Command,
|
||||
EnforceUniqueConstraintsCommand,
|
||||
SimplifySearchVectorExpressionCommand,
|
||||
],
|
||||
providers: [UpgradeTo0_32Command, EnforceUniqueConstraintsCommand],
|
||||
})
|
||||
export class UpgradeTo0_32CommandModule {}
|
||||
|
||||
Reference in New Issue
Block a user