stock-monitor/docs/database/apply_extended_schema.py
ycg 569c1c8813 重构股票监控系统:数据库架构升级与功能完善
- 重构数据访问层:引入DAO模式,支持MySQL/SQLite双数据库
- 新增数据库架构:完整的股票数据、AI分析、自选股管理表结构
- 升级AI分析服务:集成豆包大模型,支持多维度分析
- 优化API路由:分离市场数据API,提供更清晰的接口设计
- 完善项目文档:添加数据库迁移指南、新功能指南等
- 清理冗余文件:删除旧的缓存文件和无用配置
- 新增调度器:支持定时任务和数据自动更新
- 改进前端模板:简化的股票展示页面

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-01 15:44:25 +08:00

124 lines
3.6 KiB
Python

#!/usr/bin/env python3
"""
应用扩展数据库结构脚本
执行新的数据库表结构和索引创建
"""
import sys
import os
from pathlib import Path
# 添加项目根目录到Python路径
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))
from app.database import DatabaseManager
def apply_extended_schema():
"""应用扩展的数据库结构"""
print("正在应用扩展的数据库结构...")
# 读取SQL脚本
schema_file = project_root / "database_schema_extended.sql"
if not schema_file.exists():
print(f"✗ 扩展数据库结构文件不存在: {schema_file}")
return False
with open(schema_file, 'r', encoding='utf-8') as f:
sql_content = f.read()
db_manager = DatabaseManager()
try:
with db_manager.get_connection() as conn:
cursor = conn.cursor()
# 分割SQL语句并执行
statements = [stmt.strip() for stmt in sql_content.split(';') if stmt.strip()]
for statement in statements:
if statement:
try:
cursor.execute(statement)
print(f"✓ 执行成功: {statement[:50]}...")
except Exception as e:
# 忽略表已存在的错误
if "already exists" not in str(e) and "Duplicate" not in str(e):
print(f"⚠️ 警告: 执行SQL语句失败: {statement[:50]}... 错误: {e}")
conn.commit()
print("✓ 扩展数据库结构应用成功")
cursor.close()
# 验证新表是否创建成功
verify_tables_created()
return True
except Exception as e:
print(f"✗ 应用扩展数据库结构失败: {e}")
return False
def verify_tables_created():
"""验证新表是否创建成功"""
print("正在验证新表是否创建成功...")
db_manager = DatabaseManager()
new_tables = [
'industries', 'sectors', 'kline_data', 'stock_sector_relations',
'market_statistics', 'data_update_tasks', 'hot_stocks'
]
try:
with db_manager.get_connection() as conn:
cursor = conn.cursor()
for table_name in new_tables:
cursor.execute(f"SHOW TABLES LIKE '{table_name}'")
exists = cursor.fetchone()
if exists:
print(f"✓ 表 {table_name} 创建成功")
else:
print(f"✗ 表 {table_name} 创建失败")
cursor.close()
except Exception as e:
print(f"✗ 验证表创建失败: {e}")
def main():
"""主函数"""
print("=" * 60)
print("应用扩展数据库结构")
print("=" * 60)
success = apply_extended_schema()
if success:
print("\n" + "=" * 60)
print("扩展数据库结构应用完成!")
print("=" * 60)
print("\n下一步操作:")
print("1. 启动应用系统: python run.py")
print("2. 访问 http://localhost:8000/api/market/stocks 查看股票列表")
print("3. 访问 http://localhost:8000/api/market/sync 同步市场数据")
print("=" * 60)
else:
print("✗ 扩展数据库结构应用失败,请检查错误信息")
return success
if __name__ == "__main__":
try:
success = main()
sys.exit(0 if success else 1)
except KeyboardInterrupt:
print("\n数据库结构应用被用户中断")
sys.exit(1)
except Exception as e:
print(f"\n数据库结构应用过程中发生错误: {e}")
sys.exit(1)