Skip to content

Commit

Permalink
feat: close the adapter's connection on quit (#518)
Browse files Browse the repository at this point in the history
This change introduces a new optional method of HarlequinConnection
implementations in adapters, giving them an opportunity to cleanly close
the connection to the underlying database when the app exists.

The `close()` method is called in the `action_quit()` handler.
  • Loading branch information
mpetazzoni authored Apr 16, 2024
1 parent 50c53a8 commit 4bc3172
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

## [Unreleased]

### Features

- A new `HarlequinConnection.close()` method can be implemented by adapters to gracefully close database connections when the application exits.

### Bug Fixes

- Wrong link on text_area_clipboard_error error message ([#509](https://github.com/tconbeer/harlequin/issues/509))
Expand Down
9 changes: 9 additions & 0 deletions src/harlequin/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,15 @@ def validate_sql(self, text: str) -> str:
"""
raise NotImplementedError

def close(self) -> None:
"""
Closes the connection, if necessary. This function is called when the app
quits.
Returns: None
"""
return None


class HarlequinAdapter(ABC):
"""
Expand Down
2 changes: 2 additions & 0 deletions src/harlequin/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,8 @@ async def action_quit(self) -> None:
s3_tree=self.data_catalog.s3_tree,
history=self.history,
)
if self.connection:
self.connection.close()
await super().action_quit()

def action_show_help_screen(self) -> None:
Expand Down

0 comments on commit 4bc3172

Please sign in to comment.