mirror of
https://github.com/open-metadata/OpenMetadata.git
synced 2026-01-08 13:36:32 +00:00
FIX - Cleanup orphaned data contracts (#23596)
This commit is contained in:
parent
78c613445b
commit
f72dc826bf
@ -17,5 +17,6 @@ public class Migration extends MigrationProcessImpl {
|
||||
public void runDataMigration() {
|
||||
MigrationUtil migrationUtil = new MigrationUtil(handle, ConnectionType.MYSQL);
|
||||
migrationUtil.migrateEntityStatusForExistingEntities();
|
||||
migrationUtil.cleanupOrphanedDataContracts();
|
||||
}
|
||||
}
|
||||
|
||||
@ -17,5 +17,6 @@ public class Migration extends MigrationProcessImpl {
|
||||
public void runDataMigration() {
|
||||
MigrationUtil migrationUtil = new MigrationUtil(handle, ConnectionType.POSTGRES);
|
||||
migrationUtil.migrateEntityStatusForExistingEntities();
|
||||
migrationUtil.cleanupOrphanedDataContracts();
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,7 +1,14 @@
|
||||
package org.openmetadata.service.migration.utils.v1100;
|
||||
|
||||
import java.util.List;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jdbi.v3.core.Handle;
|
||||
import org.openmetadata.schema.entity.data.DataContract;
|
||||
import org.openmetadata.schema.type.Include;
|
||||
import org.openmetadata.service.Entity;
|
||||
import org.openmetadata.service.exception.EntityNotFoundException;
|
||||
import org.openmetadata.service.jdbi3.DataContractRepository;
|
||||
import org.openmetadata.service.jdbi3.ListFilter;
|
||||
import org.openmetadata.service.jdbi3.locator.ConnectionType;
|
||||
|
||||
@Slf4j
|
||||
@ -304,4 +311,62 @@ public class MigrationUtil {
|
||||
|
||||
return totalMigrated;
|
||||
}
|
||||
|
||||
public void cleanupOrphanedDataContracts() {
|
||||
LOG.info("Starting cleanup of orphaned data contracts...");
|
||||
|
||||
try {
|
||||
DataContractRepository dataContractRepository =
|
||||
(DataContractRepository) Entity.getEntityRepository(Entity.DATA_CONTRACT);
|
||||
|
||||
List<DataContract> allDataContracts =
|
||||
dataContractRepository.listAll(
|
||||
dataContractRepository.getFields("id,entity"), new ListFilter(Include.ALL));
|
||||
|
||||
if (allDataContracts.isEmpty()) {
|
||||
LOG.info("✓ No data contracts found - cleanup complete");
|
||||
return;
|
||||
}
|
||||
|
||||
int deletedCount = 0;
|
||||
int totalContracts = allDataContracts.size();
|
||||
|
||||
LOG.info("Found {} data contracts to validate", totalContracts);
|
||||
|
||||
for (DataContract dataContract : allDataContracts) {
|
||||
try {
|
||||
// Try to get the associated entity
|
||||
Entity.getEntityReferenceById(
|
||||
dataContract.getEntity().getType(),
|
||||
dataContract.getEntity().getId(),
|
||||
Include.NON_DELETED);
|
||||
|
||||
} catch (EntityNotFoundException e) {
|
||||
LOG.info(
|
||||
"Deleting orphaned data contract '{}' - associated {} entity with ID {} not found",
|
||||
dataContract.getFullyQualifiedName(),
|
||||
dataContract.getEntity().getType(),
|
||||
dataContract.getEntity().getId());
|
||||
|
||||
try {
|
||||
dataContractRepository.delete(Entity.ADMIN_USER_NAME, dataContract.getId(), true, true);
|
||||
deletedCount++;
|
||||
} catch (Exception deleteException) {
|
||||
LOG.warn(
|
||||
"Failed to delete orphaned data contract '{}': {}",
|
||||
dataContract.getFullyQualifiedName(),
|
||||
deleteException.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LOG.info(
|
||||
"✓ Cleanup complete: {} orphaned data contracts deleted out of {} total",
|
||||
deletedCount,
|
||||
totalContracts);
|
||||
|
||||
} catch (Exception e) {
|
||||
LOG.error("✗ FAILED cleanup of orphaned data contracts: {}", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user