Skip to content

modify to use upsert and alchemy style join - #27

Merged
heinrich10 merged 1 commit into
mainfrom
fix/repository_operations
May 31, 2026
Merged

modify to use upsert and alchemy style join#27
heinrich10 merged 1 commit into
mainfrom
fix/repository_operations

Conversation

@heinrich10

Copy link
Copy Markdown
Owner

What?

Modify persisting and loading data for persons

Why?

on loading data: this ensures consistent eager loading and prevents potential N+1 lazy-load issues during serialization
on persisting: upsert would make the insert or update operation atomic

How?

code change

Testing?

add integration tests to cover upsert

AI Summary (optional)

This pull request updates the PersonRepository to use an upsert (update-or-insert) approach for saving Person records and adds corresponding tests to ensure correct behavior. The main changes are the introduction of a database-level upsert using SQLite's ON CONFLICT DO UPDATE and the addition of tests to verify both creation and update via upsert.

Repository improvements:

  • Changed the update_or_create_person method in PersonRepository to use a SQLite insert statement with on_conflict_do_update, enabling true upsert behavior instead of relying on SQLAlchemy's merge method. This ensures that if a Person with the given ID exists, it is updated; otherwise, a new record is created.
  • Updated the get_person method to use joinedload for eager loading of the related country, improving query efficiency and code clarity.

Testing improvements:

  • Added test_upsert_create to verify that a PUT request for a non-existent Person ID creates a new record as expected.
  • Added test_upsert_update to verify that a PUT request for an existing Person ID updates the record correctly.

@github-actions

Copy link
Copy Markdown

☂️ Python Coverage

current status: ✅

Overall Coverage

Lines Covered Coverage Threshold Status
960 938 98% 0% 🟢

New Files

No new covered files...

Modified Files

File Coverage Status
pyfastapi/repositories/person.py 100% 🟢
tests/api_tests/test_persons.py 100% 🟢
TOTAL 100% 🟢

updated for commit: 4b1fb54 by action🐍

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors PersonRepository to use SQLite's ON CONFLICT DO UPDATE for true upsert semantics in update_or_create_person, and switches get_person to use joinedload(Person.country) for eager loading. Adds two integration tests covering create-via-PUT and update-via-PUT.

Changes:

  • update_or_create_person now uses sqlalchemy.dialects.sqlite.insert(...).on_conflict_do_update(...) instead of session.merge(person).
  • get_person replaces a manual join(Country, ...) with select(Person).options(joinedload(Person.country)), and the unused Country import is removed.
  • New tests test_upsert_create and test_upsert_update verify both branches of the upsert through the PUT endpoint.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
pyfastapi/repositories/person.py Switches to SQLite INSERT ... ON CONFLICT DO UPDATE for upsert and uses joinedload for Person.country.
tests/api_tests/test_persons.py Adds test_upsert_create and test_upsert_update integration tests for the PUT endpoint.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@heinrich10
heinrich10 merged commit 7da7498 into main May 31, 2026
2 checks passed
@heinrich10
heinrich10 deleted the fix/repository_operations branch May 31, 2026 04:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants