yangdx
commited on
Commit
·
edeea45
1
Parent(s):
3a1bf1f
Update drop implementation for all storage type of PostgreSQL
Browse files- lightrag/kg/postgres_impl.py +59 -51
lightrag/kg/postgres_impl.py
CHANGED
@@ -380,10 +380,20 @@ class PGKVStorage(BaseKVStorage):
|
|
380 |
# PG handles persistence automatically
|
381 |
pass
|
382 |
|
383 |
-
async def drop(self) ->
|
384 |
"""Drop the storage"""
|
385 |
-
|
386 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
387 |
|
388 |
|
389 |
@final
|
@@ -690,6 +700,21 @@ class PGVectorStorage(BaseVectorStorage):
|
|
690 |
logger.error(f"Error retrieving vector data for IDs {ids}: {e}")
|
691 |
return []
|
692 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
693 |
|
694 |
@final
|
695 |
@dataclass
|
@@ -846,10 +871,20 @@ class PGDocStatusStorage(DocStatusStorage):
|
|
846 |
},
|
847 |
)
|
848 |
|
849 |
-
async def drop(self) ->
|
850 |
"""Drop the storage"""
|
851 |
-
|
852 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
853 |
|
854 |
|
855 |
class PGGraphQueryException(Exception):
|
@@ -1530,12 +1565,19 @@ class PGGraphStorage(BaseGraphStorage):
|
|
1530 |
|
1531 |
return kg
|
1532 |
|
1533 |
-
async def drop(self) ->
|
1534 |
"""Drop the storage"""
|
1535 |
-
|
1536 |
-
|
1537 |
-
|
1538 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1539 |
|
1540 |
|
1541 |
NAMESPACE_TABLE_MAP = {
|
@@ -1693,6 +1735,7 @@ SQL_TEMPLATES = {
|
|
1693 |
file_path=EXCLUDED.file_path,
|
1694 |
update_time = CURRENT_TIMESTAMP
|
1695 |
""",
|
|
|
1696 |
"upsert_entity": """INSERT INTO LIGHTRAG_VDB_ENTITY (workspace, id, entity_name, content,
|
1697 |
content_vector, chunk_ids, file_path)
|
1698 |
VALUES ($1, $2, $3, $4, $5, $6::varchar[], $7)
|
@@ -1715,46 +1758,7 @@ SQL_TEMPLATES = {
|
|
1715 |
chunk_ids=EXCLUDED.chunk_ids,
|
1716 |
file_path=EXCLUDED.file_path,
|
1717 |
update_time = CURRENT_TIMESTAMP
|
1718 |
-
""",
|
1719 |
-
# SQL for VectorStorage
|
1720 |
-
# "entities": """SELECT entity_name FROM
|
1721 |
-
# (SELECT id, entity_name, 1 - (content_vector <=> '[{embedding_string}]'::vector) as distance
|
1722 |
-
# FROM LIGHTRAG_VDB_ENTITY where workspace=$1)
|
1723 |
-
# WHERE distance>$2 ORDER BY distance DESC LIMIT $3
|
1724 |
-
# """,
|
1725 |
-
# "relationships": """SELECT source_id as src_id, target_id as tgt_id FROM
|
1726 |
-
# (SELECT id, source_id,target_id, 1 - (content_vector <=> '[{embedding_string}]'::vector) as distance
|
1727 |
-
# FROM LIGHTRAG_VDB_RELATION where workspace=$1)
|
1728 |
-
# WHERE distance>$2 ORDER BY distance DESC LIMIT $3
|
1729 |
-
# """,
|
1730 |
-
# "chunks": """SELECT id FROM
|
1731 |
-
# (SELECT id, 1 - (content_vector <=> '[{embedding_string}]'::vector) as distance
|
1732 |
-
# FROM LIGHTRAG_DOC_CHUNKS where workspace=$1)
|
1733 |
-
# WHERE distance>$2 ORDER BY distance DESC LIMIT $3
|
1734 |
-
# """,
|
1735 |
-
# DROP tables
|
1736 |
-
"drop_all": """
|
1737 |
-
DROP TABLE IF EXISTS LIGHTRAG_DOC_FULL CASCADE;
|
1738 |
-
DROP TABLE IF EXISTS LIGHTRAG_DOC_CHUNKS CASCADE;
|
1739 |
-
DROP TABLE IF EXISTS LIGHTRAG_LLM_CACHE CASCADE;
|
1740 |
-
DROP TABLE IF EXISTS LIGHTRAG_VDB_ENTITY CASCADE;
|
1741 |
-
DROP TABLE IF EXISTS LIGHTRAG_VDB_RELATION CASCADE;
|
1742 |
-
""",
|
1743 |
-
"drop_doc_full": """
|
1744 |
-
DROP TABLE IF EXISTS LIGHTRAG_DOC_FULL CASCADE;
|
1745 |
-
""",
|
1746 |
-
"drop_doc_chunks": """
|
1747 |
-
DROP TABLE IF EXISTS LIGHTRAG_DOC_CHUNKS CASCADE;
|
1748 |
-
""",
|
1749 |
-
"drop_llm_cache": """
|
1750 |
-
DROP TABLE IF EXISTS LIGHTRAG_LLM_CACHE CASCADE;
|
1751 |
-
""",
|
1752 |
-
"drop_vdb_entity": """
|
1753 |
-
DROP TABLE IF EXISTS LIGHTRAG_VDB_ENTITY CASCADE;
|
1754 |
-
""",
|
1755 |
-
"drop_vdb_relation": """
|
1756 |
-
DROP TABLE IF EXISTS LIGHTRAG_VDB_RELATION CASCADE;
|
1757 |
-
""",
|
1758 |
"relationships": """
|
1759 |
WITH relevant_chunks AS (
|
1760 |
SELECT id as chunk_id
|
@@ -1806,4 +1810,8 @@ SQL_TEMPLATES = {
|
|
1806 |
ORDER BY distance DESC
|
1807 |
LIMIT $3
|
1808 |
""",
|
|
|
|
|
|
|
|
|
1809 |
}
|
|
|
380 |
# PG handles persistence automatically
|
381 |
pass
|
382 |
|
383 |
+
async def drop(self) -> dict[str, str]:
|
384 |
"""Drop the storage"""
|
385 |
+
try:
|
386 |
+
table_name = namespace_to_table_name(self.namespace)
|
387 |
+
if not table_name:
|
388 |
+
return {"status": "error", "message": f"Unknown namespace: {self.namespace}"}
|
389 |
+
|
390 |
+
drop_sql = SQL_TEMPLATES["drop_specifiy_table_workspace"].format(
|
391 |
+
table_name=table_name
|
392 |
+
)
|
393 |
+
await self.db.execute(drop_sql, {"workspace": self.db.workspace})
|
394 |
+
return {"status": "success", "message": "data dropped"}
|
395 |
+
except Exception as e:
|
396 |
+
return {"status": "error", "message": str(e)}
|
397 |
|
398 |
|
399 |
@final
|
|
|
700 |
logger.error(f"Error retrieving vector data for IDs {ids}: {e}")
|
701 |
return []
|
702 |
|
703 |
+
async def drop(self) -> dict[str, str]:
|
704 |
+
"""Drop the storage"""
|
705 |
+
try:
|
706 |
+
table_name = namespace_to_table_name(self.namespace)
|
707 |
+
if not table_name:
|
708 |
+
return {"status": "error", "message": f"Unknown namespace: {self.namespace}"}
|
709 |
+
|
710 |
+
drop_sql = SQL_TEMPLATES["drop_specifiy_table_workspace"].format(
|
711 |
+
table_name=table_name
|
712 |
+
)
|
713 |
+
await self.db.execute(drop_sql, {"workspace": self.db.workspace})
|
714 |
+
return {"status": "success", "message": "data dropped"}
|
715 |
+
except Exception as e:
|
716 |
+
return {"status": "error", "message": str(e)}
|
717 |
+
|
718 |
|
719 |
@final
|
720 |
@dataclass
|
|
|
871 |
},
|
872 |
)
|
873 |
|
874 |
+
async def drop(self) -> dict[str, str]:
|
875 |
"""Drop the storage"""
|
876 |
+
try:
|
877 |
+
table_name = namespace_to_table_name(self.namespace)
|
878 |
+
if not table_name:
|
879 |
+
return {"status": "error", "message": f"Unknown namespace: {self.namespace}"}
|
880 |
+
|
881 |
+
drop_sql = SQL_TEMPLATES["drop_specifiy_table_workspace"].format(
|
882 |
+
table_name=table_name
|
883 |
+
)
|
884 |
+
await self.db.execute(drop_sql, {"workspace": self.db.workspace})
|
885 |
+
return {"status": "success", "message": "data dropped"}
|
886 |
+
except Exception as e:
|
887 |
+
return {"status": "error", "message": str(e)}
|
888 |
|
889 |
|
890 |
class PGGraphQueryException(Exception):
|
|
|
1565 |
|
1566 |
return kg
|
1567 |
|
1568 |
+
async def drop(self) -> dict[str, str]:
|
1569 |
"""Drop the storage"""
|
1570 |
+
try:
|
1571 |
+
drop_query = f"""SELECT * FROM cypher('{self.graph_name}', $$
|
1572 |
+
MATCH (n)
|
1573 |
+
DETACH DELETE n
|
1574 |
+
$$) AS (result agtype)"""
|
1575 |
+
|
1576 |
+
await self._query(drop_query, readonly=False)
|
1577 |
+
return {"status": "success", "message": "graph data dropped"}
|
1578 |
+
except Exception as e:
|
1579 |
+
logger.error(f"Error dropping graph: {e}")
|
1580 |
+
return {"status": "error", "message": str(e)}
|
1581 |
|
1582 |
|
1583 |
NAMESPACE_TABLE_MAP = {
|
|
|
1735 |
file_path=EXCLUDED.file_path,
|
1736 |
update_time = CURRENT_TIMESTAMP
|
1737 |
""",
|
1738 |
+
# SQL for VectorStorage
|
1739 |
"upsert_entity": """INSERT INTO LIGHTRAG_VDB_ENTITY (workspace, id, entity_name, content,
|
1740 |
content_vector, chunk_ids, file_path)
|
1741 |
VALUES ($1, $2, $3, $4, $5, $6::varchar[], $7)
|
|
|
1758 |
chunk_ids=EXCLUDED.chunk_ids,
|
1759 |
file_path=EXCLUDED.file_path,
|
1760 |
update_time = CURRENT_TIMESTAMP
|
1761 |
+
""",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1762 |
"relationships": """
|
1763 |
WITH relevant_chunks AS (
|
1764 |
SELECT id as chunk_id
|
|
|
1810 |
ORDER BY distance DESC
|
1811 |
LIMIT $3
|
1812 |
""",
|
1813 |
+
# DROP tables
|
1814 |
+
"drop_specifiy_table_workspace": """
|
1815 |
+
DELETE FROM {table_name} WHERE workspace=$1
|
1816 |
+
""",
|
1817 |
}
|