-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add set custom object is soft deletable command #6788
Merged
charlesBochet
merged 8 commits into
main
from
c--add-set-custom-object-soft-deletable-command
Aug 31, 2024
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9332149
Add set custom object is soft deletable command
Weiko 316814e
add command to upgrade-0.24
Weiko 7bf8725
fix
Weiko 8f20dcf
add missing deletedAt fields
Weiko 1213434
update
Weiko 4e83bb8
revert
Weiko 1ec15cf
fix naming
Weiko ff5e706
Merge branch 'main' into c--add-set-custom-object-soft-deletable-command
charlesBochet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
...atabase/commands/upgrade-version/0-24/0-24-set-custom-object-is-soft-deletable.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,58 @@ | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
|
||
import { Command } from 'nest-commander'; | ||
import { In, Repository } from 'typeorm'; | ||
|
||
import { ActiveWorkspacesCommandRunner } from 'src/database/commands/upgrade-version/active-workspaces.command'; | ||
import { BaseCommandOptions } from 'src/database/commands/upgrade-version/base.command'; | ||
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity'; | ||
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity'; | ||
|
||
interface SetCustomObjectIsSoftDeletableCommandOptions | ||
extends BaseCommandOptions {} | ||
|
||
@Command({ | ||
name: 'upgrade-0-24:set-custom-object-is-soft-deletable', | ||
description: 'Set custom object is soft deletable', | ||
}) | ||
export class SetCustomObjectIsSoftDeletableCommand extends ActiveWorkspacesCommandRunner { | ||
constructor( | ||
@InjectRepository(Workspace, 'core') | ||
protected readonly workspaceRepository: Repository<Workspace>, | ||
@InjectRepository(ObjectMetadataEntity, 'metadata') | ||
private readonly objectMetadataRepository: Repository<ObjectMetadataEntity>, | ||
) { | ||
super(workspaceRepository); | ||
} | ||
|
||
async executeActiveWorkspacesCommand( | ||
_passedParam: string[], | ||
options: SetCustomObjectIsSoftDeletableCommandOptions, | ||
workspaceIds: string[], | ||
): Promise<void> { | ||
const updateCriteria = { | ||
workspaceId: In(workspaceIds), | ||
isCustom: true, | ||
isSoftDeletable: false, | ||
}; | ||
|
||
if (options.dryRun) { | ||
const entitiesToUpdate = await this.objectMetadataRepository.find({ | ||
select: ['id'], | ||
where: updateCriteria, | ||
}); | ||
|
||
this.logger.log( | ||
`Dry run: ${entitiesToUpdate.length} entities would be updated`, | ||
); | ||
|
||
return; | ||
} | ||
|
||
const result = await this.objectMetadataRepository.update(updateCriteria, { | ||
isSoftDeletable: true, | ||
}); | ||
|
||
this.logger.log(`Updated ${result.affected} entities`); | ||
} | ||
} |
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
96 changes: 96 additions & 0 deletions
96
packages/twenty-server/src/database/commands/upgrade-version/active-workspaces.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,96 @@ | ||
import { Logger } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
|
||
import chalk from 'chalk'; | ||
import { Option } from 'nest-commander'; | ||
import { Repository } from 'typeorm'; | ||
|
||
import { | ||
BaseCommandOptions, | ||
BaseCommandRunner, | ||
} from 'src/database/commands/upgrade-version/base.command'; | ||
import { | ||
Workspace, | ||
WorkspaceActivationStatus, | ||
} from 'src/engine/core-modules/workspace/workspace.entity'; | ||
|
||
export type ActiveWorkspacesCommandOptions = BaseCommandOptions & { | ||
workspaceId?: string; | ||
}; | ||
|
||
export abstract class ActiveWorkspacesCommandRunner extends BaseCommandRunner { | ||
private workspaceIds: string[] = []; | ||
|
||
protected readonly logger: Logger; | ||
|
||
constructor( | ||
@InjectRepository(Workspace, 'core') | ||
protected readonly workspaceRepository: Repository<Workspace>, | ||
) { | ||
super(); | ||
this.logger = new Logger(this.constructor.name); | ||
} | ||
|
||
@Option({ | ||
flags: '-w, --workspace-id [workspace_id]', | ||
description: | ||
'workspace id. Command runs on all active workspaces if not provided', | ||
required: false, | ||
}) | ||
parseWorkspaceId(val: string): string[] { | ||
this.workspaceIds.push(val); | ||
|
||
return this.workspaceIds; | ||
} | ||
|
||
protected async fetchActiveWorkspaceIds(): Promise<string[]> { | ||
const activeWorkspaces = await this.workspaceRepository.find({ | ||
select: ['id'], | ||
where: { | ||
activationStatus: WorkspaceActivationStatus.ACTIVE, | ||
}, | ||
}); | ||
|
||
return activeWorkspaces.map((workspace) => workspace.id); | ||
} | ||
|
||
protected logWorkspaceCount(activeWorkspaceIds: string[]): void { | ||
if (!activeWorkspaceIds.length) { | ||
this.logger.log(chalk.yellow('No workspace found')); | ||
} else { | ||
this.logger.log( | ||
chalk.green( | ||
`Running command on ${activeWorkspaceIds.length} workspaces`, | ||
), | ||
); | ||
} | ||
} | ||
|
||
override async executeBaseCommand( | ||
passedParams: string[], | ||
options: BaseCommandOptions, | ||
): Promise<void> { | ||
const activeWorkspaceIds = | ||
this.workspaceIds.length > 0 | ||
? this.workspaceIds | ||
: await this.fetchActiveWorkspaceIds(); | ||
|
||
this.logWorkspaceCount(activeWorkspaceIds); | ||
|
||
if (options.dryRun) { | ||
this.logger.log(chalk.yellow('Dry run mode: No changes will be applied')); | ||
} | ||
|
||
await this.executeActiveWorkspacesCommand( | ||
passedParams, | ||
options, | ||
activeWorkspaceIds, | ||
); | ||
} | ||
|
||
protected abstract executeActiveWorkspacesCommand( | ||
passedParams: string[], | ||
options: BaseCommandOptions, | ||
activeWorkspaceIds: string[], | ||
): Promise<void>; | ||
} |
46 changes: 46 additions & 0 deletions
46
packages/twenty-server/src/database/commands/upgrade-version/base.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,46 @@ | ||
import { Logger } from '@nestjs/common'; | ||
|
||
import chalk from 'chalk'; | ||
import { CommandRunner, Option } from 'nest-commander'; | ||
|
||
export type BaseCommandOptions = { | ||
workspaceId?: string; | ||
dryRun?: boolean; | ||
}; | ||
|
||
export abstract class BaseCommandRunner extends CommandRunner { | ||
protected readonly logger: Logger; | ||
|
||
constructor() { | ||
super(); | ||
this.logger = new Logger(this.constructor.name); | ||
} | ||
|
||
@Option({ | ||
flags: '-d, --dry-run', | ||
description: 'Simulate the command without making actual changes', | ||
required: false, | ||
}) | ||
parseDryRun(): boolean { | ||
return true; | ||
} | ||
Weiko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
override async run( | ||
passedParams: string[], | ||
options: BaseCommandOptions, | ||
): Promise<void> { | ||
try { | ||
await this.executeBaseCommand(passedParams, options); | ||
} catch (error) { | ||
this.logger.log(chalk.red(`Command failed`)); | ||
Weiko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
throw error; | ||
} finally { | ||
this.logger.log(chalk.blue('Command completed!')); | ||
} | ||
} | ||
|
||
protected abstract executeBaseCommand( | ||
passedParams: string[], | ||
options: BaseCommandOptions, | ||
): Promise<void>; | ||
} |
2 changes: 1 addition & 1 deletion
2
packages/twenty-server/src/database/typeorm/metadata/metadata.datasource.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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
love it!