refactor: move PG cleanup to TenantDatabaseService, keep only ClickHouse
TenantDataCleanupService now handles only ClickHouse GDPR erasure; the dropPostgresSchema private method is removed and the public method renamed cleanupClickHouse(). VendorTenantService updated accordingly with the TODO comment removed. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -12,8 +12,8 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cleans up tenant data from the server PostgreSQL and ClickHouse databases
|
* Deletes tenant data from ClickHouse tables when a tenant is deleted
|
||||||
* when a tenant is deleted (GDPR data erasure).
|
* (GDPR data erasure). PostgreSQL cleanup is handled by TenantDatabaseService.
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class TenantDataCleanupService {
|
public class TenantDataCleanupService {
|
||||||
@@ -26,33 +26,14 @@ public class TenantDataCleanupService {
|
|||||||
this.props = props;
|
this.props = props;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void cleanup(String slug) {
|
/**
|
||||||
dropPostgresSchema(slug);
|
* Deletes tenant data from ClickHouse tables (GDPR data erasure).
|
||||||
|
* PostgreSQL cleanup is handled by TenantDatabaseService.
|
||||||
|
*/
|
||||||
|
public void cleanupClickHouse(String slug) {
|
||||||
deleteClickHouseData(slug);
|
deleteClickHouseData(slug);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void dropPostgresSchema(String slug) {
|
|
||||||
String url = props.datasourceUrl();
|
|
||||||
if (url == null || url.isBlank()) {
|
|
||||||
log.warn("No server datasource URL configured — skipping PostgreSQL schema cleanup");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
String schema = "tenant_" + slug;
|
|
||||||
if (!schema.matches("^[a-z0-9_-]+$")) {
|
|
||||||
log.error("Refusing to drop schema with unexpected characters: {}", schema);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try (Connection conn = DriverManager.getConnection(url, props.datasourceUsername(), props.datasourcePassword());
|
|
||||||
Statement stmt = conn.createStatement()) {
|
|
||||||
stmt.execute("DROP SCHEMA IF EXISTS \"" + schema + "\" CASCADE");
|
|
||||||
log.info("Dropped PostgreSQL schema: {}", schema);
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.warn("Failed to drop PostgreSQL schema '{}': {}", schema, e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void deleteClickHouseData(String slug) {
|
private void deleteClickHouseData(String slug) {
|
||||||
String url = props.clickhouseUrl();
|
String url = props.clickhouseUrl();
|
||||||
if (url == null || url.isBlank()) {
|
if (url == null || url.isBlank()) {
|
||||||
|
|||||||
@@ -336,9 +336,8 @@ public class VendorTenantService {
|
|||||||
log.warn("Failed to drop tenant database for {}: {}", tenant.getSlug(), e.getMessage());
|
log.warn("Failed to drop tenant database for {}: {}", tenant.getSlug(), e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Erase tenant data from server databases (GDPR)
|
// Erase tenant data from ClickHouse (GDPR)
|
||||||
// TODO: split into cleanupClickHouse() in next task
|
dataCleanupService.cleanupClickHouse(tenant.getSlug());
|
||||||
dataCleanupService.cleanup(tenant.getSlug());
|
|
||||||
|
|
||||||
// Soft-delete
|
// Soft-delete
|
||||||
tenant.setStatus(TenantStatus.DELETED);
|
tenant.setStatus(TenantStatus.DELETED);
|
||||||
|
|||||||
Reference in New Issue
Block a user