forked from twentyhq/twenty
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix wrong standard id for objectMetadataId in auditLog (twentyhq#6180)
Add a new command to delete objectMetadataId fieldMetadata that have a wrong standard-id. This is because we have fixed the missing objectMetadataId column but one already exists in the fieldMetadataId with the wrong table. We should run this command before run the sync-metadata. Introduced a new module and command to run all the command associated with the upgrade to 0.22. Not exactly sure with this structure but ideally we would like to have only 1 command for version upgrades so this is a first step.
- Loading branch information
Showing
5 changed files
with
190 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
...database/commands/upgrade-version/0-22/0-22-fix-object-metadata-id-standard-id.command.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { Logger } from '@nestjs/common'; | ||
import { InjectDataSource, InjectRepository } from '@nestjs/typeorm'; | ||
|
||
import chalk from 'chalk'; | ||
import { Command, CommandRunner, Option } from 'nest-commander'; | ||
import { DataSource, Repository } from 'typeorm'; | ||
|
||
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity'; | ||
import { FieldMetadataEntity } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity'; | ||
import { WorkspaceCacheVersionService } from 'src/engine/metadata-modules/workspace-cache-version/workspace-cache-version.service'; | ||
import { AUDIT_LOGS_STANDARD_FIELD_IDS } from 'src/engine/workspace-manager/workspace-sync-metadata/constants/standard-field-ids'; | ||
|
||
interface FixObjectMetadataIdStandardIdCommandOptions { | ||
workspaceId?: string; | ||
} | ||
|
||
@Command({ | ||
name: 'upgrade-0.22:fix-object-metadata-id-standard-id', | ||
description: 'Fix object metadata id standard id', | ||
}) | ||
export class FixObjectMetadataIdStandardIdCommand extends CommandRunner { | ||
private readonly logger = new Logger( | ||
FixObjectMetadataIdStandardIdCommand.name, | ||
); | ||
constructor( | ||
@InjectRepository(Workspace, 'core') | ||
private readonly workspaceRepository: Repository<Workspace>, | ||
private readonly workspaceCacheVersionService: WorkspaceCacheVersionService, | ||
@InjectDataSource('metadata') | ||
private readonly metadataDataSource: DataSource, | ||
) { | ||
super(); | ||
} | ||
|
||
@Option({ | ||
flags: '-w, --workspace-id [workspace_id]', | ||
description: 'workspace id. Command runs on all workspaces if not provided', | ||
required: false, | ||
}) | ||
parseWorkspaceId(value: string): string { | ||
return value; | ||
} | ||
|
||
async run( | ||
_passedParam: string[], | ||
options: FixObjectMetadataIdStandardIdCommandOptions, | ||
): Promise<void> { | ||
const workspaceIds = options.workspaceId | ||
? [options.workspaceId] | ||
: (await this.workspaceRepository.find()).map( | ||
(workspace) => workspace.id, | ||
); | ||
|
||
if (!workspaceIds.length) { | ||
this.logger.log(chalk.yellow('No workspace found')); | ||
|
||
return; | ||
} | ||
|
||
this.logger.log( | ||
chalk.green(`Running command on ${workspaceIds.length} workspaces`), | ||
); | ||
|
||
const metadataQueryRunner = this.metadataDataSource.createQueryRunner(); | ||
|
||
await metadataQueryRunner.connect(); | ||
|
||
const fieldMetadataRepository = | ||
metadataQueryRunner.manager.getRepository(FieldMetadataEntity); | ||
|
||
for (const workspaceId of workspaceIds) { | ||
try { | ||
await metadataQueryRunner.startTransaction(); | ||
|
||
await fieldMetadataRepository.delete({ | ||
workspaceId, | ||
standardId: AUDIT_LOGS_STANDARD_FIELD_IDS.objectName, | ||
name: 'objectMetadataId', | ||
}); | ||
|
||
await metadataQueryRunner.commitTransaction(); | ||
} catch (error) { | ||
await metadataQueryRunner.rollbackTransaction(); | ||
this.logger.log( | ||
chalk.red(`Running command on workspace ${workspaceId} failed`), | ||
); | ||
throw error; | ||
} | ||
|
||
await this.workspaceCacheVersionService.incrementVersion(workspaceId); | ||
|
||
this.logger.log( | ||
chalk.green(`Running command on workspace ${workspaceId} done`), | ||
); | ||
} | ||
|
||
await metadataQueryRunner.release(); | ||
|
||
this.logger.log(chalk.green(`Command completed!`)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
.../twenty-server/src/database/commands/upgrade-version/0-22/0-22-upgrade-version.command.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Command, CommandRunner, Option } from 'nest-commander'; | ||
|
||
import { FixObjectMetadataIdStandardIdCommand } from 'src/database/commands/upgrade-version/0-22/0-22-fix-object-metadata-id-standard-id.command'; | ||
import { UpdateBooleanFieldsNullDefaultValuesAndNullValuesCommand } from 'src/database/commands/upgrade-version/0-22/0-22-update-boolean-fields-null-default-values-and-null-values.command'; | ||
|
||
interface UpdateBooleanFieldsNullDefaultValuesAndNullValuesCommandOptions { | ||
workspaceId?: string; | ||
} | ||
|
||
@Command({ | ||
name: 'upgrade-0.22', | ||
description: 'Upgrade to 0.22', | ||
}) | ||
export class UpgradeTo0_22Command extends CommandRunner { | ||
constructor( | ||
private readonly fixObjectMetadataIdStandardIdCommand: FixObjectMetadataIdStandardIdCommand, | ||
private readonly updateBooleanFieldsNullDefaultValuesAndNullValuesCommand: UpdateBooleanFieldsNullDefaultValuesAndNullValuesCommand, | ||
) { | ||
super(); | ||
} | ||
|
||
@Option({ | ||
flags: '-w, --workspace-id [workspace_id]', | ||
description: 'workspace id. Command runs on all workspaces if not provided', | ||
required: false, | ||
}) | ||
parseWorkspaceId(value: string): string { | ||
return value; | ||
} | ||
|
||
async run( | ||
_passedParam: string[], | ||
options: UpdateBooleanFieldsNullDefaultValuesAndNullValuesCommandOptions, | ||
): Promise<void> { | ||
await this.fixObjectMetadataIdStandardIdCommand.run(_passedParam, options); | ||
await this.updateBooleanFieldsNullDefaultValuesAndNullValuesCommand.run( | ||
_passedParam, | ||
options, | ||
); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...s/twenty-server/src/database/commands/upgrade-version/0-22/0-22-upgrade-version.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
|
||
import { FixObjectMetadataIdStandardIdCommand } from 'src/database/commands/upgrade-version/0-22/0-22-fix-object-metadata-id-standard-id.command'; | ||
import { UpdateBooleanFieldsNullDefaultValuesAndNullValuesCommand } from 'src/database/commands/upgrade-version/0-22/0-22-update-boolean-fields-null-default-values-and-null-values.command'; | ||
import { UpgradeTo0_22Command } from 'src/database/commands/upgrade-version/0-22/0-22-upgrade-version.command'; | ||
import { TypeORMModule } from 'src/database/typeorm/typeorm.module'; | ||
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity'; | ||
import { DataSourceModule } from 'src/engine/metadata-modules/data-source/data-source.module'; | ||
import { WorkspaceCacheVersionModule } from 'src/engine/metadata-modules/workspace-cache-version/workspace-cache-version.module'; | ||
|
||
@Module({ | ||
imports: [ | ||
TypeOrmModule.forFeature([Workspace], 'core'), | ||
WorkspaceCacheVersionModule, | ||
TypeORMModule, | ||
DataSourceModule, | ||
], | ||
providers: [ | ||
FixObjectMetadataIdStandardIdCommand, | ||
UpdateBooleanFieldsNullDefaultValuesAndNullValuesCommand, | ||
UpgradeTo0_22Command, | ||
], | ||
}) | ||
export class UpgradeTo0_22CommandModule {} |