11from datetime import datetime , timezone
22
33from loguru import logger
4- from pydantic import TypeAdapter , ValidationError
54from tenacity import retry , retry_if_exception_type , stop_after_attempt , wait_fixed
65
76from crowdgit .enums import RepositoryPriority , RepositoryState
87from crowdgit .errors import RepoLockingError
9- from crowdgit .models .affiliation_info import AffiliationInfoItem
8+ from crowdgit .models .affiliation_info import RepoAffiliationRegistry
109from crowdgit .models .repository import Repository
1110from crowdgit .models .service_execution import ServiceExecution
1211from crowdgit .settings import (
@@ -528,24 +527,7 @@ async def save_service_execution(service_execution: ServiceExecution) -> None:
528527 # Do not re-raise - we don't want metrics saving to disrupt main operations
529528
530529
531- _AFFILIATION_SNAPSHOT_ADAPTER = TypeAdapter (list [AffiliationInfoItem ])
532-
533-
534- def parse_affiliation_snapshot (snapshot ) -> list [AffiliationInfoItem ] | None :
535- if isinstance (snapshot , dict ) and "affiliations" in snapshot :
536- snapshot = snapshot ["affiliations" ]
537- try :
538- return _AFFILIATION_SNAPSHOT_ADAPTER .validate_python (snapshot )
539- except ValidationError as error :
540- logger .warning (f"Invalid affiliation snapshot in registry, will re-parse: { error } " )
541- return None
542-
543-
544- def dump_affiliation_snapshot (affiliations : list [AffiliationInfoItem ]) -> list [dict ]:
545- return [item .model_dump () for item in affiliations ]
546-
547-
548- async def get_repo_affiliation_registry (repo_id : str ):
530+ async def get_repo_affiliation_registry (repo_id : str ) -> RepoAffiliationRegistry | None :
549531 sql_query = """
550532 SELECT "filePath", "fileHash", "status", "snapshot", "lastRunAt"
551533 FROM git."repoAffiliationRegistry"
@@ -556,28 +538,12 @@ async def get_repo_affiliation_registry(repo_id: str):
556538 return None
557539
558540 row = dict (result )
559- snapshot = row .get ("snapshot" )
560- if snapshot is not None :
561- snapshot = parse_affiliation_snapshot (snapshot )
562-
563- return {
564- "file_path" : row .get ("filePath" ),
565- "file_hash" : row .get ("fileHash" ),
566- "status" : row .get ("status" ),
567- "snapshot" : snapshot ,
568- "last_run_at" : row .get ("lastRunAt" ),
569- }
541+ row ["repoId" ] = repo_id
542+ return RepoAffiliationRegistry .from_db (row )
570543
571544
572- async def upsert_repo_affiliation_registry (
573- repo_id : str ,
574- * ,
575- file_path : str | None ,
576- file_hash : str | None ,
577- status : str ,
578- snapshot : list [AffiliationInfoItem ] | None ,
579- ) -> None :
580- snapshot_json = dump_affiliation_snapshot (snapshot ) if snapshot is not None else None
545+ async def upsert_repo_affiliation_registry (registry : RepoAffiliationRegistry ) -> None :
546+ snapshot_json = registry .snapshot_for_db ()
581547 sql_query = """
582548 INSERT INTO git."repoAffiliationRegistry" (
583549 "repoId", "filePath", "fileHash", "status", "snapshot", "lastRunAt", "updatedAt"
@@ -593,7 +559,13 @@ async def upsert_repo_affiliation_registry(
593559 """
594560 await execute (
595561 sql_query ,
596- (repo_id , file_path , file_hash , status , snapshot_json ),
562+ (
563+ registry .repo_id ,
564+ registry .file_path ,
565+ registry .file_hash ,
566+ registry .status ,
567+ snapshot_json ,
568+ ),
597569 )
598570
599571
@@ -750,9 +722,9 @@ async def fetch_segment_affiliations(member_ids: list[str], segment_id: str) ->
750722 )
751723
752724
753- async def insert_member_organizations (rows : list [dict ]) -> int :
725+ async def insert_member_organizations (rows : list [dict ]) -> None :
754726 if not rows :
755- return 0
727+ return
756728
757729 sql_query = """
758730 INSERT INTO "memberOrganizations"(
@@ -780,12 +752,11 @@ async def insert_member_organizations(rows: list[dict]) -> int:
780752 for row in rows
781753 ],
782754 )
783- return len (rows )
784755
785756
786- async def insert_member_segment_affiliations (rows : list [dict ]) -> int :
757+ async def insert_member_segment_affiliations (rows : list [dict ]) -> None :
787758 if not rows :
788- return 0
759+ return
789760
790761 sql_query = """
791762 INSERT INTO "memberSegmentAffiliations"(
@@ -811,4 +782,3 @@ async def insert_member_segment_affiliations(rows: list[dict]) -> int:
811782 for row in rows
812783 ],
813784 )
814- return len (rows )
0 commit comments