Skip to content

Close connections earlier, add fallback to close upon function exit, increase defensiveness. - #172

Merged
PietrH merged 5 commits into
live-testfrom
158-close-connection
Jul 16, 2026
Merged

Close connections earlier, add fallback to close upon function exit, increase defensiveness. #172
PietrH merged 5 commits into
live-testfrom
158-close-connection

Conversation

@PietrH

@PietrH PietrH commented Jul 16, 2026

Copy link
Copy Markdown
Member

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::defer to 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 as get_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 all list_* 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::dbDisconnect calls 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 centralized check_credentials function for consistency and maintainability in all relevant list_* functions. [1] [2] [3] [4] [5] [6] [7]

  • Added the withr package to the Imports section of DESCRIPTION to support the new resource management approach.

@PietrH PietrH self-assigned this Jul 16, 2026
@PietrH PietrH linked an issue Jul 16, 2026 that may be closed by this pull request
@PietrH PietrH changed the title 158 close connection Close connections earlier, add fallback to close upon function exit, increase defensiveness. Jul 16, 2026
@PietrH
PietrH requested a review from Copilot July 16, 2026 13:08

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

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 withr to package Imports to 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.

Comment thread R/validate_login.R
@PietrH
PietrH merged commit 93941a7 into live-test Jul 16, 2026
1 check passed
@PietrH PietrH mentioned this pull request Jul 23, 2026
19 tasks
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.

Close connection right after receiving query result

2 participants