Optimize migrate-email-fields-command (#7035)
Quick follow up to prepare for 0.30 release
This commit is contained in:
@ -30,28 +30,11 @@ export class WorkspaceDatasourceFactory {
|
||||
workspaceId: string,
|
||||
workspaceMetadataVersion: number | null,
|
||||
): Promise<WorkspaceDataSource> {
|
||||
const latestWorkspaceMetadataVersion =
|
||||
await this.workspaceCacheStorageService.getMetadataVersion(workspaceId);
|
||||
|
||||
if (latestWorkspaceMetadataVersion === undefined) {
|
||||
await this.workspaceMetadataCacheService.recomputeMetadataCache(
|
||||
workspaceId,
|
||||
);
|
||||
throw new TwentyORMException(
|
||||
`Metadata version not found for workspace ${workspaceId}`,
|
||||
TwentyORMExceptionCode.METADATA_VERSION_NOT_FOUND,
|
||||
);
|
||||
}
|
||||
|
||||
const desiredWorkspaceMetadataVersion =
|
||||
workspaceMetadataVersion ?? latestWorkspaceMetadataVersion;
|
||||
|
||||
if (latestWorkspaceMetadataVersion !== desiredWorkspaceMetadataVersion) {
|
||||
throw new TwentyORMException(
|
||||
`Workspace metadata version mismatch detected for workspace ${workspaceId}. Current version: ${latestWorkspaceMetadataVersion}. Desired version: ${desiredWorkspaceMetadataVersion}`,
|
||||
TwentyORMExceptionCode.METADATA_VERSION_MISMATCH,
|
||||
await this.computeDesiredWorkspaceMetadataVersion(
|
||||
workspaceId,
|
||||
workspaceMetadataVersion,
|
||||
);
|
||||
}
|
||||
|
||||
const workspaceDataSource = await this.cacheManager.execute(
|
||||
`${workspaceId}-${desiredWorkspaceMetadataVersion}`,
|
||||
@ -166,4 +149,46 @@ export class WorkspaceDatasourceFactory {
|
||||
|
||||
return workspaceDataSource;
|
||||
}
|
||||
|
||||
private async computeDesiredWorkspaceMetadataVersion(
|
||||
workspaceId: string,
|
||||
workspaceMetadataVersion: number | null,
|
||||
): Promise<number> {
|
||||
const latestWorkspaceMetadataVersion =
|
||||
await this.workspaceCacheStorageService.getMetadataVersion(workspaceId);
|
||||
|
||||
if (latestWorkspaceMetadataVersion === undefined) {
|
||||
await this.workspaceMetadataCacheService.recomputeMetadataCache(
|
||||
workspaceId,
|
||||
);
|
||||
throw new TwentyORMException(
|
||||
`Metadata version not found for workspace ${workspaceId}`,
|
||||
TwentyORMExceptionCode.METADATA_VERSION_NOT_FOUND,
|
||||
);
|
||||
}
|
||||
|
||||
const desiredWorkspaceMetadataVersion =
|
||||
workspaceMetadataVersion ?? latestWorkspaceMetadataVersion;
|
||||
|
||||
if (latestWorkspaceMetadataVersion !== desiredWorkspaceMetadataVersion) {
|
||||
throw new TwentyORMException(
|
||||
`Workspace metadata version mismatch detected for workspace ${workspaceId}. Current version: ${latestWorkspaceMetadataVersion}. Desired version: ${desiredWorkspaceMetadataVersion}`,
|
||||
TwentyORMExceptionCode.METADATA_VERSION_MISMATCH,
|
||||
);
|
||||
}
|
||||
|
||||
return desiredWorkspaceMetadataVersion;
|
||||
}
|
||||
|
||||
public async destroy(
|
||||
workspaceId: string,
|
||||
metadataVersion: number | null,
|
||||
): Promise<void> {
|
||||
const desiredWorkspaceMetadataVersion =
|
||||
this.computeDesiredWorkspaceMetadataVersion(workspaceId, metadataVersion);
|
||||
|
||||
await this.cacheManager.clearKey(
|
||||
`${workspaceId}-${desiredWorkspaceMetadataVersion}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -34,6 +34,16 @@ export class CacheManager<T> {
|
||||
return value;
|
||||
}
|
||||
|
||||
async clearKey(
|
||||
cacheKey: CacheKey,
|
||||
onDelete?: (value: T) => Promise<void> | void,
|
||||
): Promise<void> {
|
||||
if (this.cache.has(cacheKey)) {
|
||||
await onDelete?.(this.cache.get(cacheKey)!);
|
||||
this.cache.delete(cacheKey);
|
||||
}
|
||||
}
|
||||
|
||||
async clear(onDelete?: (value: T) => Promise<void> | void): Promise<void> {
|
||||
for (const value of this.cache.values()) {
|
||||
await onDelete?.(value);
|
||||
|
||||
@ -53,4 +53,8 @@ export class TwentyORMGlobalManager {
|
||||
async loadDataSourceForWorkspace(workspaceId: string) {
|
||||
await this.workspaceDataSourceFactory.create(workspaceId, null);
|
||||
}
|
||||
|
||||
async destroyDataSourceForWorkspace(workspaceId: string) {
|
||||
await this.workspaceDataSourceFactory.destroy(workspaceId, null);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user