fix: 添加 PyMuPDF 详细错误信息并创建缺失的迁移文件
- extraction.py: 添加更详细的 ImportError 错误信息,显示具体原因 - migrate_016.py: 创建 insurance_ppt_sessions 表(修复 500 错误) 解决 PDF 上传失败问题: 1. PyMuPDF 导入失败时显示具体错误原因 2. 数据库表 insurance_ppt_sessions 不存在导致插入失败 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
parent
6eded843a7
commit
623815a134
55
api/insurance/db/migrate_016.py
Normal file
55
api/insurance/db/migrate_016.py
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
"""迁移 016: 创建 insurance_ppt_sessions 表。"""
|
||||||
|
import logging
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def migrate():
|
||||||
|
"""迁移入口。"""
|
||||||
|
from insurance.db.compat import db
|
||||||
|
from sqlalchemy import text
|
||||||
|
|
||||||
|
dialect = db.engine.dialect.name
|
||||||
|
is_postgres = dialect == 'postgresql'
|
||||||
|
|
||||||
|
if is_postgres:
|
||||||
|
sql = """
|
||||||
|
CREATE TABLE IF NOT EXISTS insurance_ppt_sessions (
|
||||||
|
id VARCHAR(36) PRIMARY KEY,
|
||||||
|
user_id VARCHAR(64) NOT NULL,
|
||||||
|
status VARCHAR(20) DEFAULT 'created' NOT NULL,
|
||||||
|
files_json TEXT,
|
||||||
|
extractions_json TEXT,
|
||||||
|
chat_history_json TEXT,
|
||||||
|
ppt_path VARCHAR(500),
|
||||||
|
markdown_path VARCHAR(500),
|
||||||
|
preview_paths_json TEXT,
|
||||||
|
preview_pdf_path VARCHAR(500),
|
||||||
|
slide_count INTEGER,
|
||||||
|
created_at TIMESTAMP DEFAULT NOW(),
|
||||||
|
updated_at TIMESTAMP DEFAULT NOW()
|
||||||
|
)
|
||||||
|
"""
|
||||||
|
else:
|
||||||
|
sql = """
|
||||||
|
CREATE TABLE IF NOT EXISTS insurance_ppt_sessions (
|
||||||
|
id VARCHAR(36) PRIMARY KEY,
|
||||||
|
user_id VARCHAR(64) NOT NULL,
|
||||||
|
status VARCHAR(20) DEFAULT 'created' NOT NULL,
|
||||||
|
files_json TEXT,
|
||||||
|
extractions_json TEXT,
|
||||||
|
chat_history_json TEXT,
|
||||||
|
ppt_path VARCHAR(500),
|
||||||
|
markdown_path VARCHAR(500),
|
||||||
|
preview_paths_json TEXT,
|
||||||
|
preview_pdf_path VARCHAR(500),
|
||||||
|
slide_count INTEGER,
|
||||||
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
|
)
|
||||||
|
"""
|
||||||
|
|
||||||
|
logger.info("[migrate_016] 创建 insurance_ppt_sessions 表 ...")
|
||||||
|
db.session.execute(text(sql))
|
||||||
|
db.session.commit()
|
||||||
|
logger.info("[migrate_016] insurance_ppt_sessions 表创建完成")
|
||||||
@ -98,8 +98,11 @@ def _extract_pdf_text(pdf_path: str, max_chars: int = 30000) -> str:
|
|||||||
if len(full_text) > max_chars:
|
if len(full_text) > max_chars:
|
||||||
full_text = full_text[:max_chars]
|
full_text = full_text[:max_chars]
|
||||||
return full_text
|
return full_text
|
||||||
except ImportError:
|
except ImportError as e:
|
||||||
logger.warning("PyMuPDF 未安装,无法提取 PDF 文本")
|
logger.warning(f"PyMuPDF 未安装,无法提取 PDF 文本: {e}")
|
||||||
|
return ""
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"PyMuPDF 导入异常: {e}")
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user