Skip to content

Make update method return Option[Row] instead of Boolean for all database libraries#139

Merged
oyvindberg merged 2 commits intomainfrom
update-returning
Jul 28, 2025
Merged

Make update method return Option[Row] instead of Boolean for all database libraries#139
oyvindberg merged 2 commits intomainfrom
update-returning

Conversation

@oyvindberg
Copy link
Copy Markdown
Collaborator

@oyvindberg oyvindberg commented Aug 30, 2024

Summary

This PR changes the update method in all database libraries to return Option[Row] instead of Boolean, providing better feedback about what was actually updated.

Changes

  • Method signature: update(row: Row): Booleanupdate(row: Row): Option[Row]
  • Implementation: All three database libraries now use PostgreSQL's RETURNING clause
    • Anorm: Uses executeInsert with singleOpt parser
    • Doobie: Uses .query(using RowType.read).option
    • ZIO-JDBC: Uses .query(jdbcDecoder).selectOne
  • Tests: Updated all tests to verify the returned Option contains expected values
  • Mocks: Updated mock implementations to return the row instead of boolean
  • Documentation: Updated examples in documentation to show new return type

Benefits

  1. Better feedback: Know exactly what values were updated in the database
  2. Verification: Can verify that database triggers, defaults, or computed columns worked as expected
  3. Consistency: Aligns with modern database practices of returning affected rows

Breaking Change

This is a breaking change for users of the update method. Migration is straightforward:

// Before
val success: Boolean = repo.update(row)

// After
val updatedRow: Option[Row] = repo.update(row)
val success: Boolean = updatedRow.isDefined

Test plan

  • All existing tests updated and passing
  • Verified update returns correct row data
  • Tested with all three database libraries (Anorm, Doobie, ZIO-JDBC)
  • Documentation builds successfully
  • No compilation warnings

Implementation Complete

All three database libraries have been updated and tested. The implementation is ready for review.

@hostgaard
Copy link
Copy Markdown

Is there any work remaining here to be able to merge? Maybe I can help?

@oyvindberg
Copy link
Copy Markdown
Collaborator Author

Yes, there is one bothersome thing. It should be completely fine for anorm now, but unfortunately there are two more libraries supported, and they should be in sync. The changes from DbLibAnorm need to be ported to DbLibDoobie and DbLibZioJdbc as well.

Feel free to ping me if you decide to try and get stuck

…base libraries

- Changed update method signature to return Option[Row] using PostgreSQL RETURNING clause
- Applied changes to all three database libraries:
  - Anorm: Uses executeInsert with singleOpt parser
  - Doobie: Uses query with option combinator
  - ZIO-JDBC: Uses query with selectOne
- Updated all tests to verify the returned Option contains expected values
- Updated mock implementations to return the row instead of boolean

This provides better feedback about what was actually updated and enables
verification that the update was applied correctly.
@oyvindberg oyvindberg force-pushed the update-returning branch 2 times, most recently from 7c04c41 to cd64548 Compare July 28, 2025 00:33
@oyvindberg oyvindberg changed the title anorm: update returns maybe row Make update method return Option[Row] instead of Boolean for all database libraries Jul 28, 2025
@oyvindberg oyvindberg merged commit 5c9e8d5 into main Jul 28, 2025
8 checks passed
@oyvindberg oyvindberg deleted the update-returning branch July 28, 2025 01:17
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