From fe99649f72ba2fc868cb6e7ec106ec400fb125fa Mon Sep 17 00:00:00 2001 From: "liweiliang0905@gmail.com" Date: Fri, 23 Jan 2026 17:57:15 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=20SQLite=20?= =?UTF-8?q?=E7=89=B9=E6=9C=89=E4=BB=A3=E7=A0=81=EF=BC=8C=E5=AE=8C=E6=88=90?= =?UTF-8?q?=20MySQL=20=E8=BF=81=E7=A7=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/vitals/core/database.py | 41 +++++++++++-------------------------- 1 file changed, 12 insertions(+), 29 deletions(-) diff --git a/src/vitals/core/database.py b/src/vitals/core/database.py index d722e5e..1ce6985 100644 --- a/src/vitals/core/database.py +++ b/src/vitals/core/database.py @@ -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") # ===== 数据清除 =====