-
-
Notifications
You must be signed in to change notification settings - Fork 700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Database class mechanism for cross-connection in-memory databases #1151
Comments
https://sqlite.org/inmemorydb.html
|
I'm going to try with |
This works in
|
This worked as a prototype: diff --git a/datasette/database.py b/datasette/database.py
index 412e0c5..a90e617 100644
--- a/datasette/database.py
+++ b/datasette/database.py
@@ -24,11 +24,12 @@ connections = threading.local()
class Database:
- def __init__(self, ds, path=None, is_mutable=False, is_memory=False):
+ def __init__(self, ds, path=None, is_mutable=False, is_memory=False, uri=None):
self.ds = ds
self.path = path
self.is_mutable = is_mutable
self.is_memory = is_memory
+ self.uri = uri
self.hash = None
self.cached_size = None
self.cached_table_counts = None
@@ -46,6 +47,8 @@ class Database:
}
def connect(self, write=False):
+ if self.uri:
+ return sqlite3.connect(self.uri, uri=True, check_same_thread=False)
if self.is_memory:
return sqlite3.connect(":memory:")
# mode=ro or immutable=1? Then in
|
I'm going to add an argument to the db = Database(ds, memory_name="datasette") |
Do I use the current https://ripgrep.datasette.io/-/ripgrep?pattern=is_memory - doesn't look like it. I may remove that feature, since it's not actually useful, and replace it with a mechanism for creating shared named memory databases instead. |
Wait I do use it - if you run |
Is it possible to connect to a memory database in read-only mode?
https://stackoverflow.com/a/40548682 suggests using |
I tested this with a one-off plugin and it worked! from datasette import hookimpl
from datasette.database import Database
@hookimpl
def startup(datasette):
datasette.add_database("statistics", Database(
datasette,
memory_name="statistics"
)) This created a |
This feature is illustrated by the tests: datasette/tests/test_internals_database.py Lines 469 to 496 in 5e9895c
I added new documentation for the |
Mentioned in https://simonwillison.net/2021/Jan/25/datasette/ |
Originally posted by @simonw in #1150 (comment)
The text was updated successfully, but these errors were encountered: