feat: 上游 Key 唯一化、分组导入跳过、账号导入平台识别&远端校验&base_url 注入
- 上游 Key 命名改为 {prefix}-{upstream.id}-{safe_group_name}-{group_id}
- 唯一约束 (upstream_id, group_id, managed_prefix) 加 managed_prefix 列
- 上游检测成功时同步 Key 状态,远端已删/分组已删自动清理
- 重复分组导入跳过,目标网站已存在同名分组返回 exists
- 账号导入平台自动识别(auto/manual 模式)
- 全选可导入 Key 按钮 + 目标分组自动匹配
- 导入幂等:已导入过的 Key 校验远端账号,不存在则重建
- 新增同步接口 POST /sync-imported-upstream-keys
- account_exists() 通过拉取账号列表判断,避免 404 误判
- credentials.base_url 注入来源上游地址,避免 401
- 前端导入弹窗自动同步+刷新按钮+并发/优先级设置
- 新增 12 个测试覆盖同步、幂等、远端删除、校验失败路径
This commit is contained in:
+58
-1
@@ -26,10 +26,11 @@ def get_db():
|
||||
def init_db():
|
||||
"""Create all tables."""
|
||||
# import models so SQLAlchemy registers them
|
||||
from app.models import admin_user, upstream, snapshot, webhook_config, notification_log, custom_page, website, revoked_token # noqa: F401
|
||||
from app.models import admin_user, upstream, snapshot, webhook_config, notification_log, custom_page, website, revoked_token, upstream_key # noqa: F401
|
||||
Base.metadata.create_all(bind=engine)
|
||||
_migrate_custom_pages()
|
||||
_migrate_upstreams()
|
||||
_migrate_upstream_generated_keys()
|
||||
|
||||
|
||||
def _migrate_custom_pages():
|
||||
@@ -87,3 +88,59 @@ def _migrate_upstreams():
|
||||
if "balance_divisor" not in columns:
|
||||
conn.execute(text("ALTER TABLE upstreams ADD COLUMN balance_divisor FLOAT NOT NULL DEFAULT 1.0"))
|
||||
|
||||
|
||||
def _migrate_upstream_generated_keys():
|
||||
"""Apply SQLite-safe migrations to the generated upstream keys table."""
|
||||
inspector = inspect(engine)
|
||||
if "upstream_generated_keys" not in inspector.get_table_names():
|
||||
return
|
||||
columns = {col["name"] for col in inspector.get_columns("upstream_generated_keys")}
|
||||
with engine.begin() as conn:
|
||||
if "imported_website_id" not in columns:
|
||||
conn.execute(text("ALTER TABLE upstream_generated_keys ADD COLUMN imported_website_id INTEGER"))
|
||||
if "imported_account_id" not in columns:
|
||||
conn.execute(text("ALTER TABLE upstream_generated_keys ADD COLUMN imported_account_id VARCHAR(255)"))
|
||||
if "imported_at" not in columns:
|
||||
conn.execute(text("ALTER TABLE upstream_generated_keys ADD COLUMN imported_at DATETIME"))
|
||||
if "updated_at" not in columns:
|
||||
conn.execute(text("ALTER TABLE upstream_generated_keys ADD COLUMN updated_at DATETIME"))
|
||||
conn.execute(text("UPDATE upstream_generated_keys SET updated_at = created_at WHERE updated_at IS NULL"))
|
||||
if "managed_prefix" not in columns:
|
||||
conn.execute(text("ALTER TABLE upstream_generated_keys ADD COLUMN managed_prefix VARCHAR(64)"))
|
||||
|
||||
# ——— 历史数据迁移:回填 managed_prefix + 清理重复 ———
|
||||
with engine.begin() as conn:
|
||||
# 1. 回填:key_name 以 SmartUp- 开头的旧记录设置 managed_prefix = 'SmartUp'
|
||||
conn.execute(text(
|
||||
"UPDATE upstream_generated_keys SET managed_prefix = 'SmartUp' "
|
||||
"WHERE managed_prefix IS NULL AND key_name LIKE 'SmartUp-%'"
|
||||
))
|
||||
# 2. 清理:同一 (upstream_id, group_id, managed_prefix) 只保留最新一条
|
||||
# SQLite 不支持子查询直接 DELETE,用两步
|
||||
to_delete = conn.execute(text("""
|
||||
SELECT id FROM upstream_generated_keys
|
||||
WHERE managed_prefix IS NOT NULL
|
||||
AND id NOT IN (
|
||||
SELECT MAX(id) FROM upstream_generated_keys
|
||||
WHERE managed_prefix IS NOT NULL
|
||||
GROUP BY upstream_id, group_id, managed_prefix
|
||||
)
|
||||
""")).fetchall()
|
||||
for (row_id,) in to_delete:
|
||||
conn.execute(text("DELETE FROM upstream_generated_keys WHERE id = :id"), {"id": row_id})
|
||||
|
||||
# ——— 创建唯一索引 ———
|
||||
try:
|
||||
with engine.begin() as conn:
|
||||
conn.execute(
|
||||
text("CREATE UNIQUE INDEX IF NOT EXISTS uq_upstream_group_key "
|
||||
"ON upstream_generated_keys(upstream_id, group_id, key_name)")
|
||||
)
|
||||
conn.execute(
|
||||
text("CREATE UNIQUE INDEX IF NOT EXISTS uq_upstream_group_managed "
|
||||
"ON upstream_generated_keys(upstream_id, group_id, managed_prefix) "
|
||||
"WHERE managed_prefix IS NOT NULL")
|
||||
)
|
||||
except Exception:
|
||||
logger = __import__("logging").getLogger(__name__)
|
||||
logger.warning("could not create unique indexes on upstream_generated_keys (non-fatal)")
|
||||
|
||||
Reference in New Issue
Block a user