gzdaniel commited on
Commit
fb6fb0f
·
1 Parent(s): bdc814e

Store utc time in PostgreSQL

Browse files
Files changed (1) hide show
  1. lightrag/kg/postgres_impl.py +33 -5
lightrag/kg/postgres_impl.py CHANGED
@@ -2,6 +2,7 @@ import asyncio
2
  import json
3
  import os
4
  import time
 
5
  from dataclasses import dataclass, field
6
  from typing import Any, Union, final
7
  import numpy as np
@@ -992,8 +993,28 @@ class PGDocStatusStorage(DocStatusStorage):
992
  if not data:
993
  return
994
 
995
- sql = """insert into LIGHTRAG_DOC_STATUS(workspace,id,content,content_summary,content_length,chunks_count,status,file_path)
996
- values($1,$2,$3,$4,$5,$6,$7,$8)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
997
  on conflict(id,workspace) do update set
998
  content = EXCLUDED.content,
999
  content_summary = EXCLUDED.content_summary,
@@ -1001,8 +1022,13 @@ class PGDocStatusStorage(DocStatusStorage):
1001
  chunks_count = EXCLUDED.chunks_count,
1002
  status = EXCLUDED.status,
1003
  file_path = EXCLUDED.file_path,
1004
- updated_at = CURRENT_TIMESTAMP"""
 
1005
  for k, v in data.items():
 
 
 
 
1006
  # chunks_count is optional
1007
  await self.db.execute(
1008
  sql,
@@ -1015,6 +1041,8 @@ class PGDocStatusStorage(DocStatusStorage):
1015
  "chunks_count": v["chunks_count"] if "chunks_count" in v else -1,
1016
  "status": v["status"],
1017
  "file_path": v["file_path"],
 
 
1018
  },
1019
  )
1020
 
@@ -2265,8 +2293,8 @@ TABLES = {
2265
  chunks_count int4 NULL,
2266
  status varchar(64) NULL,
2267
  file_path TEXT NULL,
2268
- created_at timestamp DEFAULT CURRENT_TIMESTAMP NULL,
2269
- updated_at timestamp DEFAULT CURRENT_TIMESTAMP NULL,
2270
  CONSTRAINT LIGHTRAG_DOC_STATUS_PK PRIMARY KEY (workspace, id)
2271
  )"""
2272
  },
 
2
  import json
3
  import os
4
  import time
5
+ import datetime
6
  from dataclasses import dataclass, field
7
  from typing import Any, Union, final
8
  import numpy as np
 
993
  if not data:
994
  return
995
 
996
+ def parse_datetime(dt_str):
997
+ if dt_str is None:
998
+ return None
999
+ if isinstance(dt_str, (datetime.date, datetime.datetime)):
1000
+ # If it's a datetime object without timezone info, remove timezone info
1001
+ if isinstance(dt_str, datetime.datetime):
1002
+ # Remove timezone info, return naive datetime object
1003
+ return dt_str.replace(tzinfo=None)
1004
+ return dt_str
1005
+ try:
1006
+ # Process ISO format string with timezone
1007
+ dt = datetime.datetime.fromisoformat(dt_str)
1008
+ # Remove timezone info, return naive datetime object
1009
+ return dt.replace(tzinfo=None)
1010
+ except (ValueError, TypeError):
1011
+ logger.warning(f"Unable to parse datetime string: {dt_str}")
1012
+ return None
1013
+
1014
+ # Modified SQL to include created_at and updated_at in both INSERT and UPDATE operations
1015
+ # Both fields are updated from the input data in both INSERT and UPDATE cases
1016
+ sql = """insert into LIGHTRAG_DOC_STATUS(workspace,id,content,content_summary,content_length,chunks_count,status,file_path,created_at,updated_at)
1017
+ values($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)
1018
  on conflict(id,workspace) do update set
1019
  content = EXCLUDED.content,
1020
  content_summary = EXCLUDED.content_summary,
 
1022
  chunks_count = EXCLUDED.chunks_count,
1023
  status = EXCLUDED.status,
1024
  file_path = EXCLUDED.file_path,
1025
+ created_at = EXCLUDED.created_at,
1026
+ updated_at = EXCLUDED.updated_at"""
1027
  for k, v in data.items():
1028
+ # Remove timezone information, store utc time in db
1029
+ created_at = parse_datetime(v.get("created_at"))
1030
+ updated_at = parse_datetime(v.get("updated_at"))
1031
+
1032
  # chunks_count is optional
1033
  await self.db.execute(
1034
  sql,
 
1041
  "chunks_count": v["chunks_count"] if "chunks_count" in v else -1,
1042
  "status": v["status"],
1043
  "file_path": v["file_path"],
1044
+ "created_at": created_at, # 使用转换后的datetime对象
1045
+ "updated_at": updated_at, # 使用转换后的datetime对象
1046
  },
1047
  )
1048
 
 
2293
  chunks_count int4 NULL,
2294
  status varchar(64) NULL,
2295
  file_path TEXT NULL,
2296
+ created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NULL,
2297
+ updated_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP NULL,
2298
  CONSTRAINT LIGHTRAG_DOC_STATUS_PK PRIMARY KEY (workspace, id)
2299
  )"""
2300
  },