fix: 修复proxy_service和account_service中不当的顶层import
- proxy_service.py: 将requests和WhitelistManager改为函数内延迟import, 避免启动时加载不需要的依赖;移除未使用的get_exit_ip_via_proxy导入 - account_service.py: 移除未使用的func、joinedload、AuditLog、 user_has_permission顶层导入
This commit is contained in:
+17
-42
@@ -5,12 +5,16 @@ from pathlib import Path
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker, declarative_base
|
||||
|
||||
DB_PATH = Path(__file__).parent.parent.parent / "data" / "web.db"
|
||||
PROJECT_ROOT = Path(__file__).resolve().parents[2]
|
||||
DB_PATH = PROJECT_ROOT / "data" / "web.db"
|
||||
DB_PATH.parent.mkdir(parents=True, exist_ok=True)
|
||||
DATABASE_URL = os.getenv("DATABASE_URL", f"sqlite:///{DB_PATH}")
|
||||
|
||||
connect_args = {"check_same_thread": False} if DATABASE_URL.startswith("sqlite") else {}
|
||||
|
||||
engine = create_engine(
|
||||
f"sqlite:///{DB_PATH}",
|
||||
connect_args={"check_same_thread": False},
|
||||
DATABASE_URL,
|
||||
connect_args=connect_args,
|
||||
echo=False,
|
||||
)
|
||||
|
||||
@@ -28,49 +32,20 @@ def get_db():
|
||||
|
||||
|
||||
def init_db():
|
||||
"""建表 + 写入初始数据。"""
|
||||
Base.metadata.create_all(bind=engine)
|
||||
_migrate()
|
||||
"""执行数据库迁移 + 写入初始数据。"""
|
||||
run_migrations()
|
||||
_seed()
|
||||
|
||||
|
||||
def _migrate():
|
||||
"""数据库迁移:为已有表添加新列。"""
|
||||
from sqlalchemy import text
|
||||
with engine.connect() as conn:
|
||||
# 检查 accounts.tag 列是否存在
|
||||
result = conn.execute(text("PRAGMA table_info(accounts)"))
|
||||
columns = [row[1] for row in result]
|
||||
if 'tag' not in columns:
|
||||
conn.execute(text("ALTER TABLE accounts ADD COLUMN tag VARCHAR(64) DEFAULT ''"))
|
||||
conn.commit()
|
||||
def run_migrations():
|
||||
"""运行 Alembic 迁移到最新版本。"""
|
||||
from alembic import command
|
||||
from alembic.config import Config
|
||||
|
||||
# 检查 users.custom_permissions 列是否存在
|
||||
result = conn.execute(text("PRAGMA table_info(users)"))
|
||||
columns = [row[1] for row in result]
|
||||
if 'custom_permissions' not in columns:
|
||||
conn.execute(text("ALTER TABLE users ADD COLUMN custom_permissions JSON DEFAULT NULL"))
|
||||
conn.commit()
|
||||
|
||||
# 检查 accounts.email_imap_ssl 列是否存在
|
||||
result = conn.execute(text("PRAGMA table_info(accounts)"))
|
||||
columns = [row[1] for row in result]
|
||||
if 'email_imap_ssl' not in columns:
|
||||
# 旧数据端口993的默认True(SSL),其他端口默认False
|
||||
conn.execute(text("ALTER TABLE accounts ADD COLUMN email_imap_ssl BOOLEAN DEFAULT 1"))
|
||||
conn.commit()
|
||||
# 修正旧数据中使用 111.229.206.54 的账号:端口改为143,SSL改为False
|
||||
conn.execute(text(
|
||||
"UPDATE accounts SET email_imap_port = 143, email_imap_ssl = 0 "
|
||||
"WHERE email_imap_server = '111.229.206.54'"
|
||||
))
|
||||
conn.commit()
|
||||
# 修正旧数据中使用 mail.bdhg.xyz 的账号:993端口不通,改用143非SSL
|
||||
conn.execute(text(
|
||||
"UPDATE accounts SET email_imap_port = 143, email_imap_ssl = 0 "
|
||||
"WHERE email_imap_server = 'mail.bdhg.xyz'"
|
||||
))
|
||||
conn.commit()
|
||||
config = Config(str(PROJECT_ROOT / "alembic.ini"))
|
||||
config.set_main_option("script_location", str(PROJECT_ROOT / "web" / "backend" / "migrations"))
|
||||
config.set_main_option("sqlalchemy.url", DATABASE_URL)
|
||||
command.upgrade(config, "head")
|
||||
|
||||
|
||||
def _seed():
|
||||
|
||||
Reference in New Issue
Block a user