fix(account): 支持跨平台同名账号——(platform,name)联合唯一+Asset.account_id外键重构

This commit is contained in:
gouki
2026-08-11 07:27:01 +00:00
parent bc1b804931
commit b4705331c0
9 changed files with 209 additions and 66 deletions
+10 -10
View File
@@ -169,7 +169,7 @@ def _find_asset(session: Session, provider_id: int, external_id: str, asset_type
).first()
def _sync_vps(session: Session, provider: Provider, adapter: BaseAdapter, account_name: Optional[str] = None) -> dict:
def _sync_vps(session: Session, provider: Provider, adapter: BaseAdapter, account_id: Optional[int] = None) -> dict:
created = updated = 0
for vps in adapter.list_vps():
existing = _find_asset(session, provider.id, vps.external_id, AssetType.VPS)
@@ -179,8 +179,8 @@ def _sync_vps(session: Session, provider: Provider, adapter: BaseAdapter, accoun
if vps.monthly_cost is not None:
existing.cost = vps.monthly_cost
existing.currency = vps.currency
if account_name:
existing.account = account_name
if account_id:
existing.account_id = account_id
session.add(existing)
detail = session.exec(
select(VPSDetail).where(VPSDetail.asset_id == existing.id)
@@ -201,7 +201,7 @@ def _sync_vps(session: Session, provider: Provider, adapter: BaseAdapter, accoun
provider=provider.slug,
provider_id=provider.id,
external_id=vps.external_id,
account=account_name,
account_id=account_id,
status=_norm_status(vps.status),
cost=vps.monthly_cost or 0,
currency=vps.currency,
@@ -224,7 +224,7 @@ def _sync_vps(session: Session, provider: Provider, adapter: BaseAdapter, accoun
return {"created": created, "updated": updated}
def _sync_domains(session: Session, provider: Provider, adapter: BaseAdapter, account_name: Optional[str] = None) -> dict:
def _sync_domains(session: Session, provider: Provider, adapter: BaseAdapter, account_id: Optional[int] = None) -> dict:
created = updated = 0
for dom in adapter.list_domains():
existing = _find_asset(session, provider.id, dom.external_id, AssetType.DOMAIN)
@@ -232,8 +232,8 @@ def _sync_domains(session: Session, provider: Provider, adapter: BaseAdapter, ac
existing.status = _norm_status(dom.status)
if dom.expiry_date:
existing.expiry_date = dom.expiry_date
if account_name:
existing.account = account_name
if account_id:
existing.account_id = account_id
session.add(existing)
detail = session.exec(
select(DomainDetail).where(DomainDetail.asset_id == existing.id)
@@ -250,7 +250,7 @@ def _sync_domains(session: Session, provider: Provider, adapter: BaseAdapter, ac
provider=provider.slug,
provider_id=provider.id,
external_id=dom.external_id,
account=account_name,
account_id=account_id,
status=_norm_status(dom.status),
expiry_date=dom.expiry_date,
)
@@ -295,12 +295,12 @@ def sync_account(session: Session, account_id: int) -> dict:
if caps["list_vps"]:
try:
result["vps"] = _sync_vps(session, provider, adapter, account.name)
result["vps"] = _sync_vps(session, provider, adapter, account.id)
except Exception as e: # noqa: BLE001
result["vps_error"] = str(e)
if caps["list_domains"]:
try:
result["domains"] = _sync_domains(session, provider, adapter, account.name)
result["domains"] = _sync_domains(session, provider, adapter, account.id)
except Exception as e: # noqa: BLE001
result["domains_error"] = str(e)
if caps["get_account"]: