diff --git a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/ChartRepository.java b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/ChartRepository.java index ab189c11a8a..fc55520211d 100644 --- a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/ChartRepository.java +++ b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/ChartRepository.java @@ -120,7 +120,7 @@ public class ChartRepository extends EntityRepository { private EntityReference getService(Chart chart) throws IOException { EntityReference ref = - EntityUtil.getService(daoCollection.relationshipDAO(), chart.getId(), Entity.DASHBOARD_SERVICE); + EntityUtil.getService(daoCollection.relationshipDAO(), Entity.CHART, chart.getId(), Entity.DASHBOARD_SERVICE); if (ref != null) { DashboardService service = getService(ref.getId(), ref.getType()); ref.setName(service.getName()); diff --git a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/CollectionDAO.java b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/CollectionDAO.java index 53506019fda..e914b58dabd 100644 --- a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/CollectionDAO.java +++ b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/CollectionDAO.java @@ -341,75 +341,84 @@ public interface CollectionDAO { // @SqlQuery( "SELECT toId, toEntity FROM entity_relationship " - + "WHERE fromId = :fromId AND relation = :relation AND deleted = false " + + "WHERE fromId = :fromId AND fromEntity = :fromEntity AND relation = :relation AND deleted = false " + "ORDER BY toId") @RegisterRowMapper(ToEntityReferenceMapper.class) - List findTo(@Bind("fromId") String fromId, @Bind("relation") int relation); + List findTo(@Bind("fromId") String fromId, @Bind("fromEntity") String fromEntity, @Bind("relation") int relation); @SqlQuery( "SELECT toId FROM entity_relationship " - + "WHERE fromId = :fromId AND relation = :relation AND toEntity = :toEntity AND deleted = false " + + "WHERE fromId = :fromId AND fromEntity = :fromEntity AND relation = :relation AND toEntity = :toEntity AND deleted = false " + "ORDER BY toId") List findTo( - @Bind("fromId") String fromId, @Bind("relation") int relation, @Bind("toEntity") String toEntity); + @Bind("fromId") String fromId, @Bind("fromEntity") String fromEntity, @Bind("relation") int relation, @Bind("toEntity") String toEntity); @SqlQuery( "SELECT count(*) FROM entity_relationship " - + "WHERE fromId = :fromId AND relation = :relation AND (toEntity = :toEntity || :toEntity IS NULL) " + + "WHERE fromId = :fromId AND fromEntity = :fromEntity AND relation = :relation " + + "AND (toEntity = :toEntity || :toEntity IS NULL) " + "AND deleted = false " + "ORDER BY fromId") - int findToCount(@Bind("fromId") String fromId, @Bind("relation") int relation, @Bind("toEntity") String toEntity); + int findToCount(@Bind("fromId") String fromId, @Bind("fromEntity") String fromEntity, @Bind("relation") int relation, @Bind("toEntity") String toEntity); // // Find from operations // @SqlQuery( "SELECT fromId FROM entity_relationship " - + "WHERE toId = :toId AND relation = :relation AND fromEntity = :fromEntity AND deleted = false " + + "WHERE toId = :toId AND toEntity = :toEntity AND relation = :relation AND fromEntity = :fromEntity AND deleted = false " + "ORDER BY fromId") List findFrom( - @Bind("toId") String toId, @Bind("relation") int relation, @Bind("fromEntity") String fromEntity); + @Bind("toId") String toId, @Bind("toEntity") String toEntity, @Bind("relation") int relation, @Bind("fromEntity") String fromEntity); @SqlQuery( "SELECT fromId, fromEntity FROM entity_relationship " - + "WHERE toId = :toId AND relation = :relation AND deleted = false " + + "WHERE toId = :toId AND toEntity = :toEntity AND relation = :relation AND deleted = false " + "ORDER BY fromId") @RegisterRowMapper(FromEntityReferenceMapper.class) - List findFrom(@Bind("toId") String toId, @Bind("relation") int relation); + List findFrom(@Bind("toId") String toId, @Bind("toEntity") String toEntity, @Bind("relation") int relation); @SqlQuery( "SELECT fromId, fromEntity FROM entity_relationship " - + "WHERE toId = :toId AND relation = :relation AND fromEntity = :fromEntity AND deleted = false " + + "WHERE toId = :toId AND toEntity = :toEntity AND relation = :relation AND fromEntity = :fromEntity AND deleted = false " + "ORDER BY fromId") @RegisterRowMapper(FromEntityReferenceMapper.class) List findFromEntity( - @Bind("toId") String toId, @Bind("relation") int relation, @Bind("fromEntity") String fromEntity); + @Bind("toId") String toId, @Bind("toEntity") String toEntity, @Bind("relation") int relation, @Bind("fromEntity") String fromEntity); // // Delete Operations // - @SqlUpdate("DELETE from entity_relationship " + "WHERE fromId = :fromId AND toId = :toId AND relation = :relation") - void delete(@Bind("fromId") String fromId, @Bind("toId") String toId, @Bind("relation") int relation); + @SqlUpdate("DELETE from entity_relationship WHERE fromId = :fromId " + + "AND fromEntity = :fromEntity AND toId = :toId AND toEntity = :toEntity " + + "AND relation = :relation") + void delete(@Bind("fromId") String fromId, @Bind("fromEntity") String fromEntity, @Bind("toId") String toId, + @Bind("toEntity") String toEntity, @Bind("relation") int relation); // Delete all the entity relationship fromID --- relation --> entity of type toEntity - @SqlUpdate( - "DELETE from entity_relationship " + "WHERE fromId = :fromId AND relation = :relation AND toEntity = :toEntity") - void deleteFrom(@Bind("fromId") String fromId, @Bind("relation") int relation, @Bind("toEntity") String toEntity); + @SqlUpdate("DELETE from entity_relationship WHERE fromId = :fromId AND fromEntity = :fromEntity " + + "AND relation = :relation AND toEntity = :toEntity") + void deleteFrom(@Bind("fromId") String fromId, @Bind("fromEntity") String fromEntity, + @Bind("relation") int relation, @Bind("toEntity") String toEntity); // Delete all the entity relationship fromID --- relation --> to any entity - @SqlUpdate("DELETE from entity_relationship " + "WHERE fromId = :fromId AND relation = :relation") - void deleteFrom(@Bind("fromId") String fromId, @Bind("relation") int relation); + @SqlUpdate("DELETE from entity_relationship WHERE fromId = :fromId AND fromEntity = :fromEntity " + + "AND relation = :relation") + void deleteFrom(@Bind("fromId") String fromId, @Bind("fromEntity") String fromEntity, @Bind("relation") int relation); // Delete all the entity relationship toId <-- relation -- entity of type fromEntity - @SqlUpdate( - "DELETE from entity_relationship " + "WHERE toId = :toId AND relation = :relation AND fromEntity = :fromEntity") - void deleteTo(@Bind("toId") String toId, @Bind("relation") int relation, @Bind("fromEntity") String fromEntity); + @SqlUpdate("DELETE from entity_relationship WHERE toId = :toId AND toEntity = :toEntity AND relation = :relation " + + "AND fromEntity = :fromEntity") + void deleteTo(@Bind("toId") String toId, @Bind("toEntity") String toEntity, @Bind("relation") int relation, + @Bind("fromEntity") String fromEntity); - @SqlUpdate("DELETE from entity_relationship " + "WHERE toId = :id OR fromId = :id") - void deleteAll(@Bind("id") String id); + @SqlUpdate("DELETE from entity_relationship WHERE (toId = :id AND toEntity = :entity) OR " + + "(fromId = :id AND toEntity = :entity)") + void deleteAll(@Bind("id") String id, @Bind("entity") String entity); - @SqlUpdate("UPDATE entity_relationship SET deleted = true WHERE toId = :id OR fromId = :id") - void softDeleteAll(@Bind("id") String id); + @SqlUpdate("UPDATE entity_relationship SET deleted = true WHERE (toId = :id AND toEntity = :entity) " + + "OR (fromId = :id AND fromEntity = :entity)") + void softDeleteAll(@Bind("id") String id, @Bind("entity") String entity); } interface FeedDAO { diff --git a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/DashboardRepository.java b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/DashboardRepository.java index 1c52772781f..18ed623c9d2 100644 --- a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/DashboardRepository.java +++ b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/DashboardRepository.java @@ -96,7 +96,7 @@ public class DashboardRepository extends EntityRepository { private EntityReference getService(Dashboard dashboard) throws IOException { EntityReference ref = - EntityUtil.getService(daoCollection.relationshipDAO(), dashboard.getId(), Entity.DASHBOARD_SERVICE); + EntityUtil.getService(daoCollection.relationshipDAO(), Entity.DASHBOARD, dashboard.getId(), Entity.DASHBOARD_SERVICE); if (ref != null) { DashboardService service = getService(ref.getId(), ref.getType()); ref.setName(service.getName()); @@ -189,7 +189,7 @@ public class DashboardRepository extends EntityRepository { } String dashboardId = dashboard.getId().toString(); List chartIds = - daoCollection.relationshipDAO().findTo(dashboardId, Relationship.HAS.ordinal(), Entity.CHART); + daoCollection.relationshipDAO().findTo(dashboardId, Entity.DASHBOARD, Relationship.HAS.ordinal(), Entity.CHART); List charts = new ArrayList<>(); for (String chartId : chartIds) { charts.add(daoCollection.chartDAO().findEntityReferenceById(UUID.fromString(chartId))); @@ -219,7 +219,7 @@ public class DashboardRepository extends EntityRepository { String dashboardId = updated.getId().toString(); // Remove all charts associated with this dashboard - daoCollection.relationshipDAO().deleteFrom(dashboardId, Relationship.HAS.ordinal(), Entity.CHART); + daoCollection.relationshipDAO().deleteFrom(dashboardId, Entity.DASHBOARD, Relationship.HAS.ordinal(), Entity.CHART); // Add relationship from dashboard to chart if (updated.getCharts() != null) { @@ -385,7 +385,7 @@ public class DashboardRepository extends EntityRepository { String dashboardId = updated.getId().toString(); // Remove all charts associated with this dashboard - daoCollection.relationshipDAO().deleteFrom(dashboardId, Relationship.HAS.ordinal(), Entity.CHART); + daoCollection.relationshipDAO().deleteFrom(dashboardId, Entity.DASHBOARD, Relationship.HAS.ordinal(), Entity.CHART); // Add relationship from dashboard to chart List updatedCharts = diff --git a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/DatabaseRepository.java b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/DatabaseRepository.java index 743fb6b3608..ba84d102e7e 100644 --- a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/DatabaseRepository.java +++ b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/DatabaseRepository.java @@ -60,7 +60,7 @@ public class DatabaseRepository extends EntityRepository { @Transaction public void deleteLocation(String databaseId) { - daoCollection.relationshipDAO().deleteFrom(databaseId, Relationship.HAS.ordinal(), Entity.LOCATION); + daoCollection.relationshipDAO().deleteFrom(databaseId, Entity.DATABASE, Relationship.HAS.ordinal(), Entity.LOCATION); } @Override @@ -110,7 +110,7 @@ public class DatabaseRepository extends EntityRepository { } String databaseId = database.getId().toString(); List tableIds = - daoCollection.relationshipDAO().findTo(databaseId, Relationship.CONTAINS.ordinal(), Entity.TABLE); + daoCollection.relationshipDAO().findTo(databaseId, Entity.DATABASE, Relationship.CONTAINS.ordinal(), Entity.TABLE); List tables = new ArrayList<>(); for (String tableId : tableIds) { tables.add(daoCollection.tableDAO().findEntityReferenceById(UUID.fromString(tableId))); @@ -149,7 +149,7 @@ public class DatabaseRepository extends EntityRepository { } String databaseId = database.getId().toString(); List result = - daoCollection.relationshipDAO().findTo(databaseId, Relationship.HAS.ordinal(), Entity.LOCATION); + daoCollection.relationshipDAO().findTo(databaseId, Entity.DATABASE, Relationship.HAS.ordinal(), Entity.LOCATION); if (result.size() == 1) { String locationId = result.get(0); return daoCollection.locationDAO().findEntityReferenceById(UUID.fromString(locationId)); @@ -160,7 +160,7 @@ public class DatabaseRepository extends EntityRepository { private EntityReference getService(Database database) throws IOException { EntityReference ref = - EntityUtil.getService(daoCollection.relationshipDAO(), database.getId(), Entity.DATABASE_SERVICE); + EntityUtil.getService(daoCollection.relationshipDAO(), Entity.DATABASE, database.getId(), Entity.DATABASE_SERVICE); if (ref != null) { DatabaseService service = getService(ref.getId(), ref.getType()); ref.setName(service.getName()); @@ -187,7 +187,8 @@ public class DatabaseRepository extends EntityRepository { daoCollection.databaseDAO().findEntityById(databaseId); daoCollection.locationDAO().findEntityById(locationId); // A database has only one location. - daoCollection.relationshipDAO().deleteFrom(databaseId.toString(), Relationship.HAS.ordinal(), Entity.LOCATION); + daoCollection.relationshipDAO().deleteFrom(databaseId.toString(), Entity.DATABASE, Relationship.HAS.ordinal(), + Entity.LOCATION); daoCollection .relationshipDAO() .insert( diff --git a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/EntityRepository.java b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/EntityRepository.java index e79cf4f4130..f4d6441a36d 100644 --- a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/EntityRepository.java +++ b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/EntityRepository.java @@ -374,7 +374,7 @@ public abstract class EntityRepository { public final void delete(UUID id, boolean recursive) throws IOException { // If an entity being deleted contains other children entities, it can't be deleted List contains = - daoCollection.relationshipDAO().findTo(id.toString(), Relationship.CONTAINS.ordinal()); + daoCollection.relationshipDAO().findTo(id.toString(), entityName,Relationship.CONTAINS.ordinal()); if (contains.size() > 0) { if (!recursive) { @@ -392,12 +392,12 @@ public abstract class EntityRepository { EntityInterface entityInterface = getEntityInterface(entity); entityInterface.setDeleted(true); storeEntity(entity, true); - daoCollection.relationshipDAO().softDeleteAll(id.toString()); + daoCollection.relationshipDAO().softDeleteAll(id.toString(), entityName); return; } // Hard delete dao.delete(id); - daoCollection.relationshipDAO().deleteAll(id.toString()); + daoCollection.relationshipDAO().deleteAll(id.toString(), entityName); } @Transaction @@ -409,7 +409,8 @@ public abstract class EntityRepository { User user = daoCollection.userDAO().findEntityById(userId); // Remove follower - daoCollection.relationshipDAO().delete(userId.toString(), entityId.toString(), Relationship.FOLLOWS.ordinal()); + daoCollection.relationshipDAO().delete(userId.toString(), Entity.USER, entityId.toString(), entityName, + Relationship.FOLLOWS.ordinal()); ChangeDescription change = new ChangeDescription().withPreviousVersion(entityInterface.getVersion()); change @@ -457,7 +458,7 @@ public abstract class EntityRepository { EntityInterface entityInterface = getEntityInterface(entity); return supportsOwner && entity != null ? EntityUtil.populateOwner( - entityInterface.getId(), daoCollection.relationshipDAO(), daoCollection.userDAO(), daoCollection.teamDAO()) + entityInterface.getId(), entityName, daoCollection.relationshipDAO(), daoCollection.userDAO(), daoCollection.teamDAO()) : null; } @@ -487,7 +488,7 @@ public abstract class EntityRepository { EntityInterface entityInterface = getEntityInterface(entity); return !supportsFollower || entity == null ? null - : EntityUtil.getFollowers(entityInterface.getId(), daoCollection.relationshipDAO(), daoCollection.userDAO()); + : EntityUtil.getFollowers(entityInterface.getId(), entityName, daoCollection.relationshipDAO(), daoCollection.userDAO()); } public T withHref(UriInfo uriInfo, T entity) { diff --git a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/FeedRepository.java b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/FeedRepository.java index cb100da668d..201b8dba4fa 100644 --- a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/FeedRepository.java +++ b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/FeedRepository.java @@ -50,7 +50,7 @@ public class FeedRepository { // Get owner for the addressed to Entity EntityReference owner = - EntityUtil.populateOwner(aboutRef.getId(), dao.relationshipDAO(), dao.userDAO(), dao.teamDAO()); + EntityUtil.populateOwner(aboutRef.getId(), aboutRef.getType(), dao.relationshipDAO(), dao.userDAO(), dao.teamDAO()); // Insert a new thread dao.feedDAO().insert(JsonUtils.pojoToJson(thread)); @@ -163,9 +163,9 @@ public class FeedRepository { // For a user entitylink get created or replied relationships to the thread if (reference.getType().equals(Entity.USER)) { threadIds.addAll( - dao.relationshipDAO().findTo(reference.getId().toString(), Relationship.CREATED.ordinal(), "thread")); + dao.relationshipDAO().findTo(reference.getId().toString(), reference.getType(), Relationship.CREATED.ordinal(), "thread")); threadIds.addAll( - dao.relationshipDAO().findTo(reference.getId().toString(), Relationship.REPLIED_TO.ordinal(), "thread")); + dao.relationshipDAO().findTo(reference.getId().toString(), reference.getType(), Relationship.REPLIED_TO.ordinal(), "thread")); } else { // Only data assets are added as about result = diff --git a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/IngestionRepository.java b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/IngestionRepository.java index 1e96aaf26d3..6bf29f01442 100644 --- a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/IngestionRepository.java +++ b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/IngestionRepository.java @@ -125,7 +125,7 @@ public class IngestionRepository extends EntityRepository { } private EntityReference getService(Ingestion ingestion) throws IOException { - EntityReference ref = EntityUtil.getService(daoCollection.relationshipDAO(), ingestion.getId()); + EntityReference ref = EntityUtil.getService(daoCollection.relationshipDAO(), Entity.INGESTION, ingestion.getId()); return getService(Objects.requireNonNull(ref)); } diff --git a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/LineageRepository.java b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/LineageRepository.java index 72b9fd80b0d..5bbeb819cce 100644 --- a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/LineageRepository.java +++ b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/LineageRepository.java @@ -73,8 +73,8 @@ public class LineageRepository { .withNodes(entities) .withUpstreamEdges(new ArrayList<>()) .withDownstreamEdges(new ArrayList<>()); - addUpstreamLineage(primary.getId(), lineage, upstreamDepth); - addDownstreamLineage(primary.getId(), lineage, downstreamDepth); + addUpstreamLineage(primary.getId(), primary.getType(), lineage, upstreamDepth); + addDownstreamLineage(primary.getId(), primary.getType(), lineage, downstreamDepth); // Remove duplicate nodes lineage.withNodes(lineage.getNodes().stream().distinct().collect(Collectors.toList())); @@ -88,35 +88,35 @@ public class LineageRepository { return lineage; } - private void addUpstreamLineage(UUID id, EntityLineage lineage, int upstreamDepth) { + private void addUpstreamLineage(UUID id, String entityType, EntityLineage lineage, int upstreamDepth) { if (upstreamDepth == 0) { return; } // from this id ---> find other ids List upstreamEntities = - dao.relationshipDAO().findFrom(id.toString(), Relationship.UPSTREAM.ordinal()); + dao.relationshipDAO().findFrom(id.toString(), entityType, Relationship.UPSTREAM.ordinal()); lineage.getNodes().addAll(upstreamEntities); upstreamDepth--; for (EntityReference upstreamEntity : upstreamEntities) { lineage.getUpstreamEdges().add(new Edge().withFromEntity(upstreamEntity.getId()).withToEntity(id)); - addUpstreamLineage(upstreamEntity.getId(), lineage, upstreamDepth); // Recursively add upstream nodes and edges + addUpstreamLineage(upstreamEntity.getId(), upstreamEntity.getType(), lineage, upstreamDepth); // Recursively add upstream nodes and edges } } - private void addDownstreamLineage(UUID id, EntityLineage lineage, int downstreamDepth) { + private void addDownstreamLineage(UUID id, String entityType, EntityLineage lineage, int downstreamDepth) { if (downstreamDepth == 0) { return; } // from other ids ---> to this id List downStreamEntities = - dao.relationshipDAO().findTo(id.toString(), Relationship.UPSTREAM.ordinal()); + dao.relationshipDAO().findTo(id.toString(), entityType, Relationship.UPSTREAM.ordinal()); lineage.getNodes().addAll(downStreamEntities); downstreamDepth--; for (EntityReference entity : downStreamEntities) { lineage.getDownstreamEdges().add(new Edge().withToEntity(entity.getId()).withFromEntity(id)); - addDownstreamLineage(entity.getId(), lineage, downstreamDepth); + addDownstreamLineage(entity.getId(), entity.getType(), lineage, downstreamDepth); } } } diff --git a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/LocationRepository.java b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/LocationRepository.java index 164960219e9..49793d2a649 100644 --- a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/LocationRepository.java +++ b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/LocationRepository.java @@ -218,7 +218,7 @@ public class LocationRepository extends EntityRepository { private EntityReference getService(Location location) throws IOException { EntityReference ref = - EntityUtil.getService(daoCollection.relationshipDAO(), location.getId(), Entity.STORAGE_SERVICE); + EntityUtil.getService(daoCollection.relationshipDAO(), Entity.LOCATION, location.getId(), Entity.STORAGE_SERVICE); return getService(Objects.requireNonNull(ref)); } diff --git a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/MetricsRepository.java b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/MetricsRepository.java index 5bd7f276c6e..b00f8c1e41e 100644 --- a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/MetricsRepository.java +++ b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/MetricsRepository.java @@ -110,7 +110,7 @@ public class MetricsRepository extends EntityRepository { private EntityReference getService(Metrics metrics) throws IOException { // Get service by metrics ID EntityReference ref = - EntityUtil.getService(daoCollection.relationshipDAO(), metrics.getId(), Entity.DASHBOARD_SERVICE); + EntityUtil.getService(daoCollection.relationshipDAO(), Entity.METRICS, metrics.getId(), Entity.DASHBOARD_SERVICE); return getService(Objects.requireNonNull(ref)); } diff --git a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/MlModelRepository.java b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/MlModelRepository.java index cd0cc521c66..d3a8a3d011e 100644 --- a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/MlModelRepository.java +++ b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/MlModelRepository.java @@ -203,7 +203,7 @@ public class MlModelRepository extends EntityRepository { private EntityReference getDashboard(MlModel mlModel) throws IOException { if (mlModel != null) { List ids = - daoCollection.relationshipDAO().findTo(mlModel.getId().toString(), Relationship.USES.ordinal()); + daoCollection.relationshipDAO().findTo(mlModel.getId().toString(), Entity.MLMODEL, Relationship.USES.ordinal()); if (ids.size() > 1) { LOG.warn("Possible database issues - multiple dashboards {} found for model {}", ids, mlModel.getId()); } @@ -231,7 +231,7 @@ public class MlModelRepository extends EntityRepository { public void removeDashboard(MlModel mlModel) { daoCollection .relationshipDAO() - .deleteFrom(mlModel.getId().toString(), Relationship.USES.ordinal(), Entity.DASHBOARD); + .deleteFrom(mlModel.getId().toString(), Entity.MLMODEL, Relationship.USES.ordinal(), Entity.DASHBOARD); } public static class MlModelEntityInterface implements EntityInterface { @@ -439,7 +439,7 @@ public class MlModelRepository extends EntityRepository { // Remove the dashboard associated with the model, if any String modelId = updatedModel.getId().toString(); if (origModel.getDashboard() != null) { - daoCollection.relationshipDAO().deleteFrom(modelId, Relationship.USES.ordinal(), "dashboard"); + daoCollection.relationshipDAO().deleteFrom(modelId, Entity.MLMODEL, Relationship.USES.ordinal(), "dashboard"); } // Add relationship from model to dashboard diff --git a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/PipelineRepository.java b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/PipelineRepository.java index b9da2833244..c3028928f5c 100644 --- a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/PipelineRepository.java +++ b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/PipelineRepository.java @@ -147,7 +147,7 @@ public class PipelineRepository extends EntityRepository { private EntityReference getService(Pipeline pipeline) throws IOException { EntityReference ref = - EntityUtil.getService(daoCollection.relationshipDAO(), pipeline.getId(), Entity.PIPELINE_SERVICE); + EntityUtil.getService(daoCollection.relationshipDAO(), Entity.PIPELINE, pipeline.getId(), Entity.PIPELINE_SERVICE); PipelineService service = getService(ref.getId(), ref.getType()); ref.setName(service.getName()); ref.setDescription(service.getDescription()); diff --git a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/PolicyRepository.java b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/PolicyRepository.java index 6cad209ccbd..9fff03636c4 100644 --- a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/PolicyRepository.java +++ b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/PolicyRepository.java @@ -67,7 +67,7 @@ public class PolicyRepository extends EntityRepository { @Transaction private EntityReference getLocationForPolicy(UUID policyId) throws IOException { List result = - daoCollection.relationshipDAO().findTo(policyId.toString(), Relationship.APPLIED_TO.ordinal(), Entity.LOCATION); + daoCollection.relationshipDAO().findTo(policyId.toString(), Entity.POLICY, Relationship.APPLIED_TO.ordinal(), Entity.LOCATION); // There is at most one location for a policy. return result.size() == 1 ? daoCollection.locationDAO().findEntityReferenceById(UUID.fromString(result.get(0))) @@ -340,7 +340,9 @@ public class PolicyRepository extends EntityRepository { .relationshipDAO() .delete( origPolicy.getId().toString(), + Entity.POLICY, origPolicy.getLocation().getId().toString(), + Entity.LOCATION, Relationship.APPLIED_TO.ordinal()); } // insert updated Policy --> Location relationship. diff --git a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/ReportRepository.java b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/ReportRepository.java index f49bcf59606..87480a73ed1 100644 --- a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/ReportRepository.java +++ b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/ReportRepository.java @@ -81,7 +81,7 @@ public class ReportRepository extends EntityRepository { } private EntityReference getService(Report report) { - return report == null ? null : getService(EntityUtil.getService(daoCollection.relationshipDAO(), report.getId())); + return report == null ? null : getService(EntityUtil.getService(daoCollection.relationshipDAO(), Entity.REPORT, report.getId())); } private EntityReference getService(EntityReference service) { diff --git a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/TableRepository.java b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/TableRepository.java index 35e27e5a763..b002777f7b7 100644 --- a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/TableRepository.java +++ b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/TableRepository.java @@ -206,7 +206,7 @@ public class TableRepository extends EntityRepository { Table table = daoCollection.tableDAO().findEntityById(tableId); EntityReference location = daoCollection.locationDAO().findEntityReferenceById(locationId); // A table has only one location. - daoCollection.relationshipDAO().deleteFrom(tableId.toString(), Relationship.HAS.ordinal(), Entity.LOCATION); + daoCollection.relationshipDAO().deleteFrom(tableId.toString(), Entity.TABLE, Relationship.HAS.ordinal(), Entity.LOCATION); daoCollection .relationshipDAO() .insert(tableId.toString(), locationId.toString(), Entity.TABLE, Entity.LOCATION, Relationship.HAS.ordinal()); @@ -270,7 +270,7 @@ public class TableRepository extends EntityRepository
{ @Transaction public void deleteLocation(String tableId) { - daoCollection.relationshipDAO().deleteFrom(tableId, Relationship.HAS.ordinal(), Entity.LOCATION); + daoCollection.relationshipDAO().deleteFrom(tableId, Entity.TABLE, Relationship.HAS.ordinal(), Entity.LOCATION); } @Transaction @@ -330,7 +330,7 @@ public class TableRepository extends EntityRepository
{ private EntityReference getService(Table table) throws IOException { EntityReference ref = - EntityUtil.getService(daoCollection.relationshipDAO(), table.getDatabase().getId(), Entity.DATABASE_SERVICE); + EntityUtil.getService(daoCollection.relationshipDAO(), Entity.DATABASE, table.getDatabase().getId(), Entity.DATABASE_SERVICE); DatabaseService service = getService(ref.getId(), ref.getType()); ref.setName(service.getName()); ref.setDescription(service.getDescription()); @@ -342,7 +342,7 @@ public class TableRepository extends EntityRepository
{ String serviceId = daoCollection .relationshipDAO() - .findFrom(table.getDatabase().getId().toString(), Relationship.CONTAINS.ordinal(), Entity.DATABASE_SERVICE) + .findFrom(table.getDatabase().getId().toString(), Entity.DATABASE, Relationship.CONTAINS.ordinal(), Entity.DATABASE_SERVICE) .get(0); DatabaseService service = daoCollection.dbServiceDAO().findEntityById(UUID.fromString(serviceId)); table.setService(new DatabaseServiceEntityInterface(service).getEntityReference()); @@ -436,7 +436,7 @@ public class TableRepository extends EntityRepository
{ private EntityReference getDatabase(UUID tableId) throws IOException { // Find database for the table List result = - daoCollection.relationshipDAO().findFrom(tableId.toString(), Relationship.CONTAINS.ordinal(), Entity.DATABASE); + daoCollection.relationshipDAO().findFrom(tableId.toString(), Entity.TABLE, Relationship.CONTAINS.ordinal(), Entity.DATABASE); if (result.size() != 1) { throw EntityNotFoundException.byMessage(String.format("Database for table %s Not found", tableId)); } @@ -446,7 +446,7 @@ public class TableRepository extends EntityRepository
{ private EntityReference getLocation(UUID tableId) throws IOException { // Find the location of the table List result = - daoCollection.relationshipDAO().findTo(tableId.toString(), Relationship.HAS.ordinal(), Entity.LOCATION); + daoCollection.relationshipDAO().findTo(tableId.toString(), Entity.TABLE, Relationship.HAS.ordinal(), Entity.LOCATION); if (result.size() == 1) { return daoCollection.locationDAO().findEntityReferenceById(UUID.fromString(result.get(0))); } else { diff --git a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/TeamRepository.java b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/TeamRepository.java index 9b0b30783b1..d6a0bdda916 100644 --- a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/TeamRepository.java +++ b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/TeamRepository.java @@ -129,7 +129,7 @@ public class TeamRepository extends EntityRepository { } private List getUsers(String id) throws IOException { - List userIds = daoCollection.relationshipDAO().findTo(id, Relationship.HAS.ordinal(), "user"); + List userIds = daoCollection.relationshipDAO().findTo(id, Entity.TEAM, Relationship.HAS.ordinal(), "user"); List users = new ArrayList<>(); for (String userId : userIds) { users.add(daoCollection.userDAO().findEntityReferenceById(UUID.fromString(userId))); @@ -139,7 +139,7 @@ public class TeamRepository extends EntityRepository { private List getOwns(String teamId) throws IOException { // Compile entities owned by the team - return EntityUtil.populateEntityReferences(daoCollection.relationshipDAO().findTo(teamId, OWNS.ordinal())); + return EntityUtil.populateEntityReferences(daoCollection.relationshipDAO().findTo(teamId, Entity.TEAM, OWNS.ordinal())); } public static class TeamEntityInterface implements EntityInterface { @@ -272,7 +272,7 @@ public class TeamRepository extends EntityRepository { List deleted = new ArrayList<>(); if (recordListChange("users", origUsers, updatedUsers, added, deleted, entityReferenceMatch)) { // Remove users from original and add users from updated - daoCollection.relationshipDAO().deleteFrom(origTeam.getId().toString(), Relationship.HAS.ordinal(), "user"); + daoCollection.relationshipDAO().deleteFrom(origTeam.getId().toString(), Entity.TEAM, Relationship.HAS.ordinal(), "user"); // Add relationships for (EntityReference user : updatedUsers) { daoCollection diff --git a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/TopicRepository.java b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/TopicRepository.java index 98e430b0099..714d67c1912 100644 --- a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/TopicRepository.java +++ b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/TopicRepository.java @@ -124,7 +124,7 @@ public class TopicRepository extends EntityRepository { return null; } // Find service by topic Id - EntityReference service = EntityUtil.getService(daoCollection.relationshipDAO(), topic.getId()); + EntityReference service = EntityUtil.getService(daoCollection.relationshipDAO(), Entity.TOPIC, topic.getId()); return new MessagingServiceEntityInterface(getService(service.getId(), service.getType())).getEntityReference(); } diff --git a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/UsageRepository.java b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/UsageRepository.java index 4a2a6d6372c..89248321cb8 100644 --- a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/UsageRepository.java +++ b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/UsageRepository.java @@ -78,7 +78,7 @@ public class UsageRepository { // If table usage was reported, add the usage count to database if (entityType.equalsIgnoreCase(Entity.TABLE)) { List databaseIds = - dao.relationshipDAO().findFrom(entityId, Relationship.CONTAINS.ordinal(), Entity.DATABASE); + dao.relationshipDAO().findFrom(entityId, entityType, Relationship.CONTAINS.ordinal(), Entity.DATABASE); dao.usageDAO().insertOrUpdateCount(usage.getDate(), databaseIds.get(0), Entity.DATABASE, usage.getCount()); } } diff --git a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/UserRepository.java b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/UserRepository.java index d9a83332be2..3df48a337f6 100644 --- a/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/UserRepository.java +++ b/catalog-rest-service/src/main/java/org/openmetadata/catalog/jdbi3/UserRepository.java @@ -114,12 +114,12 @@ public class UserRepository extends EntityRepository { private List getOwns(User user) throws IOException { // Compile entities owned by the user List ownedEntities = - daoCollection.relationshipDAO().findTo(user.getId().toString(), OWNS.ordinal()); + daoCollection.relationshipDAO().findTo(user.getId().toString(), Entity.USER, OWNS.ordinal()); // Compile entities owned by the team the user belongs to List teams = user.getTeams() == null ? getTeams(user) : user.getTeams(); for (EntityReference team : teams) { - ownedEntities.addAll(daoCollection.relationshipDAO().findTo(team.getId().toString(), OWNS.ordinal())); + ownedEntities.addAll(daoCollection.relationshipDAO().findTo(team.getId().toString(), Entity.TEAM, OWNS.ordinal())); } // Populate details in entity reference return EntityUtil.populateEntityReferences(ownedEntities); @@ -127,7 +127,7 @@ public class UserRepository extends EntityRepository { private List getFollows(User user) throws IOException { return EntityUtil.populateEntityReferences( - daoCollection.relationshipDAO().findTo(user.getId().toString(), FOLLOWS.ordinal())); + daoCollection.relationshipDAO().findTo(user.getId().toString(), Entity.USER, FOLLOWS.ordinal())); } public List validateRoles(List roleIds) throws IOException { @@ -154,7 +154,7 @@ public class UserRepository extends EntityRepository { /* Add all the roles that user has been assigned, to User entity */ private List getRoles(User user) throws IOException { - List roleIds = daoCollection.relationshipDAO().findTo(user.getId().toString(), HAS.ordinal(), Entity.ROLE); + List roleIds = daoCollection.relationshipDAO().findTo(user.getId().toString(), Entity.USER, HAS.ordinal(), Entity.ROLE); List roles = new ArrayList<>(roleIds.size()); for (String roleId : roleIds) { roles.add(daoCollection.roleDAO().findEntityReferenceById(UUID.fromString(roleId))); @@ -165,7 +165,7 @@ public class UserRepository extends EntityRepository { /* Add all the teams that user belongs to User entity */ private List getTeams(User user) throws IOException { List teamIds = - daoCollection.relationshipDAO().findFrom(user.getId().toString(), HAS.ordinal(), Entity.TEAM); + daoCollection.relationshipDAO().findFrom(user.getId().toString(), Entity.USER, HAS.ordinal(), Entity.TEAM); List teams = new ArrayList<>(); for (String teamId : teamIds) { teams.add(daoCollection.teamDAO().findEntityReferenceById(UUID.fromString(teamId))); @@ -321,7 +321,7 @@ public class UserRepository extends EntityRepository { private void updateRoles(User origUser, User updatedUser) throws JsonProcessingException { // Remove roles from original and add roles from updated - daoCollection.relationshipDAO().deleteFrom(origUser.getId().toString(), HAS.ordinal(), Entity.ROLE); + daoCollection.relationshipDAO().deleteFrom(origUser.getId().toString(), Entity.USER, HAS.ordinal(), Entity.ROLE); assignRoles(updatedUser, updatedUser.getRoles()); List origRoles = Optional.ofNullable(origUser.getRoles()).orElse(Collections.emptyList()); @@ -337,7 +337,7 @@ public class UserRepository extends EntityRepository { private void updateTeams(User origUser, User updatedUser) throws JsonProcessingException { // Remove teams from original and add teams from updated - daoCollection.relationshipDAO().deleteTo(origUser.getId().toString(), HAS.ordinal(), Entity.TEAM); + daoCollection.relationshipDAO().deleteTo(origUser.getId().toString(), Entity.USER, HAS.ordinal(), Entity.TEAM); assignTeams(updatedUser, updatedUser.getTeams()); List origTeams = Optional.ofNullable(origUser.getTeams()).orElse(Collections.emptyList()); diff --git a/catalog-rest-service/src/main/java/org/openmetadata/catalog/util/EntityUtil.java b/catalog-rest-service/src/main/java/org/openmetadata/catalog/util/EntityUtil.java index 04b813ad5da..ffbc8850983 100644 --- a/catalog-rest-service/src/main/java/org/openmetadata/catalog/util/EntityUtil.java +++ b/catalog-rest-service/src/main/java/org/openmetadata/catalog/util/EntityUtil.java @@ -152,8 +152,8 @@ public final class EntityUtil { return entity; } - public static EntityReference getService(EntityRelationshipDAO dao, UUID entityId) { - List refs = dao.findFrom(entityId.toString(), Relationship.CONTAINS.ordinal()); + public static EntityReference getService(EntityRelationshipDAO dao, String entityType, UUID entityId) { + List refs = dao.findFrom(entityId.toString(), entityType, Relationship.CONTAINS.ordinal()); if (refs.size() > 1) { LOG.warn("Possible database issues - multiple services found for entity {}", entityId); return refs.get(0); @@ -161,8 +161,8 @@ public final class EntityUtil { return refs.isEmpty() ? null : refs.get(0); } - public static EntityReference getService(EntityRelationshipDAO dao, UUID entityId, String serviceType) { - List refs = dao.findFromEntity(entityId.toString(), Relationship.CONTAINS.ordinal(), serviceType); + public static EntityReference getService(EntityRelationshipDAO dao, String entityType, UUID entityId, String serviceType) { + List refs = dao.findFromEntity(entityId.toString(), entityType, Relationship.CONTAINS.ordinal(), serviceType); if (refs.size() > 1) { LOG.warn("Possible database issues - multiple services found for entity {}", entityId); return refs.get(0); @@ -178,8 +178,8 @@ public final class EntityUtil { // Get owner for a given entity public static EntityReference populateOwner( - UUID id, EntityRelationshipDAO entityRelationshipDAO, UserDAO userDAO, TeamDAO teamDAO) throws IOException { - List ids = entityRelationshipDAO.findFrom(id.toString(), Relationship.OWNS.ordinal()); + UUID id, String entityType, EntityRelationshipDAO entityRelationshipDAO, UserDAO userDAO, TeamDAO teamDAO) throws IOException { + List ids = entityRelationshipDAO.findFrom(id.toString(), entityType, Relationship.OWNS.ordinal()); if (ids.size() > 1) { LOG.warn("Possible database issues - multiple owners {} found for entity {}", ids, id); } @@ -223,10 +223,10 @@ public final class EntityUtil { } /** Unassign owner relationship for a given entity */ - public static void unassignOwner(EntityRelationshipDAO dao, EntityReference owner, String ownedEntityId) { + public static void unassignOwner(EntityRelationshipDAO dao, EntityReference owner, String ownedEntityId, String ownedEntityType) { if (owner != null && owner.getId() != null) { LOG.info("Removing owner {}:{} for entity {}", owner.getType(), owner.getId(), ownedEntityId); - dao.delete(owner.getId().toString(), ownedEntityId, Relationship.OWNS.ordinal()); + dao.delete(owner.getId().toString(), owner.getType(), ownedEntityId, ownedEntityType, Relationship.OWNS.ordinal()); } } @@ -238,7 +238,7 @@ public final class EntityUtil { String ownedEntityType) { // TODO inefficient use replace instead of delete and add? // TODO check for orig and new owners being the same - unassignOwner(dao, originalOwner, ownedEntityId.toString()); + unassignOwner(dao, originalOwner, ownedEntityId.toString(), ownedEntityType); setOwner(dao, ownedEntityId, ownedEntityType, newOwner); } @@ -372,14 +372,10 @@ public final class EntityUtil { > 0; } - public static void removeFollower(EntityRelationshipDAO dao, UUID followedEntityId, UUID followerId) { - dao.delete(followerId.toString(), followedEntityId.toString(), Relationship.FOLLOWS.ordinal()); - } - public static List getFollowers( - UUID followedEntityId, EntityRelationshipDAO entityRelationshipDAO, UserDAO userDAO) throws IOException { + UUID followedEntityId, String entityName, EntityRelationshipDAO entityRelationshipDAO, UserDAO userDAO) throws IOException { List followerIds = - entityRelationshipDAO.findFrom(followedEntityId.toString(), Relationship.FOLLOWS.ordinal(), Entity.USER); + entityRelationshipDAO.findFrom(followedEntityId.toString(), entityName, Relationship.FOLLOWS.ordinal(), Entity.USER); List followers = new ArrayList<>(); for (String followerId : followerIds) { User user = userDAO.findEntityById(UUID.fromString(followerId));