Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesBochet committed Aug 20, 2024
1 parent 4759f6d commit 970dfe3
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const useApolloFactory = (options: Partial<Options<any>> = {}) => {
}),
headers: {
...(currentWorkspace?.metadataVersion && {
'X-Schema-Version': currentWorkspace.metadataVersion,
'X-Schema-Version': `${currentWorkspace.metadataVersion}`,
}),
},
defaultOptions: {
Expand Down
4 changes: 3 additions & 1 deletion packages/twenty-front/src/testing/mock-data/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,16 @@ export const mockDefaultWorkspace: Workspace = {
],
createdAt: '2023-04-26T10:23:42.33625+00:00',
updatedAt: '2023-04-26T10:23:42.33625+00:00',
metadataVersion: '1',
metadataVersion: 1,
currentBillingSubscription: {
__typename: 'BillingSubscription',
id: '7efbc3f7-6e5e-4128-957e-8d86808cdf6a',
interval: SubscriptionInterval.Month,
status: SubscriptionStatus.Active,
},
workspaceMembersCount: 1,
databaseSchema: '',
databaseUrl: '',
};

export const mockedWorkspaceMemberData: WorkspaceMember = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Test, TestingModule } from '@nestjs/testing';

import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service';
import { ObjectMetadataService } from 'src/engine/metadata-modules/object-metadata/object-metadata.service';
import { ScalarsExplorerService } from 'src/engine/api/graphql/services/scalars-explorer.service';
import { WorkspaceResolverFactory } from 'src/engine/api/graphql/workspace-resolver-builder/workspace-resolver.factory';
import { WorkspaceGraphQLSchemaFactory } from 'src/engine/api/graphql/workspace-schema-builder/workspace-graphql-schema.factory';
import { WorkspaceSchemaFactory } from 'src/engine/api/graphql/workspace-schema.factory';
import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service';
import { ObjectMetadataService } from 'src/engine/metadata-modules/object-metadata/object-metadata.service';
import { WorkspaceMetadataVersionService } from 'src/engine/metadata-modules/workspace-metadata-version/workspace-metadata-version.service';
import { WorkspaceCacheStorageService } from 'src/engine/workspace-cache-storage/workspace-cache-storage.service';

describe('WorkspaceSchemaFactory', () => {
Expand Down Expand Up @@ -39,6 +40,10 @@ describe('WorkspaceSchemaFactory', () => {
provide: WorkspaceCacheStorageService,
useValue: {},
},
{
provide: WorkspaceMetadataVersionService,
useValue: {},
},
],
}).compile();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function useCachedMetadata(config: CacheMetadataPluginConfig): Plugin {
serverContext.req.workspaceMetadataVersion ?? '0';
const operationName = getOperationName(serverContext);

return `cachedOperations:${operationName}:${workspaceId}:${workspaceMetadataVersion}`;
return `graphql:operations:${operationName}:${workspaceId}:${workspaceMetadataVersion}`;
};

