-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
10f6335
commit f0383a8
Showing
9 changed files
with
209 additions
and
5 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
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
142 changes: 142 additions & 0 deletions
142
...c/database/commands/upgrade-version/0-35/0-35-phone-calling-code-create-column.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,142 @@ | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
|
||
import chalk from 'chalk'; | ||
import { Command } from 'nest-commander'; | ||
import { Repository } from 'typeorm'; | ||
import { v4 } from 'uuid'; | ||
|
||
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 { WorkspaceMetadataVersionService } from 'src/engine/metadata-modules/workspace-metadata-version/services/workspace-metadata-version.service'; | ||
import { generateMigrationName } from 'src/engine/metadata-modules/workspace-migration/utils/generate-migration-name.util'; | ||
import { | ||
WorkspaceMigrationColumnActionType, | ||
WorkspaceMigrationTableAction, | ||
WorkspaceMigrationTableActionType, | ||
} from 'src/engine/metadata-modules/workspace-migration/workspace-migration.entity'; | ||
import { WorkspaceMigrationFactory } from 'src/engine/metadata-modules/workspace-migration/workspace-migration.factory'; | ||
import { WorkspaceMigrationService } from 'src/engine/metadata-modules/workspace-migration/workspace-migration.service'; | ||
import { computeObjectTargetTable } from 'src/engine/utils/compute-object-target-table.util'; | ||
import { WorkspaceMigrationRunnerService } from 'src/engine/workspace-manager/workspace-migration-runner/workspace-migration-runner.service'; | ||
import { isDefined } from 'src/utils/is-defined'; | ||
|
||
@Command({ | ||
name: 'upgrade-0.35:phone-calling-code-create-column', | ||
description: 'Create the callingCode column', | ||
}) | ||
export class PhoneCallingCodeCreateColumnCommand extends ActiveWorkspacesCommandRunner { | ||
constructor( | ||
@InjectRepository(Workspace, 'core') | ||
protected readonly workspaceRepository: Repository<Workspace>, | ||
@InjectRepository(FieldMetadataEntity, 'metadata') | ||
private readonly fieldMetadataRepository: Repository<FieldMetadataEntity>, | ||
private readonly workspaceMigrationService: WorkspaceMigrationService, | ||
private readonly workspaceMigrationFactory: WorkspaceMigrationFactory, | ||
private readonly workspaceMigrationRunnerService: WorkspaceMigrationRunnerService, | ||
private readonly workspaceMetadataVersionService: WorkspaceMetadataVersionService, | ||
) { | ||
super(workspaceRepository); | ||
} | ||
|
||
async executeActiveWorkspacesCommand( | ||
_passedParam: string[], | ||
options: ActiveWorkspacesCommandOptions, | ||
workspaceIds: string[], | ||
): Promise<void> { | ||
this.logger.log( | ||
'Running command to add calling code and change country code with default one', | ||
); | ||
|
||
this.logger.verbose(`Part 1 - Workspace`); | ||
let workspaceIterator = 1; | ||
|
||
for (const workspaceId of workspaceIds) { | ||
this.logger.verbose( | ||
`Running command for workspace ${workspaceId} ${workspaceIterator}/${workspaceIds.length}`, | ||
); | ||
|
||
this.logger.verbose( | ||
`P1 Step 1 - let's find all the fieldsMetadata that have the PHONES type, and extract the objectMetadataId`, | ||
); | ||
|
||
try { | ||
const phonesFieldMetadata = await this.fieldMetadataRepository.find({ | ||
where: { | ||
workspaceId, | ||
type: FieldMetadataType.PHONES, | ||
}, | ||
relations: ['object'], | ||
}); | ||
|
||
for (const phoneFieldMetadata of phonesFieldMetadata) { | ||
if ( | ||
isDefined(phoneFieldMetadata?.name) && | ||
isDefined(phoneFieldMetadata.object) | ||
) { | ||
this.logger.verbose( | ||
`P1 Step 1 - Let's find the "nameSingular" of this objectMetadata: ${phoneFieldMetadata.object.nameSingular || 'not found'}`, | ||
); | ||
|
||
if (!phoneFieldMetadata.object?.nameSingular) continue; | ||
|
||
this.logger.verbose( | ||
`P1 Step 1 - Create migration for field ${phoneFieldMetadata.name}`, | ||
); | ||
|
||
if (options.dryRun) { | ||
continue; | ||
} | ||
await this.workspaceMigrationService.createCustomMigration( | ||
generateMigrationName( | ||
`create-${phoneFieldMetadata.object.nameSingular}PrimaryPhoneCallingCode-for-field-${phoneFieldMetadata.name}`, | ||
), | ||
workspaceId, | ||
[ | ||
{ | ||
name: computeObjectTargetTable(phoneFieldMetadata.object), | ||
action: WorkspaceMigrationTableActionType.ALTER, | ||
columns: this.workspaceMigrationFactory.createColumnActions( | ||
WorkspaceMigrationColumnActionType.CREATE, | ||
{ | ||
id: v4(), | ||
type: FieldMetadataType.TEXT, | ||
name: `${phoneFieldMetadata.name}PrimaryPhoneCallingCode`, | ||
label: `${phoneFieldMetadata.name}PrimaryPhoneCallingCode`, | ||
objectMetadataId: phoneFieldMetadata.object.id, | ||
workspaceId: workspaceId, | ||
isNullable: true, | ||
defaultValue: "''", | ||
} satisfies Partial<FieldMetadataEntity>, | ||
), | ||
} satisfies WorkspaceMigrationTableAction, | ||
], | ||
); | ||
} | ||
} | ||
|
||
this.logger.verbose( | ||
`P1 Step 1 - RUN migration to create callingCodes for ${workspaceId.slice(0, 5)}`, | ||
); | ||
await this.workspaceMigrationRunnerService.executeMigrationFromPendingMigrations( | ||
workspaceId, | ||
); | ||
|
||
await this.workspaceMetadataVersionService.incrementMetadataVersion( | ||
workspaceId, | ||
); | ||
} catch (error) { | ||
this.logger.log(`Error in workspace ${workspaceId} : ${error}`); | ||
} | ||
workspaceIterator++; | ||
} | ||
|
||
this.logger.log(chalk.green(`Command completed!`)); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
.../twenty-server/src/database/commands/upgrade-version/0-35/0-35-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,38 @@ | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
|
||
import { Command } from 'nest-commander'; | ||
import { Repository } from 'typeorm'; | ||
|
||
import { ActiveWorkspacesCommandRunner } from 'src/database/commands/active-workspaces.command'; | ||
import { PhoneCallingCodeCreateColumnCommand } from 'src/database/commands/upgrade-version/0-35/0-35-phone-calling-code-create-column.command'; | ||
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity'; | ||
|
||
interface UpdateTo0_35CommandOptions { | ||
workspaceId?: string; | ||
} | ||
|
||
@Command({ | ||
name: 'upgrade-0.35', | ||
description: 'Upgrade to 0.35', | ||
}) | ||
export class UpgradeTo0_35Command extends ActiveWorkspacesCommandRunner { | ||
constructor( | ||
@InjectRepository(Workspace, 'core') | ||
protected readonly workspaceRepository: Repository<Workspace>, | ||
private readonly phoneCallingCodeCreateColumnCommand: PhoneCallingCodeCreateColumnCommand, | ||
) { | ||
super(workspaceRepository); | ||
} | ||
|
||
async executeActiveWorkspacesCommand( | ||
passedParam: string[], | ||
options: UpdateTo0_35CommandOptions, | ||
workspaceIds: string[], | ||
): Promise<void> { | ||
await this.phoneCallingCodeCreateColumnCommand.executeActiveWorkspacesCommand( | ||
passedParam, | ||
options, | ||
workspaceIds, | ||
); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...s/twenty-server/src/database/commands/upgrade-version/0-35/0-35-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,22 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
|
||
import { PhoneCallingCodeCreateColumnCommand } from 'src/database/commands/upgrade-version/0-35/0-35-phone-calling-code-create-column.command'; | ||
import { UpgradeTo0_35Command } from 'src/database/commands/upgrade-version/0-35/0-35-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 { WorkspaceMetadataVersionModule } from 'src/engine/metadata-modules/workspace-metadata-version/workspace-metadata-version.module'; | ||
import { WorkspaceMigrationModule } from 'src/engine/metadata-modules/workspace-migration/workspace-migration.module'; | ||
import { WorkspaceMigrationRunnerModule } from 'src/engine/workspace-manager/workspace-migration-runner/workspace-migration-runner.module'; | ||
|
||
@Module({ | ||
imports: [ | ||
TypeOrmModule.forFeature([Workspace], 'core'), | ||
TypeOrmModule.forFeature([FieldMetadataEntity], 'metadata'), | ||
WorkspaceMigrationRunnerModule, | ||
WorkspaceMetadataVersionModule, | ||
WorkspaceMigrationModule, | ||
], | ||
providers: [UpgradeTo0_35Command, PhoneCallingCodeCreateColumnCommand], | ||
}) | ||
export class UpgradeTo0_35CommandModule {} |
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