refactor: 移除 SQLite 特有代码,完成 MySQL 迁移

This commit is contained in:
2026-01-23 17:57:15 +08:00
parent ef43b1bc57
commit fe99649f72

View File

@@ -4,7 +4,6 @@ import json
import os
from contextlib import contextmanager
from datetime import date, time, datetime, timedelta
from pathlib import Path
from typing import Optional
import mysql.connector
@@ -614,39 +613,23 @@ def ensure_default_user():
def migrate_auth_fields():
"""迁移:为现有 users 表添加认证字段"""
with get_connection() as conn:
cursor = conn.cursor()
"""迁移:为现有 users 表添加认证字段MySQL 版本)"""
with get_connection() as (conn, cursor):
# 检查列是否存在MySQL 方式
cursor.execute("""
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'users'
""")
columns = [row["COLUMN_NAME"] for row in cursor.fetchall()]
# 检查 users 表是否有 password_hash 列
cursor.execute("PRAGMA table_info(users)")
columns = [col["name"] for col in cursor.fetchall()]
# 添加缺失的列
if "password_hash" not in columns:
cursor.execute("ALTER TABLE users ADD COLUMN password_hash TEXT")
cursor.execute("ALTER TABLE users ADD COLUMN password_hash VARCHAR(255)")
if "email" not in columns:
cursor.execute("ALTER TABLE users ADD COLUMN email TEXT")
cursor.execute("ALTER TABLE users ADD COLUMN email VARCHAR(255)")
if "is_admin" not in columns:
cursor.execute("ALTER TABLE users ADD COLUMN is_admin INTEGER DEFAULT 0")
cursor.execute("ALTER TABLE users ADD COLUMN is_admin TINYINT DEFAULT 0")
if "is_disabled" not in columns:
cursor.execute("ALTER TABLE users ADD COLUMN is_disabled INTEGER DEFAULT 0")
# 检查 invites 表是否存在
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='invites'")
if not cursor.fetchone():
cursor.execute("""
CREATE TABLE IF NOT EXISTS invites (
id INTEGER PRIMARY KEY AUTOINCREMENT,
code TEXT NOT NULL UNIQUE,
created_by INTEGER NOT NULL,
used_by INTEGER,
created_at TEXT NOT NULL,
expires_at TEXT,
FOREIGN KEY (created_by) REFERENCES users(id),
FOREIGN KEY (used_by) REFERENCES users(id)
)
""")
cursor.execute("ALTER TABLE users ADD COLUMN is_disabled TINYINT DEFAULT 0")
# ===== 数据清除 =====