修复数据库错误
This commit is contained in:
@@ -180,6 +180,9 @@ class Database:
|
||||
""")
|
||||
|
||||
# 赌场游戏会话表
|
||||
# 注意:移除了UNIQUE(chat_id, game_type, status)约束
|
||||
# 因为status='closed'时需要允许多条历史记录
|
||||
# 单场限制通过应用层逻辑(get_any_active_casino_session)保证
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS casino_sessions (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
@@ -192,11 +195,92 @@ class Database:
|
||||
house_fee REAL DEFAULT 0.05,
|
||||
status TEXT DEFAULT 'open',
|
||||
created_at INTEGER NOT NULL,
|
||||
settled_at INTEGER,
|
||||
UNIQUE(chat_id, game_type, status)
|
||||
settled_at INTEGER
|
||||
)
|
||||
""")
|
||||
|
||||
# 迁移:如果表已存在且有UNIQUE约束,需要重建表
|
||||
# 检查是否已有旧表(通过检查是否有UNIQUE约束的索引)
|
||||
try:
|
||||
# 尝试查询表结构,检查是否有UNIQUE约束相关的索引
|
||||
cursor.execute("SELECT sql FROM sqlite_master WHERE type='table' AND name='casino_sessions'")
|
||||
old_sql = cursor.fetchone()
|
||||
if old_sql and 'UNIQUE(chat_id, game_type, status)' in old_sql[0]:
|
||||
# 需要重建表以移除UNIQUE约束
|
||||
logger.info("检测到旧版本的casino_sessions表,需要重建以移除UNIQUE约束")
|
||||
# 禁用外键检查(SQLite默认可能未启用,但为了安全)
|
||||
cursor.execute("PRAGMA foreign_keys=OFF")
|
||||
# 创建临时表
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS casino_sessions_new (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
chat_id INTEGER NOT NULL,
|
||||
game_type TEXT NOT NULL,
|
||||
banker_id INTEGER NOT NULL,
|
||||
min_bet INTEGER NOT NULL,
|
||||
max_bet INTEGER NOT NULL,
|
||||
multiplier REAL NOT NULL,
|
||||
house_fee REAL DEFAULT 0.05,
|
||||
status TEXT DEFAULT 'open',
|
||||
created_at INTEGER NOT NULL,
|
||||
settled_at INTEGER,
|
||||
current_phase TEXT DEFAULT 'betting',
|
||||
blackjack_multiplier REAL DEFAULT 1.5
|
||||
)
|
||||
""")
|
||||
# 复制所有数据(包括历史记录)
|
||||
# 检查旧表是否有新字段
|
||||
old_columns = [row[1] for row in cursor.execute("PRAGMA table_info(casino_sessions)").fetchall()]
|
||||
has_current_phase = 'current_phase' in old_columns
|
||||
has_blackjack_multiplier = 'blackjack_multiplier' in old_columns
|
||||
|
||||
if has_current_phase and has_blackjack_multiplier:
|
||||
# 旧表已有新字段,直接复制所有列
|
||||
cursor.execute("""
|
||||
INSERT INTO casino_sessions_new
|
||||
SELECT id, chat_id, game_type, banker_id, min_bet, max_bet,
|
||||
multiplier, house_fee, status, created_at, settled_at,
|
||||
current_phase, blackjack_multiplier
|
||||
FROM casino_sessions
|
||||
""")
|
||||
elif has_current_phase:
|
||||
# 只有current_phase字段
|
||||
cursor.execute("""
|
||||
INSERT INTO casino_sessions_new
|
||||
SELECT id, chat_id, game_type, banker_id, min_bet, max_bet,
|
||||
multiplier, house_fee, status, created_at, settled_at,
|
||||
current_phase, 1.5 as blackjack_multiplier
|
||||
FROM casino_sessions
|
||||
""")
|
||||
elif has_blackjack_multiplier:
|
||||
# 只有blackjack_multiplier字段
|
||||
cursor.execute("""
|
||||
INSERT INTO casino_sessions_new
|
||||
SELECT id, chat_id, game_type, banker_id, min_bet, max_bet,
|
||||
multiplier, house_fee, status, created_at, settled_at,
|
||||
'betting' as current_phase, blackjack_multiplier
|
||||
FROM casino_sessions
|
||||
""")
|
||||
else:
|
||||
# 都没有新字段,使用默认值
|
||||
cursor.execute("""
|
||||
INSERT INTO casino_sessions_new
|
||||
SELECT id, chat_id, game_type, banker_id, min_bet, max_bet,
|
||||
multiplier, house_fee, status, created_at, settled_at,
|
||||
'betting' as current_phase, 1.5 as blackjack_multiplier
|
||||
FROM casino_sessions
|
||||
""")
|
||||
# 删除旧表
|
||||
cursor.execute("DROP TABLE casino_sessions")
|
||||
# 重命名新表
|
||||
cursor.execute("ALTER TABLE casino_sessions_new RENAME TO casino_sessions")
|
||||
# 重新启用外键检查
|
||||
cursor.execute("PRAGMA foreign_keys=ON")
|
||||
logger.info("成功重建casino_sessions表,移除UNIQUE约束")
|
||||
except Exception as e:
|
||||
# 如果迁移失败,记录日志但不影响正常运行
|
||||
logger.warning(f"迁移casino_sessions表时出现错误(可能表结构已更新): {e}")
|
||||
|
||||
# 21点手牌表
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS casino_blackjack_hands (
|
||||
|
||||
Reference in New Issue
Block a user