Close connections earlier, add fallback to close upon function exit, increase defensiveness. - #172
Merged
Conversation
…alid connection, and add fallback for closing connection when function crashes.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR standardizes database-connection lifecycle handling across ETN query helpers by adding a deferred disconnect “failsafe” (to avoid leaking connections on early exits/errors), moving some disconnects earlier where possible, and centralizing credential-shape validation via check_credentials().
Changes:
- Add
withr::defer()-based disconnect guards in functions that open DB connections, and in several cases disconnect immediately after fetching results. - Replace scattered inline credential assertions with
check_credentials(credentials)calls for consistent validation/error messages. - Add
withrto packageImportsto support the new resource-management approach.
Reviewed changes
Copilot reviewed 23 out of 23 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| R/validate_login.R | Switches login validation to a withr-managed local DB connection (noted compatibility risk if using local_db_connection() without pinning withr version). |
| R/list_tag_serial_numbers.R | Adds check_credentials() and deferred disconnect guard around connection usage. |
| R/list_station_names.R | Adds check_credentials() and deferred disconnect guard around connection usage. |
| R/list_scientific_names.R | Adds check_credentials() and deferred disconnect guard around connection usage. |
| R/list_receiver_ids.R | Adds check_credentials() and deferred disconnect guard around connection usage. |
| R/list_deployment_ids.R | Adds check_credentials() and deferred disconnect guard around connection usage. |
| R/list_cpod_project_codes.R | Adds check_credentials() and deferred disconnect guard around connection usage. |
| R/list_animal_project_codes.R | Adds check_credentials() and deferred disconnect guard around connection usage. |
| R/list_animal_ids.R | Replaces inline credential checks with check_credentials(), adds deferred disconnect guard, and ensures connection validation is invoked. |
| R/list_acoustic_tag_ids.R | Adds check_credentials() and deferred disconnect guard around connection usage. |
| R/list_acoustic_project_codes.R | Adds check_credentials() and deferred disconnect guard around connection usage. |
| R/get_tags.R | Adds deferred disconnect guard and closes connection immediately after query execution. |
| R/get_cpod_projects.R | Adds deferred disconnect guard and closes connection immediately after query execution. |
| R/get_archival_data_uuid.R | Adds deferred disconnect guard and closes connection immediately after query execution. |
| R/get_animals.R | Adds deferred disconnect guard and closes connection immediately after query execution. |
| R/get_animal_projects.R | Adds deferred disconnect guard and closes connection immediately after query execution. |
| R/get_acoustic_receivers.R | Adds deferred disconnect guard and closes connection immediately after query execution. |
| R/get_acoustic_projects.R | Adds deferred disconnect guard and closes connection immediately after query execution. |
| R/get_acoustic_detections.R | Adds deferred disconnect guard and closes connection immediately after query execution. |
| R/get_acoustic_detections_page.R | Adds deferred disconnect guard and closes connection immediately after query execution. |
| R/get_acoustic_deployments.R | Adds deferred disconnect guard and closes connection immediately after query execution. |
| R/get_acoustic_deployment_logs.R | Adds deferred disconnect guard and closes connection immediately after query execution. |
| DESCRIPTION | Adds withr to Imports. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I've made small changes in a few instances where a connection was left open longer than absolutly neccesairy. This in an effort to keep as many database connections available as possible at any moment.
Furthermore I've added a bit of boilerplate that will act as a failsafe and close the connection (if not closed already) when a function exits, even when it crashes.
Finally I noticed that a number of early exit helpers weren't called for a bunch of functions, so I've added those in to improve error messaging.
Diffhunks
Added
withr::deferto all functions that open a database connection, ensuring the connection is properly closed when the function exits, even if an error occurs. This change affects files such asget_acoustic_deployment_logs.R,get_acoustic_deployments.R,get_acoustic_detections.R,get_acoustic_detections_page.R,get_acoustic_projects.R,get_acoustic_receivers.R,get_animal_projects.R,get_animals.R,get_archival_data_uuid.R,get_cpod_projects.R,get_tags.R, and alllist_*functions. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19]Removed redundant or misplaced manual
DBI::dbDisconnectcalls after data processing, as connection cleanup is now handled automatically. [1] [2] [3] [4] [5]Replaced inline credential checks (e.g.,
stopifnot(...)) with calls to a centralizedcheck_credentialsfunction for consistency and maintainability in all relevantlist_*functions. [1] [2] [3] [4] [5] [6] [7]Added the
withrpackage to theImportssection ofDESCRIPTIONto support the new resource management approach.