[Requires "warm" cache flush (no immediate downtime before flush)] Migrate viewGroup.fieldMetadataId -> view.mainGroupByFieldMetadataId (1/3) (#16206)
In this PR (1/3) - introduce view.mainGroupByFieldMetadataId as the new reference determining which fieldMetadataId is used in a grouped view, in order to deprecate viewGroup.fieldMetadataId which creates inconsistencies. view.mainGroupByFieldMetadataId is now filled at every view creation, though not in use yet. - Introduce a command to backfill view.mainGroupByFieldMetadataId for existing views + delete all viewGroup.fieldMetadataId with a fieldMetadataId that is not view.mainGroupByFieldMetadataId. (It should concern 37 active workspaces) - Temporarily disable the option to change a grouped view's fieldMetadataId as for now it creates inconsistencies. This feature can be reintroduced when we have done the full migration. In a next PR - (2/3) use view.mainGroupByFieldMetadataId instead of viewGroup.fieldMetadataId. In FE we may keep viewGroup.fieldMetadataId as a state (TBD). View groups will now be created / deleted as a side effect of view's mainGroupByFieldMetadataId update. - (3/3) remove viewGroup.fieldMetadataId --------- Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
+5
-2
@@ -1,7 +1,7 @@
|
||||
import {
|
||||
FieldMetadataType,
|
||||
FieldMetadataSettings,
|
||||
FieldMetadataOptions,
|
||||
FieldMetadataSettings,
|
||||
FieldMetadataType,
|
||||
} from 'twenty-shared/types';
|
||||
import {
|
||||
Check,
|
||||
@@ -212,4 +212,7 @@ export class FieldMetadataEntity<
|
||||
|
||||
@OneToMany(() => ViewEntity, (view) => view.calendarFieldMetadata)
|
||||
calendarViews: Relation<ViewEntity[]>;
|
||||
|
||||
@OneToMany(() => ViewEntity, (view) => view.mainGroupByFieldMetadata)
|
||||
mainGroupByFieldMetadataViews: Relation<ViewEntity[]>;
|
||||
}
|
||||
|
||||
+4
@@ -41,6 +41,10 @@ export const ALL_METADATA_RELATED_METADATA_BY_FOREIGN_KEY = {
|
||||
metadataName: 'fieldMetadata',
|
||||
flatEntityForeignKeyAggregator: 'calendarViewIds',
|
||||
},
|
||||
mainGroupByFieldMetadataId: {
|
||||
metadataName: 'fieldMetadata',
|
||||
flatEntityForeignKeyAggregator: 'mainGroupByFieldMetadataViewIds',
|
||||
},
|
||||
objectMetadataId: {
|
||||
metadataName: 'objectMetadata',
|
||||
flatEntityForeignKeyAggregator: 'viewIds',
|
||||
|
||||
+1
@@ -25,6 +25,7 @@ export const getFlatFieldMetadataMock = <T extends FieldMetadataType>(
|
||||
kanbanAggregateOperationViewIds: [],
|
||||
viewFieldIds: [],
|
||||
createdAt,
|
||||
mainGroupByFieldMetadataViewIds: [],
|
||||
updatedAt: createdAt,
|
||||
defaultValue: null,
|
||||
options: null,
|
||||
|
||||
+1
@@ -33,6 +33,7 @@ export const getRelationTargetFlatFieldMetadataMock = ({
|
||||
|
||||
return {
|
||||
calendarViewIds: [],
|
||||
mainGroupByFieldMetadataViewIds: [],
|
||||
viewFilterIds: [],
|
||||
viewGroupIds: [],
|
||||
viewFieldIds: [],
|
||||
|
||||
+4
@@ -120,6 +120,7 @@ exports[`fromCreateFieldInputToFlatFieldMetadatasToCreate MORPH_RELATION test su
|
||||
"isUnique": null,
|
||||
"kanbanAggregateOperationViewIds": [],
|
||||
"label": "newFieldLabel",
|
||||
"mainGroupByFieldMetadataViewIds": [],
|
||||
"morphId": Any<String>,
|
||||
"name": "newFieldPets",
|
||||
"objectMetadataId": Any<String>,
|
||||
@@ -156,6 +157,7 @@ exports[`fromCreateFieldInputToFlatFieldMetadatasToCreate MORPH_RELATION test su
|
||||
"isUnique": null,
|
||||
"kanbanAggregateOperationViewIds": [],
|
||||
"label": "Pet",
|
||||
"mainGroupByFieldMetadataViewIds": [],
|
||||
"morphId": null,
|
||||
"name": "pet",
|
||||
"objectMetadataId": Any<String>,
|
||||
@@ -194,6 +196,7 @@ exports[`fromCreateFieldInputToFlatFieldMetadatasToCreate MORPH_RELATION test su
|
||||
"isUnique": null,
|
||||
"kanbanAggregateOperationViewIds": [],
|
||||
"label": "newFieldLabel",
|
||||
"mainGroupByFieldMetadataViewIds": [],
|
||||
"morphId": Any<String>,
|
||||
"name": "newFieldCompanies",
|
||||
"objectMetadataId": Any<String>,
|
||||
@@ -230,6 +233,7 @@ exports[`fromCreateFieldInputToFlatFieldMetadatasToCreate MORPH_RELATION test su
|
||||
"isUnique": null,
|
||||
"kanbanAggregateOperationViewIds": [],
|
||||
"label": "Company",
|
||||
"mainGroupByFieldMetadataViewIds": [],
|
||||
"morphId": null,
|
||||
"name": "company",
|
||||
"objectMetadataId": Any<String>,
|
||||
|
||||
+9
@@ -68,6 +68,7 @@ export class WorkspaceFlatFieldMetadataMapCacheService extends WorkspaceCachePro
|
||||
'id',
|
||||
'kanbanAggregateOperationFieldMetadataId',
|
||||
'calendarFieldMetadataId',
|
||||
'mainGroupByFieldMetadataId',
|
||||
],
|
||||
withDeleted: true,
|
||||
}),
|
||||
@@ -79,6 +80,7 @@ export class WorkspaceFlatFieldMetadataMapCacheService extends WorkspaceCachePro
|
||||
viewGroupsByFieldId,
|
||||
calendarViewsByFieldId,
|
||||
kanbanViewsByFieldId,
|
||||
mainGroupByFieldMetadataViewsByFieldId,
|
||||
] = (
|
||||
[
|
||||
{
|
||||
@@ -101,6 +103,10 @@ export class WorkspaceFlatFieldMetadataMapCacheService extends WorkspaceCachePro
|
||||
entities: views,
|
||||
foreignKey: 'kanbanAggregateOperationFieldMetadataId',
|
||||
},
|
||||
{
|
||||
entities: views,
|
||||
foreignKey: 'mainGroupByFieldMetadataId',
|
||||
},
|
||||
] as const
|
||||
).map(regroupEntitiesByRelatedEntityId);
|
||||
|
||||
@@ -115,6 +121,9 @@ export class WorkspaceFlatFieldMetadataMapCacheService extends WorkspaceCachePro
|
||||
kanbanAggregateOperationViews:
|
||||
kanbanViewsByFieldId.get(fieldMetadataEntity.id) || [],
|
||||
calendarViews: calendarViewsByFieldId.get(fieldMetadataEntity.id) || [],
|
||||
mainGroupByFieldMetadataViews:
|
||||
mainGroupByFieldMetadataViewsByFieldId.get(fieldMetadataEntity.id) ||
|
||||
[],
|
||||
} as FieldMetadataEntity);
|
||||
|
||||
addFlatEntityToFlatEntityMapsThroughMutationOrThrow({
|
||||
|
||||
+4
-2
@@ -11,9 +11,10 @@ export const FIELD_METADATA_RELATION_PROPERTIES = [
|
||||
'viewFields',
|
||||
'application',
|
||||
'viewFilters',
|
||||
'viewGroups',
|
||||
'kanbanAggregateOperationViews',
|
||||
'calendarViews',
|
||||
'mainGroupByFieldMetadataViews',
|
||||
'viewGroups',
|
||||
] as const satisfies (keyof FieldMetadataEntity)[];
|
||||
|
||||
export type FieldMetadataEntityRelationProperties =
|
||||
@@ -24,7 +25,8 @@ export type FlatFieldMetadata<T extends FieldMetadataType = FieldMetadataType> =
|
||||
universalIdentifier: string;
|
||||
viewFieldIds: string[];
|
||||
viewFilterIds: string[];
|
||||
viewGroupIds: string[];
|
||||
kanbanAggregateOperationViewIds: string[];
|
||||
calendarViewIds: string[];
|
||||
mainGroupByFieldMetadataViewIds: string[];
|
||||
viewGroupIds: string[];
|
||||
};
|
||||
|
||||
+3
@@ -23,6 +23,9 @@ export const fromFieldMetadataEntityToFlatFieldMetadata = <
|
||||
kanbanAggregateOperationViewIds:
|
||||
fieldMetadataEntity.kanbanAggregateOperationViews.map(({ id }) => id),
|
||||
calendarViewIds: fieldMetadataEntity.calendarViews.map(({ id }) => id),
|
||||
mainGroupByFieldMetadataViewIds:
|
||||
fieldMetadataEntity.mainGroupByFieldMetadataViews?.map(({ id }) => id) ??
|
||||
[],
|
||||
viewGroupIds: fieldMetadataEntity.viewGroups.map(({ id }) => id),
|
||||
viewFieldIds: fieldMetadataEntity.viewFields.map(({ id }) => id),
|
||||
viewFilterIds: fieldMetadataEntity.viewFilters.map(({ id }) => id),
|
||||
|
||||
+1
@@ -26,6 +26,7 @@ export const getDefaultFlatFieldMetadata = ({
|
||||
|
||||
return {
|
||||
calendarViewIds: [],
|
||||
mainGroupByFieldMetadataViewIds: [],
|
||||
viewFieldIds: [],
|
||||
description: createFieldInput.description ?? null,
|
||||
id: fieldMetadataId,
|
||||
|
||||
+3
-2
@@ -1,5 +1,5 @@
|
||||
import { isDefined } from 'class-validator';
|
||||
import { type EnumFieldMetadataType } from 'twenty-shared/types';
|
||||
import { isDefined } from 'twenty-shared/utils';
|
||||
import { v4 } from 'uuid';
|
||||
|
||||
import {
|
||||
@@ -63,7 +63,8 @@ export const recomputeViewGroupsOnFlatFieldMetadataOptionsUpdate = ({
|
||||
const flatViewGroupsToUpdate = updatedFieldMetadataOptions.flatMap(
|
||||
({ from: fromOption, to: toOption }) =>
|
||||
flatViewGroups.flatMap((flatViewGroup) =>
|
||||
flatViewGroup.fieldValue === fromOption.value
|
||||
flatViewGroup.fieldValue === fromOption.value &&
|
||||
flatViewGroup.fieldMetadataId === fromFlatFieldMetadata.id
|
||||
? { ...flatViewGroup, fieldValue: toOption.value }
|
||||
: [],
|
||||
),
|
||||
|
||||
+1
@@ -13,4 +13,5 @@ export const FLAT_VIEW_EDITABLE_PROPERTIES = [
|
||||
'calendarLayout',
|
||||
'calendarFieldMetadataId',
|
||||
'visibility',
|
||||
'mainGroupByFieldMetadataId',
|
||||
] as const satisfies (keyof FlatView)[];
|
||||
|
||||
+2
@@ -44,6 +44,8 @@ export const fromCreateViewInputToFlatViewToCreate = ({
|
||||
kanbanAggregateOperation: createViewInput.kanbanAggregateOperation ?? null,
|
||||
kanbanAggregateOperationFieldMetadataId:
|
||||
createViewInput.kanbanAggregateOperationFieldMetadataId ?? null,
|
||||
mainGroupByFieldMetadataId:
|
||||
createViewInput.mainGroupByFieldMetadataId ?? null,
|
||||
key: createViewInput.key ?? null,
|
||||
openRecordIn: createViewInput.openRecordIn ?? ViewOpenRecordIn.SIDE_PANEL,
|
||||
position: createViewInput.position ?? 0,
|
||||
|
||||
+8
@@ -31,6 +31,7 @@ export const buildDefaultFlatFieldMetadatasForCustomObject = ({
|
||||
viewGroupIds: [],
|
||||
kanbanAggregateOperationViewIds: [],
|
||||
calendarViewIds: [],
|
||||
mainGroupByFieldMetadataViewIds: [],
|
||||
isLabelSyncedWithName: false,
|
||||
isUnique: true,
|
||||
objectMetadataId,
|
||||
@@ -68,6 +69,7 @@ export const buildDefaultFlatFieldMetadatasForCustomObject = ({
|
||||
viewGroupIds: [],
|
||||
kanbanAggregateOperationViewIds: [],
|
||||
calendarViewIds: [],
|
||||
mainGroupByFieldMetadataViewIds: [],
|
||||
isLabelSyncedWithName: false,
|
||||
isUnique: false,
|
||||
objectMetadataId,
|
||||
@@ -105,6 +107,7 @@ export const buildDefaultFlatFieldMetadatasForCustomObject = ({
|
||||
viewGroupIds: [],
|
||||
kanbanAggregateOperationViewIds: [],
|
||||
calendarViewIds: [],
|
||||
mainGroupByFieldMetadataViewIds: [],
|
||||
isLabelSyncedWithName: false,
|
||||
isUnique: false,
|
||||
objectMetadataId,
|
||||
@@ -142,6 +145,7 @@ export const buildDefaultFlatFieldMetadatasForCustomObject = ({
|
||||
viewGroupIds: [],
|
||||
kanbanAggregateOperationViewIds: [],
|
||||
calendarViewIds: [],
|
||||
mainGroupByFieldMetadataViewIds: [],
|
||||
isLabelSyncedWithName: false,
|
||||
isUnique: false,
|
||||
objectMetadataId,
|
||||
@@ -179,6 +183,7 @@ export const buildDefaultFlatFieldMetadatasForCustomObject = ({
|
||||
viewGroupIds: [],
|
||||
kanbanAggregateOperationViewIds: [],
|
||||
calendarViewIds: [],
|
||||
mainGroupByFieldMetadataViewIds: [],
|
||||
isLabelSyncedWithName: false,
|
||||
isUnique: false,
|
||||
objectMetadataId,
|
||||
@@ -216,6 +221,7 @@ export const buildDefaultFlatFieldMetadatasForCustomObject = ({
|
||||
viewGroupIds: [],
|
||||
kanbanAggregateOperationViewIds: [],
|
||||
calendarViewIds: [],
|
||||
mainGroupByFieldMetadataViewIds: [],
|
||||
isLabelSyncedWithName: false,
|
||||
isUnique: false,
|
||||
objectMetadataId,
|
||||
@@ -252,6 +258,7 @@ export const buildDefaultFlatFieldMetadatasForCustomObject = ({
|
||||
viewGroupIds: [],
|
||||
kanbanAggregateOperationViewIds: [],
|
||||
calendarViewIds: [],
|
||||
mainGroupByFieldMetadataViewIds: [],
|
||||
isLabelSyncedWithName: false,
|
||||
isUnique: false,
|
||||
objectMetadataId,
|
||||
@@ -284,6 +291,7 @@ export const buildDefaultFlatFieldMetadatasForCustomObject = ({
|
||||
const searchVectorFieldId = v4();
|
||||
const searchVectorField: FlatFieldMetadata<FieldMetadataType.TS_VECTOR> = {
|
||||
type: FieldMetadataType.TS_VECTOR,
|
||||
mainGroupByFieldMetadataViewIds: [],
|
||||
viewFieldIds: [],
|
||||
viewGroupIds: [],
|
||||
kanbanAggregateOperationViewIds: [],
|
||||
|
||||
+2
@@ -61,6 +61,7 @@ const generateSourceFlatFieldMetadata = ({
|
||||
|
||||
return {
|
||||
calendarViewIds: [],
|
||||
mainGroupByFieldMetadataViewIds: [],
|
||||
kanbanAggregateOperationViewIds: [],
|
||||
viewFilterIds: [],
|
||||
viewFieldIds: [],
|
||||
@@ -128,6 +129,7 @@ const generateTargetFlatFieldMetadata = ({
|
||||
return {
|
||||
morphId: null,
|
||||
calendarViewIds: [],
|
||||
mainGroupByFieldMetadataViewIds: [],
|
||||
viewFieldIds: [],
|
||||
kanbanAggregateOperationViewIds: [],
|
||||
viewFilterIds: [],
|
||||
|
||||
+8
-3
@@ -69,7 +69,7 @@ export class ViewFilterService {
|
||||
workspaceCustomApplicationId: workspaceCustomFlatApplication.id,
|
||||
});
|
||||
|
||||
const validateAndBuildResult =
|
||||
const buildAndRunResult =
|
||||
await this.workspaceMigrationValidateBuildAndRunService.validateBuildAndRunWorkspaceMigration(
|
||||
{
|
||||
fromToAllFlatEntityMaps: {
|
||||
@@ -92,13 +92,18 @@ export class ViewFilterService {
|
||||
},
|
||||
);
|
||||
|
||||
if (isDefined(validateAndBuildResult)) {
|
||||
if (isDefined(buildAndRunResult)) {
|
||||
throw new WorkspaceMigrationBuilderExceptionV2(
|
||||
validateAndBuildResult,
|
||||
buildAndRunResult,
|
||||
'Multiple validation errors occurred while creating view filter',
|
||||
);
|
||||
}
|
||||
|
||||
this.flatEntityMapsCacheService.invalidateFlatEntityMaps({
|
||||
workspaceId,
|
||||
flatMapsKeys: ['flatViewFilterMaps'],
|
||||
});
|
||||
|
||||
const { flatViewFilterMaps: recomputedExistingFlatViewFilterMaps } =
|
||||
await this.flatEntityMapsCacheService.getOrRecomputeManyOrAllFlatEntityMaps(
|
||||
{
|
||||
|
||||
+5
@@ -92,6 +92,11 @@ export class CreateViewInput {
|
||||
@Field(() => UUIDScalarType, { nullable: true })
|
||||
calendarFieldMetadataId?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsUUID()
|
||||
@Field(() => UUIDScalarType, { nullable: true })
|
||||
mainGroupByFieldMetadataId?: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsEnum(ViewVisibility)
|
||||
@Field(() => ViewVisibility, {
|
||||
|
||||
+5
@@ -87,4 +87,9 @@ export class UpdateViewInput {
|
||||
@IsEnum(ViewVisibility)
|
||||
@Field(() => ViewVisibility, { nullable: true })
|
||||
visibility?: ViewVisibility;
|
||||
|
||||
@IsOptional()
|
||||
@IsUUID()
|
||||
@Field(() => UUIDScalarType, { nullable: true })
|
||||
mainGroupByFieldMetadataId?: string;
|
||||
}
|
||||
|
||||
@@ -62,6 +62,9 @@ export class ViewDTO {
|
||||
@Field(() => UUIDScalarType, { nullable: true })
|
||||
kanbanAggregateOperationFieldMetadataId?: string | null;
|
||||
|
||||
@Field(() => UUIDScalarType, { nullable: true })
|
||||
mainGroupByFieldMetadataId?: string | null;
|
||||
|
||||
@Field(() => UUIDScalarType, { nullable: true })
|
||||
calendarFieldMetadataId?: string | null;
|
||||
|
||||
|
||||
@@ -138,6 +138,20 @@ export class ViewEntity extends SyncableEntity implements Required<ViewEntity> {
|
||||
@JoinColumn({ name: 'calendarFieldMetadataId' })
|
||||
calendarFieldMetadata: Relation<FieldMetadataEntity>;
|
||||
|
||||
@Column({ nullable: true, type: 'uuid' })
|
||||
mainGroupByFieldMetadataId: string | null;
|
||||
|
||||
@ManyToOne(
|
||||
() => FieldMetadataEntity,
|
||||
(fieldMetadata) => fieldMetadata.mainGroupByFieldMetadataViews,
|
||||
{
|
||||
onDelete: 'CASCADE',
|
||||
nullable: true,
|
||||
},
|
||||
)
|
||||
@JoinColumn({ name: 'mainGroupByFieldMetadataId' })
|
||||
mainGroupByFieldMetadata: Relation<FieldMetadataEntity>;
|
||||
|
||||
@Column({ nullable: false, type: 'uuid' })
|
||||
workspaceId: string;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user