feat: track imported accounts per upstream key across platforms

Add upstream_key_account_links table mapping generated keys to remote
accounts per website/platform, surface imported_accounts on key
responses, and update website sync/routers to manage the links.
This commit is contained in:
liujing
2026-07-23 17:52:58 +08:00
parent ee89cd6119
commit df7e400e7b
17 changed files with 1081 additions and 537 deletions
+42
View File
@@ -47,6 +47,8 @@ def init_db():
_migrate_custom_pages()
_migrate_upstreams()
_migrate_upstream_generated_keys()
_migrate_website_group_bindings_platform()
_migrate_upstream_key_account_links()
# ── 已有数据库幂等索引迁移 ─────────────────────────────────
@@ -197,3 +199,43 @@ def _migrate_upstream_generated_keys():
except Exception:
logger = __import__("logging").getLogger(__name__)
logger.warning("could not create unique indexes on upstream_generated_keys (non-fatal)")
def _migrate_upstream_key_account_links():
"""Create the multi-platform account mapping table and indexes."""
inspector = inspect(engine)
if "upstream_key_account_links" not in inspector.get_table_names():
with engine.begin() as conn:
conn.execute(text("""
CREATE TABLE IF NOT EXISTS upstream_key_account_links (
id INTEGER PRIMARY KEY AUTOINCREMENT,
upstream_key_id INTEGER NOT NULL REFERENCES upstream_generated_keys(id) ON DELETE CASCADE,
website_id INTEGER NOT NULL REFERENCES websites(id) ON DELETE CASCADE,
target_group VARCHAR(255) NOT NULL DEFAULT 'default',
platform VARCHAR(64) NOT NULL,
remote_account_id VARCHAR(255) NOT NULL,
status VARCHAR(32) NOT NULL DEFAULT 'active',
created_at DATETIME,
updated_at DATETIME
)
"""))
with engine.begin() as conn:
conn.execute(text("""
CREATE UNIQUE INDEX IF NOT EXISTS uq_key_website_platform
ON upstream_key_account_links(upstream_key_id, website_id, platform)
"""))
conn.execute(text("""
CREATE UNIQUE INDEX IF NOT EXISTS uq_website_remote_account
ON upstream_key_account_links(website_id, remote_account_id)
"""))
def _migrate_website_group_bindings_platform():
"""Add platform column to website_group_bindings if missing."""
inspector = inspect(engine)
if "website_group_bindings" not in inspector.get_table_names():
return
columns = {col["name"] for col in inspector.get_columns("website_group_bindings")}
if "platform" not in columns:
with engine.begin() as conn:
conn.execute(text("ALTER TABLE website_group_bindings ADD COLUMN platform VARCHAR(64)"))