Skip to content

Commit

Permalink
feat: switch to session merge for simplicity
Browse files Browse the repository at this point in the history
  • Loading branch information
lchen-2101 committed Jan 8, 2024
1 parent 7d85135 commit 6ec29f3
Showing 1 changed file with 1 addition and 9 deletions.
10 changes: 1 addition & 9 deletions src/entities/repos/institutions_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,9 @@ async def get_federal_regulators(session: AsyncSession) -> List[FederalRegulator

async def upsert_institution(session: AsyncSession, fi: FinancialInstitutionDto) -> FinancialInstitutionDao:
async with session.begin():
stmt = select(FinancialInstitutionDao).filter(FinancialInstitutionDao.lei == fi.lei)
res = await session.execute(stmt)
db_fi = res.scalar_one_or_none()
fi_data = fi.__dict__.copy()
fi_data.pop("_sa_instance_state", None)
if db_fi is None:
db_fi = FinancialInstitutionDao(**fi_data)
session.add(db_fi)
else:
for key, value in fi_data.items():
setattr(db_fi, key, value)
db_fi = await session.merge(FinancialInstitutionDao(**fi_data))
await session.flush([db_fi])
await session.refresh(db_fi)
return db_fi
Expand Down

0 comments on commit 6ec29f3

Please sign in to comment.