Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize migrate-email-fields-command #7035

Merged
merged 3 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,12 @@ export class MigrateEmailFieldsToEmailsCommand extends ActiveWorkspacesCommandRu
);

for (const workspaceId of workspaceIds) {
let dataSourceMetadata;
let workspaceQueryRunner;

this.logger.log(`Running command for workspace ${workspaceId}`);
try {
const dataSourceMetadata =
dataSourceMetadata =
await this.dataSourceService.getLastDataSourceMetadataFromWorkspaceId(
workspaceId,
);
Expand All @@ -77,10 +80,19 @@ export class MigrateEmailFieldsToEmailsCommand extends ActiveWorkspacesCommandRu
);
}

const workspaceQueryRunner = workspaceDataSource.createQueryRunner();
workspaceQueryRunner = workspaceDataSource.createQueryRunner();

await workspaceQueryRunner.connect();
} catch (error) {
this.logger.log(
chalk.red(
`Could not connect to workspace data source for workspace ${workspaceId}`,
),
);
continue;
}

try {
const customFieldsWithEmailType =
await this.fieldMetadataRepository.find({
where: {
Expand Down Expand Up @@ -267,17 +279,19 @@ export class MigrateEmailFieldsToEmailsCommand extends ActiveWorkspacesCommandRu
workspaceId,
);
}
} finally {
await workspaceQueryRunner.release();
}
}
} catch (error) {
await workspaceQueryRunner.release();

this.logger.log(
chalk.red(
`Running command on workspace ${workspaceId} failed with error: ${error}`,
),
);
continue;
} finally {
await workspaceQueryRunner.release();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the query runner was not release in case of errors

}

this.logger.log(chalk.green(`Command completed!`));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Expand Down Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: This method call should be awaited

Suggested change
this.computeDesiredWorkspaceMetadataVersion(workspaceId, metadataVersion);
await this.computeDesiredWorkspaceMetadataVersion(workspaceId, metadataVersion);


await this.cacheManager.clearKey(
`${workspaceId}-${desiredWorkspaceMetadataVersion}`,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Loading