Skip to content

Commit

Permalink
Flush cache when reset db (twentyhq#5214)
Browse files Browse the repository at this point in the history
Now that we have persistent cache for schemas, we want to be able to
reset its state when users run the database:reset db otherwise schemas
won't be synced with the new DB state.

Note: In an upcoming PR, we want to be able to invalidate the cache on a
workspace level when we change the metadata schema through twenty
version upgrade
  • Loading branch information
Weiko authored Apr 30, 2024
1 parent 01c81c1 commit 7e1de81
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { seedCoreSchema } from 'src/database/typeorm-seeds/core';
import { ObjectMetadataService } from 'src/engine/metadata-modules/object-metadata/object-metadata.service';
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
import { WorkspaceSyncMetadataService } from 'src/engine/workspace-manager/workspace-sync-metadata/workspace-sync-metadata.service';
import { EnvironmentService } from 'src/engine/integrations/environment/environment.service';
import {
SEED_APPLE_WORKSPACE_ID,
SEED_TWENTY_WORKSPACE_ID,
Expand All @@ -28,6 +27,9 @@ import { seedCalendarChannels } from 'src/database/typeorm-seeds/workspace/calen
import { seedCalendarChannelEventAssociations } from 'src/database/typeorm-seeds/workspace/calendar-channel-event-association';
import { seedCalendarEventParticipants } from 'src/database/typeorm-seeds/workspace/calendar-event-participants';
import { rawDataSource } from 'src/database/typeorm/raw/raw.datasource';
import { CacheStorageService } from 'src/engine/integrations/cache-storage/cache-storage.service';
import { InjectCacheStorage } from 'src/engine/integrations/cache-storage/decorators/cache-storage.decorator';
import { CacheStorageNamespace } from 'src/engine/integrations/cache-storage/types/cache-storage-namespace.enum';

// TODO: implement dry-run
@Command({
Expand All @@ -39,19 +41,22 @@ export class DataSeedWorkspaceCommand extends CommandRunner {
workspaceIds = [SEED_APPLE_WORKSPACE_ID, SEED_TWENTY_WORKSPACE_ID];

constructor(
private readonly environmentService: EnvironmentService,
private readonly dataSourceService: DataSourceService,
private readonly typeORMService: TypeORMService,
private readonly workspaceSyncMetadataService: WorkspaceSyncMetadataService,
private readonly workspaceDataSourceService: WorkspaceDataSourceService,
private readonly objectMetadataService: ObjectMetadataService,
@InjectCacheStorage(CacheStorageNamespace.WorkspaceSchema)
private readonly workspaceSchemaCache: CacheStorageService,
) {
super();
}

async run(): Promise<void> {
try {
for (const workspaceId of this.workspaceIds) {
await this.workspaceSchemaCache.flush();

await rawDataSource.initialize();

await seedCoreSchema(rawDataSource, workspaceId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ export class CacheStorageService {
});
}

async flush() {
return this.cache.reset();
}

private isRedisCache() {
return (this.cache.store as any)?.name === 'redis';
}
Expand Down

0 comments on commit 7e1de81

Please sign in to comment.