Kamixitong/fix_license_table.py
2025-11-11 21:39:12 +08:00

135 lines
5.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
修复license表结构
"""
import sys
import os
import sqlite3
# 添加项目路径
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from app import create_app
def fix_license_table():
"""修复license表结构"""
app = create_app()
with app.app_context():
try:
# 获取数据库路径
db_uri = app.config['SQLALCHEMY_DATABASE_URI']
if db_uri.startswith('sqlite:///'):
db_path = db_uri[10:] # 移除 'sqlite:///' 前缀
print(f"数据库路径: {db_path}")
# 连接到SQLite数据库
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# 列出所有表
cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
tables = cursor.fetchall()
print("=== 数据库中的所有表 ===")
for table in tables:
print(f"表名: {table[0]}")
# 检查是否有license表
if any(table[0] == 'license' for table in tables):
print("\n找到license表检查表结构...")
# 检查当前表结构
cursor.execute("PRAGMA table_info(license)")
columns = cursor.fetchall()
print("=== 当前License表结构 ===")
for column in columns:
print(f"列名: {column[1]}, 类型: {column[2]}, 是否可为空: {column[3]}, 默认值: {column[4]}, 是否为主键: {column[5]}")
# 检查license_key列
license_key_info = None
for column in columns:
if column[1] == 'license_key':
license_key_info = column
break
if license_key_info:
current_type = license_key_info[2]
print(f"\n当前license_key列类型: {current_type}")
if "VARCHAR(32)" in current_type.upper():
print("需要更新license_key列类型为VARCHAR(35)")
# SQLite修改表结构的方法
# 1. 创建新表
# 2. 复制数据
# 3. 删除旧表
# 4. 重命名新表
# 开始事务
cursor.execute("BEGIN TRANSACTION")
# 1. 创建新表修改license_key列长度
cursor.execute("""
CREATE TABLE license_new (
license_id INTEGER NOT NULL PRIMARY KEY,
license_key VARCHAR(35) NOT NULL,
product_id VARCHAR(32) NOT NULL,
type INTEGER NOT NULL,
status INTEGER NOT NULL,
valid_days INTEGER NOT NULL,
bind_machine_code VARCHAR(64),
activate_time DATETIME,
expire_time DATETIME,
last_verify_time DATETIME,
unbind_count INTEGER,
create_time DATETIME,
update_time DATETIME
)
""")
# 2. 复制数据
cursor.execute("""
INSERT INTO license_new
SELECT license_id, license_key, product_id, type, status, valid_days,
bind_machine_code, activate_time, expire_time, last_verify_time,
unbind_count, create_time, update_time
FROM license
""")
# 3. 删除旧表
cursor.execute("DROP TABLE license")
# 4. 重命名新表
cursor.execute("ALTER TABLE license_new RENAME TO license")
# 5. 重新创建索引
cursor.execute("CREATE UNIQUE INDEX ix_license_license_key ON license (license_key)")
cursor.execute("CREATE INDEX ix_license_product_id ON license (product_id)")
# 提交事务
conn.commit()
print("✓ 成功更新license_key列类型为VARCHAR(35)")
elif "VARCHAR(35)" in current_type.upper():
print("✓ license_key列已经是正确的长度")
else:
print(f"⚠ license_key列类型未知: {current_type}")
else:
print("✗ 未找到license_key列!")
else:
print("✗ 未找到license表!")
# 关闭连接
conn.close()
except Exception as e:
print(f"修复数据库结构时出错: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
fix_license_table()