修改model默认时间为服务器时间
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
from datetime import datetime
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
|
||||
from sqlalchemy.orm import DeclarativeBase
|
||||
from sqlalchemy import Column, String, Integer, DateTime, Text, JSON, Index, text
|
||||
@@ -14,7 +15,7 @@ class Base(DeclarativeBase):
|
||||
|
||||
class CallbackFailureLog(Base):
|
||||
__tablename__ = "callback_failure_logs"
|
||||
|
||||
|
||||
id = Column(Integer, primary_key=True, autoincrement=True, comment="日志ID")
|
||||
site_id = Column(String(100), nullable=False, comment="站点ID") # 新增siteId字段
|
||||
remote_address = Column(String(45), nullable=True, comment="客户端IP地址")
|
||||
@@ -22,12 +23,12 @@ class CallbackFailureLog(Base):
|
||||
request_url = Column(String(500), nullable=False, comment="请求URL")
|
||||
request_headers = Column(JSON, nullable=False, comment="请求头")
|
||||
request_body = Column(JSON, nullable=False, comment="请求体")
|
||||
created_at = Column(DateTime, server_default=func.now(), comment="创建时间")
|
||||
created_at = Column(DateTime, default=datetime.now(), comment="创建时间")
|
||||
|
||||
|
||||
class CallbackFailureData(Base):
|
||||
__tablename__ = "callback_failure_data"
|
||||
|
||||
|
||||
id = Column(Integer, primary_key=True, autoincrement=True, comment="数据ID")
|
||||
callback_failure_log_id = Column(Integer, nullable=False, comment="回调失败日志ID")
|
||||
phone_number = Column(String(20), nullable=False, comment="手机号")
|
||||
@@ -36,12 +37,12 @@ class CallbackFailureData(Base):
|
||||
status_description = Column(String(200), nullable=False, comment="状态描述")
|
||||
raw_data = Column(Text, nullable=False, comment="原始回调数据JSON字符串")
|
||||
calldate = Column(DateTime, nullable=True, comment="通话日期")
|
||||
created_at = Column(DateTime, server_default=func.now(), comment="创建时间")
|
||||
created_at = Column(DateTime, default=datetime.now(), comment="创建时间")
|
||||
|
||||
|
||||
class ExternalApiLog(Base):
|
||||
__tablename__ = "external_api_logs" # 重命名表,更通用
|
||||
|
||||
|
||||
id = Column(Integer, primary_key=True, autoincrement=True, comment="日志ID")
|
||||
callback_failure_log_id = Column(Integer, nullable=False, comment="回调失败日志ID")
|
||||
request_url = Column(String(500), nullable=False, comment="外部接口请求URL")
|
||||
@@ -51,21 +52,15 @@ class ExternalApiLog(Base):
|
||||
response_headers = Column(JSON, nullable=False, comment="外部接口响应头")
|
||||
response_body = Column(Text, nullable=False, comment="外部接口响应体")
|
||||
retry_count = Column(Integer, default=0, comment="重试次数")
|
||||
created_at = Column(DateTime, server_default=func.now(), comment="创建时间")
|
||||
created_at = Column(DateTime, default=datetime.now(), comment="创建时间")
|
||||
|
||||
|
||||
# 创建数据库引擎
|
||||
engine = create_async_engine(
|
||||
settings.database_url,
|
||||
echo=settings.debug,
|
||||
future=True
|
||||
)
|
||||
engine = create_async_engine(settings.database_url, echo=settings.debug, future=True)
|
||||
|
||||
# 创建会话工厂
|
||||
AsyncSessionLocal = async_sessionmaker(
|
||||
engine,
|
||||
class_=AsyncSession,
|
||||
expire_on_commit=False
|
||||
engine, class_=AsyncSession, expire_on_commit=False
|
||||
)
|
||||
|
||||
|
||||
@@ -82,14 +77,18 @@ async def init_db():
|
||||
try:
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
|
||||
|
||||
# 检查并创建手机号索引
|
||||
await conn.execute(text("""
|
||||
await conn.execute(
|
||||
text(
|
||||
"""
|
||||
CREATE INDEX IF NOT EXISTS idx_phone_number
|
||||
ON callback_failure_data (phone_number)
|
||||
"""))
|
||||
|
||||
"""
|
||||
)
|
||||
)
|
||||
|
||||
logger.info("✅ 数据库表结构初始化完成")
|
||||
except Exception as e:
|
||||
logger.error(f"❌ 数据库初始化失败: {e}")
|
||||
raise
|
||||
raise
|
||||
|
||||
Reference in New Issue
Block a user