Skip to content

Commit e3b0606

Browse files
committed
fix: auto create bug
1 parent b5546b4 commit e3b0606

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/memos/graph_dbs/nebular.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,19 @@ def _get_or_create_shared_client(cls, cfg: NebulaGraphDBConfig) -> tuple[str, "N
188188
client = cls._CLIENT_CACHE.get(key)
189189
if client is None:
190190
# Connection setting
191+
192+
tmp_client = NebulaClient(
193+
hosts=cfg.uri,
194+
username=cfg.user,
195+
password=cfg.password,
196+
session_config=SessionConfig(graph=None),
197+
session_pool_config=SessionPoolConfig(size=1, wait_timeout=3000),
198+
)
199+
try:
200+
cls._ensure_space_exists(tmp_client, cfg)
201+
finally:
202+
tmp_client.close()
203+
191204
conn_conf: ConnectionConfig | None = getattr(cfg, "conn_config", None)
192205
if conn_conf is None:
193206
conn_conf = ConnectionConfig.from_defults(
@@ -1467,6 +1480,25 @@ def merge_nodes(self, id1: str, id2: str) -> str:
14671480
"""
14681481
raise NotImplementedError
14691482

1483+
@classmethod
1484+
def _ensure_space_exists(cls, tmp_client, cfg):
1485+
"""Lightweight check to ensure target graph (space) exists."""
1486+
db_name = getattr(cfg, "space", None)
1487+
if not db_name:
1488+
logger.warning("[NebulaGraphDBSync] No `space` specified in cfg.")
1489+
return
1490+
1491+
try:
1492+
res = tmp_client.execute("SHOW GRAPHS;")
1493+
existing = {row.values()[0].as_string() for row in res}
1494+
if db_name not in existing:
1495+
tmp_client.execute(f"CREATE GRAPH IF NOT EXISTS `{db_name}` TYPED MemOSBgeM3Type;")
1496+
logger.info(f"✅ Graph `{db_name}` created before session binding.")
1497+
else:
1498+
logger.debug(f"Graph `{db_name}` already exists.")
1499+
except Exception:
1500+
logger.exception("[NebulaGraphDBSync] Failed to ensure space exists")
1501+
14701502
@timed
14711503
def _ensure_database_exists(self):
14721504
graph_type_name = "MemOSBgeM3Type"

0 commit comments

Comments
 (0)