diff --git a/cognee/infrastructure/databases/graph/get_graph_engine.py b/cognee/infrastructure/databases/graph/get_graph_engine.py index e579ab66b..08117f408 100644 --- a/cognee/infrastructure/databases/graph/get_graph_engine.py +++ b/cognee/infrastructure/databases/graph/get_graph_engine.py @@ -16,6 +16,11 @@ async def get_graph_engine() -> GraphDBInterface: # Async functions can't be cached. After creating and caching the graph engine # handle all necessary async operations for different graph types bellow. + + # Run any adapter‐specific async initialization + if hasattr(graph_client, "initialize"): + await graph_client.initialize() + # Handle loading of graph for NetworkX if config["graph_database_provider"].lower() == "networkx" and graph_client.graph is None: await graph_client.load_graph_from_file() diff --git a/cognee/infrastructure/databases/graph/neo4j_driver/adapter.py b/cognee/infrastructure/databases/graph/neo4j_driver/adapter.py index 816888dae..f9a759500 100644 --- a/cognee/infrastructure/databases/graph/neo4j_driver/adapter.py +++ b/cognee/infrastructure/databases/graph/neo4j_driver/adapter.py @@ -1,5 +1,3 @@ -# - """Neo4j Adapter for Graph Database""" import json @@ -52,8 +50,12 @@ class Neo4jAdapter(GraphDBInterface): max_connection_lifetime=120, notifications_min_severity="OFF", ) - # Create contraint/index - self.query( + + async def initialize(self) -> None: + """ + Initializes the database: adds uniqueness constraint on id and performs indexing + """ + await self.query( (f"CREATE CONSTRAINT IF NOT EXISTS FOR (n:`{BASE_LABEL}`) REQUIRE n.id IS UNIQUE;") )