From 82c0279f4591328c50aa70020019f89a8add2f91 Mon Sep 17 00:00:00 2001 From: hajdul88 <52442977+hajdul88@users.noreply.github.com> Date: Mon, 9 Jun 2025 13:40:30 +0200 Subject: [PATCH] Fix: adds and invokes Neo4jAdapter initialize for schema setup (#933) ## Description adds and invokes Neo4jAdapter initialize for schema setup ## DCO Affirmation I affirm that all code in every commit of this pull request conforms to the terms of the Topoteretes Developer Certificate of Origin. --- .../infrastructure/databases/graph/get_graph_engine.py | 5 +++++ .../databases/graph/neo4j_driver/adapter.py | 10 ++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) 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;") )