原始数据建表优化
This commit is contained in:
@@ -3,7 +3,7 @@ from sqlalchemy.ext.declarative import declarative_base
|
|||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import sessionmaker
|
||||||
from .config import settings
|
from .config import settings
|
||||||
|
|
||||||
engine = create_engine(settings.DATABASE_URL, echo=False, pool_size=15, max_overflow=30, pool_timeout=60, pool_recycle=300)
|
engine = create_engine(settings.DATABASE_URL, pool_pre_ping=True, echo=False, pool_size=30, max_overflow=60, pool_timeout=30, pool_recycle=3600)
|
||||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||||
|
|
||||||
Base = declarative_base()
|
Base = declarative_base()
|
||||||
|
|||||||
@@ -37,31 +37,50 @@ class OriginalDataService(BaseService[OriginalData]):
|
|||||||
logger.info(f"Table {table_name} already exists")
|
logger.info(f"Table {table_name} already exists")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# 表不存在,创建表
|
# 表不存在,创建表 - 添加重试机制
|
||||||
try:
|
max_retries = 3
|
||||||
create_table_sql = f"""
|
for attempt in range(max_retries):
|
||||||
CREATE TABLE `{table_name}` (
|
try:
|
||||||
`id` INT AUTO_INCREMENT PRIMARY KEY,
|
create_table_sql = f"""
|
||||||
`account_id` INT NOT NULL COMMENT '账号ID',
|
CREATE TABLE `{table_name}` (
|
||||||
`bfpcode` VARCHAR(1000) NOT NULL COMMENT '前(后)视点名称',
|
`id` INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
`mtime` DATETIME NOT NULL COMMENT '测点观测时间',
|
`account_id` INT NOT NULL COMMENT '账号ID',
|
||||||
`bffb` VARCHAR(1000) NOT NULL COMMENT '前(后)视标记符',
|
`bfpcode` VARCHAR(1000) NOT NULL COMMENT '前(后)视点名称',
|
||||||
`bfpl` VARCHAR(1000) NOT NULL COMMENT '前(后)视距离(m)',
|
`mtime` DATETIME NOT NULL COMMENT '测点观测时间',
|
||||||
`bfpvalue` VARCHAR(1000) NOT NULL COMMENT '前(后)视尺读数(m)',
|
`bffb` VARCHAR(1000) NOT NULL COMMENT '前(后)视标记符',
|
||||||
`NYID` VARCHAR(100) NOT NULL COMMENT '期数id',
|
`bfpl` VARCHAR(1000) NOT NULL COMMENT '前(后)视距离(m)',
|
||||||
`sort` INT COMMENT '序号',
|
`bfpvalue` VARCHAR(1000) NOT NULL COMMENT '前(后)视尺读数(m)',
|
||||||
INDEX `idx_nyid` (`NYID`),
|
`NYID` VARCHAR(100) NOT NULL COMMENT '期数id',
|
||||||
INDEX `idx_account_id` (`account_id`)
|
`sort` INT COMMENT '序号',
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='原始数据表_账号{account_id}'
|
INDEX `idx_nyid` (`NYID`),
|
||||||
"""
|
INDEX `idx_account_id` (`account_id`)
|
||||||
db.execute(text(create_table_sql))
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='原始数据表_账号{account_id}'
|
||||||
db.commit()
|
"""
|
||||||
logger.info(f"Table {table_name} created successfully")
|
# 使用引擎直接执行,不依赖db会话的事务
|
||||||
return True
|
with engine.begin() as conn: # 创建独立连接
|
||||||
except Exception as e:
|
conn.execute(text(create_table_sql))
|
||||||
db.rollback()
|
|
||||||
logger.error(f"Failed to create table {table_name}: {str(e)}")
|
logger.info(f"Table {table_name} created successfully on attempt {attempt + 1}")
|
||||||
raise Exception(f"创建原始数据表失败: {str(e)}")
|
return True
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Attempt {attempt + 1} to create table {table_name} failed: {str(e)}")
|
||||||
|
|
||||||
|
# 检查是否是表已存在错误(并发情况下可能发生)
|
||||||
|
if "already exists" in str(e).lower() or "exist" in str(e).lower():
|
||||||
|
logger.info(f"Table {table_name} was created by another process")
|
||||||
|
return True
|
||||||
|
|
||||||
|
if attempt == max_retries - 1:
|
||||||
|
db.rollback() # 确保回滚当前事务
|
||||||
|
logger.error(f"Failed to create table {table_name} after {max_retries} attempts: {str(e)}")
|
||||||
|
raise Exception(f"创建原始数据表失败: {str(e)}")
|
||||||
|
|
||||||
|
# 短暂延迟后重试
|
||||||
|
import time
|
||||||
|
time.sleep(0.1 * (attempt + 1))
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
def create_table_for_account(self, db: Session, account_id: int) -> Dict[str, Any]:
|
def create_table_for_account(self, db: Session, account_id: int) -> Dict[str, Any]:
|
||||||
"""
|
"""
|
||||||
@@ -83,12 +102,20 @@ class OriginalDataService(BaseService[OriginalData]):
|
|||||||
'message': f'账号ID {account_id} 不存在'
|
'message': f'账号ID {account_id} 不存在'
|
||||||
}
|
}
|
||||||
|
|
||||||
# 创建表
|
# 创建表 - 现在使用增强的错误处理
|
||||||
self._ensure_table_exists(db, account_id)
|
table_created = self._ensure_table_exists(db, account_id)
|
||||||
return {
|
|
||||||
'success': True,
|
if table_created:
|
||||||
'message': f'原始数据表 {self._get_table_name(account_id)} 创建成功'
|
return {
|
||||||
}
|
'success': True,
|
||||||
|
'message': f'原始数据表 {self._get_table_name(account_id)} 创建成功'
|
||||||
|
}
|
||||||
|
else:
|
||||||
|
return {
|
||||||
|
'success': False,
|
||||||
|
'message': f'原始数据表 {self._get_table_name(account_id)} 创建失败'
|
||||||
|
}
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Failed to create table for account {account_id}: {str(e)}")
|
logger.error(f"Failed to create table for account {account_id}: {str(e)}")
|
||||||
return {
|
return {
|
||||||
@@ -296,13 +323,12 @@ class OriginalDataService(BaseService[OriginalData]):
|
|||||||
'failed_items': []
|
'failed_items': []
|
||||||
}
|
}
|
||||||
|
|
||||||
# 确保表存在
|
# 确保表存在 - 现在使用增强的错误处理和重试机制
|
||||||
try:
|
table_created = self._ensure_table_exists(db, account_id)
|
||||||
self._ensure_table_exists(db, account_id)
|
if not table_created:
|
||||||
except Exception as e:
|
|
||||||
return {
|
return {
|
||||||
'success': False,
|
'success': False,
|
||||||
'message': f'创建原始数据表失败: {str(e)}',
|
'message': '创建原始数据表失败',
|
||||||
'total_count': total_count,
|
'total_count': total_count,
|
||||||
'success_count': 0,
|
'success_count': 0,
|
||||||
'failed_count': total_count,
|
'failed_count': total_count,
|
||||||
|
|||||||
Reference in New Issue
Block a user