-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement standardId for sync command
- Loading branch information
Showing
48 changed files
with
866 additions
and
22 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
...ges/twenty-server/src/database/typeorm/metadata/migrations/1709894431938-addStandardId.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,23 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class AddStandardId1709894431938 implements MigrationInterface { | ||
name = 'AddStandardId1709894431938'; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`ALTER TABLE "metadata"."objectMetadata" ADD "standardId" uuid`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "metadata"."fieldMetadata" ADD "standardId" uuid`, | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`ALTER TABLE "metadata"."fieldMetadata" DROP COLUMN "standardId"`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "metadata"."objectMetadata" DROP COLUMN "standardId"`, | ||
); | ||
} | ||
} |
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
157 changes: 157 additions & 0 deletions
157
...s/twenty-server/src/workspace/workspace-sync-metadata/commands/add-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,157 @@ | ||
import { Logger } from '@nestjs/common'; | ||
import { InjectDataSource } from '@nestjs/typeorm'; | ||
|
||
import { Command, CommandRunner } from 'nest-commander'; | ||
import { DataSource } from 'typeorm'; | ||
|
||
import { ObjectMetadataEntity } from 'src/metadata/object-metadata/object-metadata.entity'; | ||
import { FieldMetadataEntity } from 'src/metadata/field-metadata/field-metadata.entity'; | ||
import { standardObjectMetadataDefinitions } from 'src/workspace/workspace-sync-metadata/standard-objects'; | ||
import { StandardObjectFactory } from 'src/workspace/workspace-sync-metadata/factories/standard-object.factory'; | ||
import { computeStandardObject } from 'src/workspace/workspace-sync-metadata/utils/compute-standard-object.util'; | ||
import { StandardFieldFactory } from 'src/workspace/workspace-sync-metadata/factories/standard-field.factory'; | ||
import { CustomObjectMetadata } from 'src/workspace/workspace-sync-metadata/custom-objects/custom.object-metadata'; | ||
|
||
@Command({ | ||
name: 'workspace:add-standard-id', | ||
description: 'Add standard id to all metadata objects and fields', | ||
}) | ||
export class AddStandardIdCommand extends CommandRunner { | ||
private readonly logger = new Logger(AddStandardIdCommand.name); | ||
|
||
constructor( | ||
@InjectDataSource('metadata') | ||
private readonly metadataDataSource: DataSource, | ||
private readonly standardObjectFactory: StandardObjectFactory, | ||
private readonly standardFieldFactory: StandardFieldFactory, | ||
) { | ||
super(); | ||
} | ||
|
||
async run(): Promise<void> { | ||
const queryRunner = this.metadataDataSource.createQueryRunner(); | ||
|
||
await queryRunner.connect(); | ||
await queryRunner.startTransaction(); | ||
|
||
const manager = queryRunner.manager; | ||
|
||
this.logger.log('Adding standardId to metadata objects and fields'); | ||
|
||
try { | ||
const standardObjectMetadataCollection = | ||
this.standardObjectFactory.create( | ||
standardObjectMetadataDefinitions, | ||
{ | ||
// We don't need to provide the workspace id and data source id as we're only adding standardId | ||
workspaceId: '', | ||
dataSourceId: '', | ||
}, | ||
{ | ||
IS_BLOCKLIST_ENABLED: true, | ||
IS_CALENDAR_ENABLED: true, | ||
IS_SELF_BILLING_ENABLED: true, | ||
}, | ||
); | ||
const standardFieldMetadataCollection = this.standardFieldFactory.create( | ||
CustomObjectMetadata, | ||
{ | ||
workspaceId: '', | ||
dataSourceId: '', | ||
}, | ||
{ | ||
IS_BLOCKLIST_ENABLED: true, | ||
IS_CALENDAR_ENABLED: true, | ||
IS_SELF_BILLING_ENABLED: true, | ||
}, | ||
); | ||
|
||
const objectMetadataRepository = | ||
manager.getRepository(ObjectMetadataEntity); | ||
const fieldMetadataRepository = | ||
manager.getRepository(FieldMetadataEntity); | ||
|
||
/** | ||
* Update all object metadata with standard id | ||
*/ | ||
const updateObjectMetadataCollection: Partial<ObjectMetadataEntity>[] = | ||
[]; | ||
const updateFieldMetadataCollection: Partial<FieldMetadataEntity>[] = []; | ||
const originalObjectMetadataCollection = | ||
await objectMetadataRepository.find({ | ||
where: { | ||
fields: { isCustom: false }, | ||
}, | ||
relations: ['fields'], | ||
}); | ||
const customObjectMetadataCollection = | ||
originalObjectMetadataCollection.filter( | ||
(metadata) => metadata.isCustom, | ||
); | ||
const standardObjectMetadataMap = new Map( | ||
standardObjectMetadataCollection.map((metadata) => [ | ||
metadata.nameSingular, | ||
metadata, | ||
]), | ||
); | ||
|
||
for (const originalObjectMetadata of originalObjectMetadataCollection) { | ||
const standardObjectMetadata = standardObjectMetadataMap.get( | ||
originalObjectMetadata.nameSingular, | ||
); | ||
|
||
if (!standardObjectMetadata && !originalObjectMetadata.isCustom) { | ||
continue; | ||
} | ||
|
||
const computedStandardObjectMetadata = computeStandardObject( | ||
!standardObjectMetadata | ||
? { | ||
...originalObjectMetadata, | ||
fields: standardFieldMetadataCollection, | ||
} | ||
: standardObjectMetadata, | ||
originalObjectMetadata, | ||
customObjectMetadataCollection, | ||
); | ||
|
||
if ( | ||
!originalObjectMetadata.isCustom && | ||
!originalObjectMetadata.standardId | ||
) { | ||
updateObjectMetadataCollection.push({ | ||
id: originalObjectMetadata.id, | ||
standardId: computedStandardObjectMetadata.standardId, | ||
}); | ||
} | ||
|
||
for (const fieldMetadata of originalObjectMetadata.fields) { | ||
const standardFieldMetadata = | ||
computedStandardObjectMetadata.fields.find( | ||
(field) => field.name === fieldMetadata.name, | ||
); | ||
|
||
if (!standardFieldMetadata || standardFieldMetadata.isCustom) { | ||
continue; | ||
} | ||
|
||
updateFieldMetadataCollection.push({ | ||
id: fieldMetadata.id, | ||
standardId: standardFieldMetadata.standardId, | ||
}); | ||
} | ||
} | ||
|
||
await objectMetadataRepository.save(updateObjectMetadataCollection); | ||
|
||
await fieldMetadataRepository.save(updateFieldMetadataCollection); | ||
|
||
await queryRunner.commitTransaction(); | ||
} catch (error) { | ||
await queryRunner.rollbackTransaction(); | ||
this.logger.error('Error adding standard id to metadata', error); | ||
} finally { | ||
await queryRunner.release(); | ||
} | ||
} | ||
} |
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.