From cf68cdfe3a1e077316196520e81645c9924aa327 Mon Sep 17 00:00:00 2001 From: BukeLy Date: Wed, 26 Nov 2025 02:06:40 +0800 Subject: [PATCH] refactor: improve PostgreSQL migration code quality Why this change is needed: 1. Added clarifying comments to _pg_migrate_workspace_data() parameter handling 2. Removed dead code from PGDocStatusStorage.initialize() that was never executed Changes: 1. PostgreSQL Migration Parameter Documentation (lightrag/kg/postgres_impl.py:2240-2241): - Added comments explaining dict rebuild for correct value ordering - Clarifies that Python 3.7+ dict insertion order is relied upon - Documents that execute() converts dict to tuple via .values() 2. Dead Code Removal (lightrag/kg/postgres_impl.py:3061-3062): - Removed unreachable table creation code from PGDocStatusStorage.initialize() - Table is already created by PostgreSQLDB.initdb() during initialization - This code path was never executed as table always exists before initialize() is called - Added NOTE comment explaining where table creation actually happens Impact: - No functional changes - only code clarification and cleanup - Reduces maintenance burden by removing unreachable code - Improves code readability with better documentation Testing: - All 14 PostgreSQL migration tests pass - All 5 UnifiedLock safety tests pass - Pre-commit checks pass (ruff-format, ruff) --- lightrag/kg/postgres_impl.py | 9 ++++----- uv.lock | 6 ++---- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/lightrag/kg/postgres_impl.py b/lightrag/kg/postgres_impl.py index 9cef3dde..c9ea40f7 100644 --- a/lightrag/kg/postgres_impl.py +++ b/lightrag/kg/postgres_impl.py @@ -2237,6 +2237,8 @@ async def _pg_migrate_workspace_data( VALUES ({placeholders}) ON CONFLICT (workspace, id) DO NOTHING """ + # Rebuild dict in columns order to ensure values() matches placeholders order + # Python 3.7+ dicts maintain insertion order, and execute() uses tuple(data.values()) values = {col: row_dict[col] for col in columns} await db.execute(insert_query, values) @@ -3058,11 +3060,8 @@ class PGDocStatusStorage(DocStatusStorage): # Use "default" for compatibility (lowest priority) self.workspace = "default" - # Create table if not exists - table_name = namespace_to_table_name(self.namespace) - table_exists = await _pg_table_exists(self.db, table_name) - if not table_exists: - await _pg_create_table(self.db, table_name, table_name) + # NOTE: Table creation is handled by PostgreSQLDB.initdb() during initialization + # No need to create table here as it's already created in the TABLES dict async def finalize(self): if self.db is not None: diff --git a/uv.lock b/uv.lock index 019f7539..97703af0 100644 --- a/uv.lock +++ b/uv.lock @@ -1,5 +1,5 @@ version = 1 -revision = 2 +revision = 3 requires-python = ">=3.10" resolution-markers = [ "python_full_version >= '3.14' and python_full_version < '4' and platform_machine == 'x86_64' and sys_platform == 'darwin'", @@ -2735,6 +2735,7 @@ requires-dist = [ { name = "json-repair", marker = "extra == 'api'" }, { name = "langfuse", marker = "extra == 'observability'", specifier = ">=3.8.1" }, { name = "lightrag-hku", extras = ["api", "offline-llm", "offline-storage"], marker = "extra == 'offline'" }, + { name = "lightrag-hku", extras = ["pytest"], marker = "extra == 'evaluation'" }, { name = "llama-index", marker = "extra == 'offline-llm'", specifier = ">=0.9.0,<1.0.0" }, { name = "nano-vectordb" }, { name = "nano-vectordb", marker = "extra == 'api'" }, @@ -2752,7 +2753,6 @@ requires-dist = [ { name = "passlib", extras = ["bcrypt"], marker = "extra == 'api'" }, { name = "pipmaster" }, { name = "pipmaster", marker = "extra == 'api'" }, - { name = "pre-commit", marker = "extra == 'evaluation'" }, { name = "pre-commit", marker = "extra == 'pytest'" }, { name = "psutil", marker = "extra == 'api'" }, { name = "pycryptodome", marker = "extra == 'api'", specifier = ">=3.0.0,<4.0.0" }, @@ -2764,9 +2764,7 @@ requires-dist = [ { name = "pypdf", marker = "extra == 'api'", specifier = ">=6.1.0" }, { name = "pypinyin" }, { name = "pypinyin", marker = "extra == 'api'" }, - { name = "pytest", marker = "extra == 'evaluation'", specifier = ">=8.4.2" }, { name = "pytest", marker = "extra == 'pytest'", specifier = ">=8.4.2" }, - { name = "pytest-asyncio", marker = "extra == 'evaluation'", specifier = ">=1.2.0" }, { name = "pytest-asyncio", marker = "extra == 'pytest'", specifier = ">=1.2.0" }, { name = "python-docx", marker = "extra == 'api'", specifier = ">=0.8.11,<2.0.0" }, { name = "python-dotenv" },