Support smooth upgrade

This commit is contained in:
Liujian committed 2025-07-30 19:48:13 +08:00
1 parent be7033e9a2
commit e42e5ad752
7 files changed
+258 -50

No files matched your search

+216 -31
View File
@@ -1,9 +1,10 @@
import os
import sys
import re
from pathlib import Path
from dotenv import load_dotenv
import mysql.connector
from typing import Optional
from typing import Optional, List, Tuple, Union
# 加载环境变量
load_dotenv()
@@ -16,26 +17,77 @@ class DBConfig:
MYSQL_PASSWORD = os.getenv("MYSQL_PASSWORD", "123456")
MYSQL_DB = os.getenv("MYSQL_DB", "xpack")
def execute_sql_file(file_path: str, db_config: Optional[DBConfig] = None) -> None:
"""执行 SQL 文件
def get_db_connection(db_config: Optional[DBConfig] = None):
"""获取数据库连接
Args:
file_path: SQL 文件路径
db_config: 数据库配置,如果为 None 则使用默认配置
Returns:
数据库连接对象
"""
if db_config is None:
db_config = DBConfig()
# 连接数据库
conn = mysql.connector.connect(
return mysql.connector.connect(
host=db_config.MYSQL_HOST,
port=db_config.MYSQL_PORT,
user=db_config.MYSQL_USER,
password=db_config.MYSQL_PASSWORD,
database=db_config.MYSQL_DB
)
cursor = conn.cursor()
def get_current_version(db_config: Optional[DBConfig] = None) -> Optional[str]:
"""查询sys_config表中的version值
Args:
db_config: 数据库配置
Returns:
当前版本号,如果不存在则返回None
"""
try:
conn = get_db_connection(db_config)
cursor = conn.cursor()
# 查询version配置
cursor.execute("SELECT value FROM sys_config WHERE `key` = 'version'")
result = cursor.fetchone()
cursor.close()
conn.close()
if result and len(result) > 0:
# 将元组的第一个元素转换为字符串并返回
return str(result['value'] if isinstance(result, dict) else result[0])
return None
except mysql.connector.Error as err:
# 如果表不存在或其他数据库错误,返回None
print(f"查询版本信息失败: {err}")
return None
except Exception as e:
print(f"查询版本信息出错: {e}")
return None
def execute_sql_file(file_path: str, db_config: Optional[DBConfig] = None) -> bool:
"""执行 SQL 文件
Args:
file_path: SQL 文件路径
db_config: 数据库配置,如果为 None 则使用默认配置
Returns:
执行是否成功
"""
if db_config is None:
db_config = DBConfig()
sql = ""
try:
conn = get_db_connection(db_config)
cursor = conn.cursor()
# 读取 SQL 文件
with open(file_path, 'r', encoding='utf-8') as f:
sql_content = f.read()
@@ -48,43 +100,176 @@ def execute_sql_file(file_path: str, db_config: Optional[DBConfig] = None) -> No
# 执行 SQL 语句
for statement in sql_content.split(';'):
if statement.strip():
sql = statement
cursor.execute(statement)
sql = statement.strip()
# 跳过注释行和空行
if sql.startswith('#') or sql.startswith('--') or not sql:
continue
# 移除行内注释
lines = sql.split('\n')
cleaned_lines = []
for line in lines:
line = line.strip()
if line and not line.startswith('#') and not line.startswith('--'):
cleaned_lines.append(line)
if not cleaned_lines:
continue
sql = '\n'.join(cleaned_lines)
try:
cursor.execute(sql)
except mysql.connector.Error as err:
# 检查是否是重复列错误,如果是则忽略并继续
if "Duplicate column name" in str(err):
print(f"忽略重复列错误: {err}")
continue
else:
print(f"执行SQL语句失败: {err}")
print(f"问题语句: {sql}")
# 回滚事务并关闭连接
conn.rollback()
cursor.close()
conn.close()
return False
# 启用外键检查
cursor.execute('SET FOREIGN_KEY_CHECKS = 1')
# 提交事务
conn.commit()
print(f"成功执行 SQL 文件: {file_path}")
except FileNotFoundError:
print(f"错误: SQL 文件不存在: {file_path}")
sys.exit(1)
except mysql.connector.Error as err:
# 输出语句
print(f"数据库错误: {err}, 语句: {sql}")
sys.exit(1)
except Exception as e:
print(f"执行出错: {e}")
sys.exit(1)
finally:
# 关闭连接
cursor.close()
conn.close()
return True
except FileNotFoundError:
print(f"错误: SQL 文件不存在: {file_path}")
return False
except mysql.connector.Error as err:
print(f"数据库错误: {err}")
if sql:
print(f"问题语句: {sql}")
return False
except Exception as e:
print(f"执行出错: {e}")
return False
def get_version_files(sql_dir: Path) -> List[Tuple[str, Path]]:
"""获取所有版本SQL文件并按版本号排序
Args:
sql_dir: SQL文件目录
Returns:
排序后的版本文件列表,格式为[(版本号, 文件路径), ...]
"""
version_files = []
# 查找所有version-*.sql文件
for file_path in sql_dir.glob("version-*.sql"):
# 提取版本号
match = re.match(r'version-(.+)\.sql', file_path.name)
if match:
version_str = match.group(1)
version_files.append((version_str, file_path))
# 按版本号排序(简单的字符串排序)
version_files.sort(key=lambda x: x[0])
return version_files
def should_apply_version(current_version: Optional[str], target_version: str) -> bool:
"""判断是否需要应用指定版本的更新
Args:
current_version: 当前版本号
target_version: 目标版本号
Returns:
是否需要应用更新
"""
if current_version is None:
return True
# 简单的字符串比较
return target_version > current_version
def main():
"""主函数:实现版本化数据库迁移策略"""
print("开始数据库初始化和迁移...")
# 获取项目根目录
project_root = Path(__file__).parent
sql_dir = project_root / 'sql'
# SQL 文件路径(优先使用 scripts/resource/db.sql,如果不存在则使用 docs/db.sql)
sql_paths = [
project_root / 'sql' / 'init.sql'
]
sql_file = next((path for path in sql_paths if path.exists()), None)
if not sql_file:
print("错误: 未找到 init.sql 文件")
if not sql_dir.exists():
print(f"错误: SQL目录不存在: {sql_dir}")
sys.exit(1)
execute_sql_file(str(sql_file))
# 步骤1: 查询sys_config,获取当key=version的值
print("步骤1: 查询当前数据库版本...")
current_version = get_current_version()
# 步骤2: 当不存在该key时,执行init.sql
if current_version is None:
print("步骤2: 未找到版本信息,执行初始化SQL...")
init_sql_file = sql_dir / 'init.sql'
if not init_sql_file.exists():
print(f"错误: 初始化SQL文件不存在: {init_sql_file}")
sys.exit(1)
if not execute_sql_file(str(init_sql_file)):
print("初始化SQL执行失败")
sys.exit(1)
# 重新查询版本信息
current_version = get_current_version()
print(f"初始化完成,当前版本: {current_version}")
else:
print(f"当前数据库版本: {current_version}")
# 步骤3: 获取对应的版本号
# 步骤4: 依次读取sql下的version-*.sql文件,执行更新sql,直到所有sql执行完成
print("步骤3-4: 检查并执行版本更新...")
# 获取所有版本文件
version_files = get_version_files(sql_dir)
if not version_files:
print("未找到版本更新文件,数据库已是最新版本")
return
print(f"找到 {len(version_files)} 个版本更新文件")
# 依次执行需要的版本更新
updated_count = 0
for version_str, file_path in version_files:
if should_apply_version(current_version, version_str):
print(f"执行版本更新: {version_str} ({file_path.name})")
if execute_sql_file(str(file_path)):
print(f"版本 {version_str} 更新成功")
current_version = version_str
updated_count += 1
else:
print(f"版本 {version_str} 更新失败,停止后续更新")
sys.exit(1)
else:
print(f"跳过版本 {version_str}(已应用或版本较低)")
if updated_count > 0:
print(f"数据库迁移完成,共应用了 {updated_count} 个版本更新")
# 验证最终版本
final_version = get_current_version()
print(f"最终数据库版本: {final_version}")
else:
print("数据库已是最新版本,无需更新")
print("数据库初始化和迁移流程完成!")
if __name__ == '__main__':
main()
+5 -9
View File
@@ -275,15 +275,7 @@ UNIQUE KEY `uk_key` (`key`) USING BTREE COMMENT '配置键名唯一索引'
INSERT INTO `user` (`id`, `name`, `email`, `password`, `is_active`, `is_deleted`, `register_type`, `role_id`, `created_at`, `updated_at`)
VALUES ('admin', 'admin', 'admin@xpack.com', '25f9e794323b453885f5181f1b624d0b', 1, 0, 'inner', 1, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
ON DUPLICATE KEY UPDATE
`name` = VALUES(`name`),
`email` = VALUES(`email`),
`password` = VALUES(`password`),
`is_active` = VALUES(`is_active`),
`is_deleted` = VALUES(`is_deleted`),
`register_type` = VALUES(`register_type`),
`role_id` = VALUES(`role_id`),
`updated_at` = CURRENT_TIMESTAMP;
ON DUPLICATE KEY UPDATE `register_type` = VALUES(`register_type`);
INSERT INTO `user_wallet` (`id`, `user_id`, `balance`, `frozen_balance`, `created_at`, `updated_at`)
SELECT UUID(), 'admin', 0.00, 0.00, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
@@ -293,4 +285,8 @@ INSERT INTO `payment_channel` (`id`, `name`, `status`, `config`, `updated_at`)
SELECT 'stripe', 'Stripe', 1, '{\"secret\": \"\", \"webhook_secret\": \"\"}', '2025-07-17 06:42:55'
WHERE NOT EXISTS (SELECT 1 FROM `payment_channel` WHERE `id` = 'stripe');
INSERT INTO `sys_config` (`id`,`key`, `value`,`description`,`created_at`,`updated_at`)
VALUES ('xpack-version','version', '0.2.2', 'User wallet history max count', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
ON DUPLICATE KEY UPDATE `value` = VALUES(`value`), `description` = VALUES(`description`), `updated_at` = CURRENT_TIMESTAMP;
SET FOREIGN_KEY_CHECKS = 1;
+24
View File
@@ -0,0 +1,24 @@
# mcp_call_log
ALTER TABLE `mcp_call_log` DROP COLUMN IF EXISTS `apikey`;
ALTER TABLE `mcp_call_log` ADD COLUMN `apikey_id` char(36) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT 'apikey_id' AFTER `wallet_history_id`;
# mcp_service
ALTER TABLE `mcp_service` ADD COLUMN `input_token_price` decimal(10, 2) NULL DEFAULT NULL COMMENT 'input token price' AFTER `price`;
ALTER TABLE `mcp_service` ADD COLUMN `output_token_price` decimal(10, 2) NULL DEFAULT NULL COMMENT 'output token price' AFTER `price`;
# temp_mcp_service
ALTER TABLE `temp_mcp_service` ADD COLUMN `input_token_price` decimal(10, 2) NULL DEFAULT NULL COMMENT 'input token price' AFTER `price`;
ALTER TABLE `temp_mcp_service` ADD COLUMN `output_token_price` decimal(10, 2) NULL DEFAULT NULL COMMENT 'output token price' AFTER `price`;
# user_wallet
ALTER TABLE `user_wallet` MODIFY COLUMN `balance` decimal(16, 6) NOT NULL COMMENT 'Wallet balance' AFTER `user_id`;
ALTER TABLE `user_wallet` MODIFY COLUMN `frozen_balance` decimal(16, 6) NOT NULL COMMENT 'Frozen balance' AFTER `balance`;
# user_wallet_history
ALTER TABLE `user_wallet_history` MODIFY COLUMN `amount` decimal(16, 6) NOT NULL COMMENT 'Change amount' AFTER `payment_method`;
ALTER TABLE `user_wallet_history` MODIFY COLUMN `balance_after` decimal(16, 6) NOT NULL COMMENT 'Balance after change' AFTER `amount`;
INSERT INTO `sys_config` (`id`,`key`, `value`,`description`,`created_at`,`updated_at`)
VALUES ('xpack-version','version', '1.0.0', 'User wallet history max count', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
ON DUPLICATE KEY UPDATE `value` = VALUES(`value`), `description` = VALUES(`description`), `updated_at` = CURRENT_TIMESTAMP;
+3 -3
View File
@@ -1,11 +1,11 @@
{
"version": "v0.2",
"releaseDate": "2025-07-24",
"version": "v1.0",
"releaseDate": "2025-07-30",
"description": "This is the latest release of the application.",
"downloads": {
"program": {
"xpack_mcp_market": {
"version": "v0.2.2",
"version": "v1.0.0",
"docker": "xpackai/xpack-mcp-market",
"tar": "xpack-mcp-market.tar.gz",
"url": "https://public.eolinker.com/xpack/images/xpack-mcp-market.${VERSION}.${ARCH}.tar.gz"
+1 -1
View File
@@ -26,7 +26,7 @@ def get_user_order_list(
):
"""Get paginated list of user order history."""
offset = (page - 1) * page_size
total, orders = user_wallet_history_service.success_order_list(offset, page_size)
total, orders = user_wallet_history_service.success_deposit_order_list(offset, page_size)
if not orders:
return ResponseUtils.success_page(data=[], total=total, page_num=page, page_size=page_size)
result = []
@@ -214,7 +214,7 @@ class UserWalletHistoryRepository:
self.db.rollback()
return False
def success_order_list(self, offset: int, limit: int) -> Tuple[int, List[UserWalletHistory]]:
def success_deposit_order_list(self, offset: int, limit: int) -> Tuple[int, List[UserWalletHistory]]:
"""
Get list of successful orders.
@@ -225,12 +225,15 @@ class UserWalletHistoryRepository:
Returns:
tuple: (total_count, order_list)
"""
total = self.db.query(UserWalletHistory).filter(UserWalletHistory.status == 1).count()
query = self.db.query(UserWalletHistory).filter(
UserWalletHistory.status == 1,
UserWalletHistory.type == TransactionType.DEPOSIT,
)
total = query.count()
if total < offset:
return total, []
history = (
self.db.query(UserWalletHistory)
.filter(UserWalletHistory.status == 1)
query
.order_by(UserWalletHistory.created_at.desc())
.offset(offset)
.limit(limit)
@@ -15,8 +15,8 @@ class UserWalletHistoryService:
def add_refund(self, user_id: str, amount: float, payment_method: str, transaction_id: str) -> UserWalletHistory:
return self.user_wallet_history_repository.add_refund(user_id, amount, payment_method, transaction_id)
def success_order_list(self, offset: int, limit: int) -> Tuple[int, List[UserWalletHistory]]:
return self.user_wallet_history_repository.success_order_list(offset, limit)
def success_deposit_order_list(self, offset: int, limit: int) -> Tuple[int, List[UserWalletHistory]]:
return self.user_wallet_history_repository.success_deposit_order_list(offset, limit)
def order_list(self, payment_method: str, status: int, start: Optional[datetime] = None, end: Optional[datetime] = None) -> List[UserWalletHistory]:
return self.user_wallet_history_repository.order_list(payment_method, status, start, end)