Open up SQLite query param, cached database functionality, inspired by #196 #451
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.
Background
In sqlite3, if you set
uri=True
you can pass query param arguments to the underlying database, including:mode = memory, ro, rw
cache = shared
Examples:
Options like
cache=shared
andmode=memory
are particularly useful for unit testing (you can create an in-memory DB which stays alive between multiple connections).The Issue
The issue is twofold:
databases
strips out query params before passing the connection string to underling connectiondatabases
closing the db connection after each query means that there is no living reference to a connection of the cached in-memory database once the query finishes. This causes the cached in-memory database to be deleted after each query even if we could setcache=shared
This leads to the following behavior:
Proposed Solution
This allows query params to be passed to the underlying connection and, when
cache=shared
, a reference to a connection to the cached in-memory database to be held (allowing it to persist) untilDatabase.disconnect
is called, leading to the following good behavior:Also fixes: #196, #75