const getOperationName = (serverContext: any) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { InjectMessageQueue } from 'src/engine/integrations/message-queue/decora
import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager';
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
import {
CalendarEventListFetchJob,
CalendarEventsImportJobData,
Expand Down Expand Up @@ -39,7 +39,7 @@ import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/sta
@Injectable()
export class GoogleAPIsService {
constructor(
private readonly twentyORMManager: TwentyORMManager,
private readonly twentyORMGlobalManager: TwentyORMGlobalManager,
@InjectMessageQueue(MessageQueue.messagingQueue)
private readonly messageQueueService: MessageQueueService,
@InjectMessageQueue(MessageQueue.calendarQueue)
Expand Down Expand Up @@ -82,16 +82,19 @@ export class GoogleAPIsService {
const newOrExistingConnectedAccountId = existingAccountId ?? v4();

const calendarChannelRepository =
await this.twentyORMManager.getRepository<CalendarChannelWorkspaceEntity>(
await this.twentyORMGlobalManager.getRepositoryForWorkspace<CalendarChannelWorkspaceEntity>(
workspaceId,
'calendarChannel',
);

const messageChannelRepository =
await this.twentyORMManager.getRepository<MessageChannelWorkspaceEntity>(
await this.twentyORMGlobalManager.getRepositoryForWorkspace<MessageChannelWorkspaceEntity>(
workspaceId,
'messageChannel',
);

const workspaceDataSource = await this.twentyORMManager.getDatasource();
const workspaceDataSource =
await this.twentyORMGlobalManager.getDataSourceForWorkspace(workspaceId);

await workspaceDataSource.transaction(async (manager: EntityManager) => {
if (!existingAccountId) {
Expand Down Expand Up @@ -146,7 +149,8 @@ export class GoogleAPIsService {
);

const workspaceMemberRepository =
await this.twentyORMManager.getRepository<WorkspaceMemberWorkspaceEntity>(
await this.twentyORMGlobalManager.getRepositoryForWorkspace<WorkspaceMemberWorkspaceEntity>(
workspaceId,
'workspaceMember',
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ export const useGraphQLErrorHandlerHook = <

if (Array.isArray(errors) && errors.length > 0) {
const headers = context.req.headers;
const currentSchemaVersion = context.req.workspaceMetadataVersion;
const currentMetadataVersion = context.req.workspaceMetadataVersion;

const requestSchemaVersion = headers['x-schema-version'];
const requestMetadataVersion = headers['x-schema-version'];

if (
requestSchemaVersion &&
requestSchemaVersion !== `${currentSchemaVersion}`
requestMetadataVersion &&
requestMetadataVersion !== `${currentMetadataVersion}`
) {
throw new GraphQLError(
`Schema version mismatch, please refresh the page.`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ export class WorkspaceMetadataVersionService {
const currentVersion =
(await this.workspaceCacheStorageService.getMetadataVersion(
workspaceId,
)) ?? 0;
)) ?? 1;

let latestVersion = await this.getMetadataVersion(workspaceId);

if (!latestVersion || currentVersion !== latestVersion) {
if (latestVersion === undefined || currentVersion !== latestVersion) {
this.logger.log(
`Metadata version mismatch detected for workspace ${workspaceId}. Current version: ${currentVersion}. Latest version: ${latestVersion}. Invalidating cache...`,
);
Expand All @@ -43,33 +43,37 @@ export class WorkspaceMetadataVersionService {
}

async incrementMetadataVersion(workspaceId: string): Promise<number> {
const metadataVersion = (await this.getMetadataVersion(workspaceId)) ?? '0';
const metadataVersion = (await this.getMetadataVersion(workspaceId)) ?? 0;
const newMetadataVersion = metadataVersion + 1;

await this.workspaceRepository.update(
{ id: workspaceId },
{ metadataVersion: newMetadataVersion },
);

await this.flushCacheIfMetadataVersionIsOutdated(workspaceId);
await this.workspaceCacheStorageService.setMetadataVersion(
workspaceId,
newMetadataVersion,
);

return newMetadataVersion;
}

async getMetadataVersion(workspaceId: string): Promise<number> {
async getMetadataVersion(workspaceId: string): Promise<number | undefined> {
const workspace = await this.workspaceRepository.findOne({
where: { id: workspaceId },
});

return workspace?.metadataVersion ?? 0;
return workspace?.metadataVersion;
}

async resetMetadataVersion(workspaceId: string): Promise<void> {
await this.workspaceRepository.update(
{ id: workspaceId },
{ metadataVersion: 0 },
{ metadataVersion: 1 },
);

await this.flushCacheIfMetadataVersionIsOutdated(workspaceId);
await this.workspaceCacheStorageService.flush(workspaceId);
await this.workspaceCacheStorageService.setMetadataVersion(workspaceId, 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,29 @@ export class WorkspaceDatasourceFactory {

public async create(
workspaceId: string,
workspaceSchemaVersion: string | null,
workspaceMetadataVersion: string | null,
): Promise<WorkspaceDataSource> {
const desiredWorkspaceSchemaVersion =
workspaceSchemaVersion ??
const desiredWorkspaceMetadataVersion =
workspaceMetadataVersion ??
(await this.workspaceMetadataVersionService.getMetadataVersion(
workspaceId,
));

if (!desiredWorkspaceSchemaVersion) {
throw new Error('Cache version not found');
if (!desiredWorkspaceMetadataVersion) {
throw new Error(
`Desired workspace metadata version not found while creating workspace data source for workspace ${workspaceId}`,
);
}

const latestWorkspaceSchemaVersion =
const latestWorkspaceMetadataVersion =
await this.workspaceMetadataVersionService.getMetadataVersion(
workspaceId,
);

if (latestWorkspaceSchemaVersion !== desiredWorkspaceSchemaVersion) {
throw new Error('Cache version mismatch');
if (latestWorkspaceMetadataVersion !== desiredWorkspaceMetadataVersion) {
throw new Error(
`Workspace metadata version mismatch detected for workspace ${workspaceId}. Current version: ${latestWorkspaceMetadataVersion}. Desired version: ${desiredWorkspaceMetadataVersion}`,
);
}

let cachedObjectMetadataCollection =
Expand Down Expand Up @@ -74,7 +78,7 @@ export class WorkspaceDatasourceFactory {
}

const workspaceDataSource = await workspaceDataSourceCacheInstance.execute(
`${workspaceId}-${latestWorkspaceSchemaVersion}`,
`${workspaceId}-${latestWorkspaceMetadataVersion}`,
async () => {
const dataSourceMetadata =
await this.dataSourceService.getLastDataSourceMetadataFromWorkspaceId(
Expand Down

0 comments on commit 970dfe3

Please sign in to comment.