Clean metadata and schema (#2382)

This commit is contained in:
martmull
2023-11-07 12:33:54 +01:00
committed by GitHub
parent 7aa6b20418
commit 0ae56b055c
6 changed files with 39 additions and 0 deletions

View File

@ -161,6 +161,8 @@ export class WorkspaceService {
this.delete({ where: { id: workspaceId } }),
]);
await this.tenantInitialisationService.delete(workspaceId);
return workspace;
}
}

View File

@ -41,4 +41,8 @@ export class DataSourceMetadataService {
order: { createdAt: 'DESC' },
});
}
async delete(workspaceId: string) {
await this.dataSourceMetadataRepository.delete({ workspaceId });
}
}

View File

@ -121,6 +121,20 @@ export class DataSourceService implements OnModuleInit, OnModuleDestroy {
return `workspace_${uuidToBase36(workspaceId)}`;
}
public async deleteWorkspaceSchema(workspaceId: string) {
const schemaName = this.getSchemaName(workspaceId);
const queryRunner = this.mainDataSource.createQueryRunner();
const schemaAlreadyExists = await queryRunner.hasSchema(schemaName);
if (!schemaAlreadyExists) {
throw new Error(`Schema ${schemaName} does not exist`);
}
await queryRunner.dropSchema(schemaName, true, true);
await queryRunner.release();
}
async onModuleInit() {
// Init main data source "default" schema
await this.mainDataSource.initialize();

View File

@ -120,4 +120,8 @@ export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadata> {
})),
);
}
public async deleteObjectsAndFieldsMetadata(workspaceId: string) {
await this.objectMetadataRepository.delete({ workspaceId });
}
}

View File

@ -75,4 +75,15 @@ export class TenantInitialisationService {
standardObjectsPrefillData(workspaceDataSource, dataSourceMetadata.schema);
}
public async delete(workspaceId: string): Promise<void> {
// Delete data from metadata tables
await this.objectMetadataService.deleteObjectsAndFieldsMetadata(
workspaceId,
);
await this.tenantMigrationService.delete(workspaceId);
await this.dataSourceMetadataService.delete(workspaceId);
// Delete schema
await this.dataSourceService.deleteWorkspaceSchema(workspaceId);
}
}

View File

@ -103,4 +103,8 @@ export class TenantMigrationService {
isCustom: true,
});
}
public async delete(workspaceId: string) {
await this.tenantMigrationRepository.delete({ workspaceId });
}
}