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:
yml2213
2026-06-23 08:35:29 +08:00
parent 2ab6724543
commit dd56de9bd4
40 changed files with 2006 additions and 936 deletions
+12
View File
@@ -0,0 +1,12 @@
# 数据库迁移
本目录由 Alembic 管理 Web 后台数据库结构。
常用命令:
```bash
uv run alembic upgrade head
uv run alembic revision -m "描述"
```
应用启动时会自动执行 `upgrade head`,本地开发通常不需要手动运行迁移命令。
+57
View File
@@ -0,0 +1,57 @@
"""Alembic 迁移环境。"""
from logging.config import fileConfig
from pathlib import Path
import sys
from alembic import context
from sqlalchemy import engine_from_config, pool
ROOT_DIR = Path(__file__).resolve().parents[3]
if str(ROOT_DIR) not in sys.path:
sys.path.insert(0, str(ROOT_DIR))
from web.backend.database import Base, DATABASE_URL # noqa: E402
from web.backend import models # noqa: F401,E402
config = context.config
config.set_main_option("sqlalchemy.url", DATABASE_URL)
if config.config_file_name is not None:
fileConfig(config.config_file_name)
target_metadata = Base.metadata
def run_migrations_offline() -> None:
"""离线模式生成 SQL。"""
context.configure(
url=DATABASE_URL,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online() -> None:
"""在线模式直接执行迁移。"""
connectable = engine_from_config(
config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
with connectable.connect() as connection:
context.configure(connection=connection, target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
+25
View File
@@ -0,0 +1,25 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
revision: str = ${repr(up_revision)}
down_revision: Union[str, None] = ${repr(down_revision)}
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
def upgrade() -> None:
${upgrades if upgrades else "pass"}
def downgrade() -> None:
${downgrades if downgrades else "pass"}
@@ -0,0 +1,160 @@
"""初始化 Web 后台数据库结构
Revision ID: 20260623_0001
Revises:
Create Date: 2026-06-23
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
revision: str = "20260623_0001"
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def _has_table(bind, table_name: str) -> bool:
return sa.inspect(bind).has_table(table_name)
def _columns(bind, table_name: str) -> set[str]:
if not _has_table(bind, table_name):
return set()
return {column["name"] for column in sa.inspect(bind).get_columns(table_name)}
def _indexes(bind, table_name: str) -> set[str]:
if not _has_table(bind, table_name):
return set()
return {index["name"] for index in sa.inspect(bind).get_indexes(table_name)}
def _add_column_if_missing(bind, table_name: str, column: sa.Column) -> bool:
if column.name in _columns(bind, table_name):
return False
op.add_column(table_name, column)
return True
def _create_index_if_missing(bind, name: str, table_name: str, columns: list[str], unique: bool = False) -> None:
if name not in _indexes(bind, table_name):
op.create_index(name, table_name, columns, unique=unique)
def upgrade() -> None:
bind = op.get_bind()
if not _has_table(bind, "users"):
op.create_table(
"users",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("username", sa.String(length=64), nullable=False),
sa.Column("password_hash", sa.String(length=256), nullable=False),
sa.Column("role", sa.String(length=32), nullable=False),
sa.Column("is_active", sa.Boolean(), nullable=True),
sa.Column("remark", sa.String(length=256), nullable=True),
sa.Column("custom_permissions", sa.JSON(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=True),
sa.Column("updated_at", sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint("id"),
)
else:
_add_column_if_missing(bind, "users", sa.Column("custom_permissions", sa.JSON(), nullable=True))
_create_index_if_missing(bind, "ix_users_username", "users", ["username"], unique=True)
if not _has_table(bind, "accounts"):
op.create_table(
"accounts",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("username", sa.String(length=128), nullable=False),
sa.Column("password", sa.String(length=256), nullable=False),
sa.Column("email", sa.String(length=128), nullable=False),
sa.Column("email_password", sa.String(length=256), nullable=False),
sa.Column("email_imap_server", sa.String(length=128), nullable=True),
sa.Column("email_imap_port", sa.Integer(), nullable=True),
sa.Column("email_imap_ssl", sa.Boolean(), nullable=True),
sa.Column("assigned_to", sa.Integer(), nullable=True),
sa.Column("tag", sa.String(length=64), nullable=True),
sa.Column("remark", sa.String(length=256), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=True),
sa.ForeignKeyConstraint(["assigned_to"], ["users.id"]),
sa.PrimaryKeyConstraint("id"),
)
else:
_add_column_if_missing(bind, "accounts", sa.Column("tag", sa.String(length=64), server_default=""))
added_ssl = _add_column_if_missing(bind, "accounts", sa.Column("email_imap_ssl", sa.Boolean(), server_default=sa.text("1")))
if added_ssl:
op.execute(
"UPDATE accounts SET email_imap_port = 143, email_imap_ssl = 0 "
"WHERE email_imap_server = '111.229.206.54'"
)
op.execute(
"UPDATE accounts SET email_imap_port = 143, email_imap_ssl = 0 "
"WHERE email_imap_server = 'mail.bdhg.xyz'"
)
_create_index_if_missing(bind, "ix_accounts_assigned_to", "accounts", ["assigned_to"])
if not _has_table(bind, "proxy_config"):
op.create_table(
"proxy_config",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("enabled", sa.Boolean(), nullable=True),
sa.Column("api_url", sa.String(length=512), nullable=True),
sa.Column("http", sa.String(length=256), nullable=True),
sa.Column("https", sa.String(length=256), nullable=True),
sa.Column("whitelist_enabled", sa.Boolean(), nullable=True),
sa.Column("whitelist_uid", sa.String(length=64), nullable=True),
sa.Column("whitelist_ukey", sa.String(length=128), nullable=True),
sa.PrimaryKeyConstraint("id"),
)
if not _has_table(bind, "login_tasks"):
op.create_table(
"login_tasks",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("batch_id", sa.String(length=64), nullable=False),
sa.Column("account_id", sa.Integer(), nullable=False),
sa.Column("status", sa.String(length=32), nullable=True),
sa.Column("cookie", sa.Text(), nullable=True),
sa.Column("message", sa.String(length=512), nullable=True),
sa.Column("created_by", sa.Integer(), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=True),
sa.Column("finished_at", sa.DateTime(), nullable=True),
sa.ForeignKeyConstraint(["account_id"], ["accounts.id"]),
sa.ForeignKeyConstraint(["created_by"], ["users.id"]),
sa.PrimaryKeyConstraint("id"),
)
_create_index_if_missing(bind, "ix_login_tasks_batch_id", "login_tasks", ["batch_id"])
if not _has_table(bind, "audit_logs"):
op.create_table(
"audit_logs",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("user_id", sa.Integer(), nullable=True),
sa.Column("username", sa.String(length=64), nullable=True),
sa.Column("action", sa.String(length=128), nullable=False),
sa.Column("target", sa.String(length=256), nullable=True),
sa.Column("detail", sa.Text(), nullable=True),
sa.Column("created_at", sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint("id"),
)
def downgrade() -> None:
bind = op.get_bind()
if _has_table(bind, "audit_logs"):
op.drop_table("audit_logs")
if _has_table(bind, "login_tasks"):
op.drop_index("ix_login_tasks_batch_id", table_name="login_tasks")
op.drop_table("login_tasks")
if _has_table(bind, "proxy_config"):
op.drop_table("proxy_config")
if _has_table(bind, "accounts"):
op.drop_index("ix_accounts_assigned_to", table_name="accounts")
op.drop_table("accounts")
if _has_table(bind, "users"):
op.drop_index("ix_users_username", table_name="users")
op.drop_table("users")