修复:PostgreSQL 语法适配 + UUID 自动生成
All checks were successful
构建并部署 AI Agent 服务 / deploy (push) Successful in 5m11s
All checks were successful
构建并部署 AI Agent 服务 / deploy (push) Successful in 5m11s
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
所有子图的数据操作都用这个,避免重复代码
|
所有子图的数据操作都用这个,避免重复代码
|
||||||
"""
|
"""
|
||||||
import json
|
import json
|
||||||
|
import uuid
|
||||||
from typing import Any, List, Dict, Optional
|
from typing import Any, List, Dict, Optional
|
||||||
from dataclasses import dataclass, asdict
|
from dataclasses import dataclass, asdict
|
||||||
|
|
||||||
@@ -53,7 +54,10 @@ class BaseRepository:
|
|||||||
async def insert(self, entity: BaseEntity) -> str:
|
async def insert(self, entity: BaseEntity) -> str:
|
||||||
"""插入单个实体,返回 ID"""
|
"""插入单个实体,返回 ID"""
|
||||||
data = entity.to_dict()
|
data = entity.to_dict()
|
||||||
data.pop('id', None) # 如果 id 是自增的
|
|
||||||
|
# 确保有 ID
|
||||||
|
if not data.get('id'):
|
||||||
|
data['id'] = str(uuid.uuid4())
|
||||||
|
|
||||||
if not data:
|
if not data:
|
||||||
raise ValueError("实体数据为空")
|
raise ValueError("实体数据为空")
|
||||||
@@ -71,7 +75,7 @@ class BaseRepository:
|
|||||||
async with self.conn.cursor() as cur:
|
async with self.conn.cursor() as cur:
|
||||||
await cur.execute(sql, values)
|
await cur.execute(sql, values)
|
||||||
row = await cur.fetchone()
|
row = await cur.fetchone()
|
||||||
return row[self.id_column] if row else None
|
return row[self.id_column] if row else data['id']
|
||||||
|
|
||||||
async def update(self, entity_id: str, data: Dict[str, Any]) -> bool:
|
async def update(self, entity_id: str, data: Dict[str, Any]) -> bool:
|
||||||
"""更新实体"""
|
"""更新实体"""
|
||||||
|
|||||||
@@ -34,16 +34,14 @@ async def _create_contacts_table(conn):
|
|||||||
email VARCHAR(100),
|
email VARCHAR(100),
|
||||||
company VARCHAR(100),
|
company VARCHAR(100),
|
||||||
position VARCHAR(100),
|
position VARCHAR(100),
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
|
|
||||||
INDEX idx_contacts_user (user_id)
|
|
||||||
);
|
);
|
||||||
"""
|
"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with conn.cursor() as cur:
|
async with conn.cursor() as cur:
|
||||||
await cur.execute(sql)
|
await cur.execute(sql)
|
||||||
# 注释掉 INFO 打印,避免噪声
|
await _create_index_if_not_exists(conn, "idx_contacts_user", "contacts", "user_id")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"contacts 表可能已存在: {e}")
|
print(f"contacts 表可能已存在: {e}")
|
||||||
|
|
||||||
@@ -59,16 +57,15 @@ async def _create_words_table(conn):
|
|||||||
part_of_speech VARCHAR(50),
|
part_of_speech VARCHAR(50),
|
||||||
definition TEXT,
|
definition TEXT,
|
||||||
examples TEXT,
|
examples TEXT,
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
|
|
||||||
INDEX idx_words_user (user_id),
|
|
||||||
INDEX idx_words_word (word)
|
|
||||||
);
|
);
|
||||||
"""
|
"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with conn.cursor() as cur:
|
async with conn.cursor() as cur:
|
||||||
await cur.execute(sql)
|
await cur.execute(sql)
|
||||||
|
await _create_index_if_not_exists(conn, "idx_words_user", "words", "user_id")
|
||||||
|
await _create_index_if_not_exists(conn, "idx_words_word", "words", "word")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"words 表可能已存在: {e}")
|
print(f"words 表可能已存在: {e}")
|
||||||
|
|
||||||
@@ -84,14 +81,23 @@ async def _create_news_table(conn):
|
|||||||
url VARCHAR(500),
|
url VARCHAR(500),
|
||||||
source VARCHAR(100),
|
source VARCHAR(100),
|
||||||
keywords TEXT,
|
keywords TEXT,
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
|
|
||||||
INDEX idx_news_user (user_id)
|
|
||||||
);
|
);
|
||||||
"""
|
"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with conn.cursor() as cur:
|
async with conn.cursor() as cur:
|
||||||
await cur.execute(sql)
|
await cur.execute(sql)
|
||||||
|
await _create_index_if_not_exists(conn, "idx_news_user", "news", "user_id")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"news 表可能已存在: {e}")
|
print(f"news 表可能已存在: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
async def _create_index_if_not_exists(conn, index_name: str, table_name: str, column_name: str):
|
||||||
|
"""创建索引(如果不存在)"""
|
||||||
|
sql = f"CREATE INDEX IF NOT EXISTS {index_name} ON {table_name} ({column_name});"
|
||||||
|
try:
|
||||||
|
async with conn.cursor() as cur:
|
||||||
|
await cur.execute(sql)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"索引 {index_name} 可能已存在: {e}")
|
||||||
|
|||||||
Reference in New Issue
Block a user