177 lines
7.0 KiB
Python
177 lines
7.0 KiB
Python
"""初始化 Web 后台数据库结构
|
|
|
|
Revision ID: 20260623_0001
|
|
Revises:
|
|
Create Date: 2026-06-23
|
|
"""
|
|
|
|
from collections.abc import Sequence
|
|
|
|
import sqlalchemy as sa
|
|
from alembic import op
|
|
|
|
revision: str = "20260623_0001"
|
|
down_revision: str | None = None
|
|
branch_labels: str | Sequence[str] | None = None
|
|
depends_on: 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.Text(), nullable=False),
|
|
sa.Column("email", sa.Text(), nullable=False),
|
|
sa.Column("email_password", sa.Text(), 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.Text(), nullable=True),
|
|
sa.Column("http", sa.Text(), nullable=True),
|
|
sa.Column("https", sa.Text(), nullable=True),
|
|
sa.Column("whitelist_enabled", sa.Boolean(), nullable=True),
|
|
sa.Column("whitelist_uid", sa.Text(), nullable=True),
|
|
sa.Column("whitelist_ukey", sa.Text(), 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")
|