Skip to content
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

Ability to tell if a Database is an in-memory one #590

Open
simonw opened this issue Sep 3, 2023 · 1 comment
Open

Ability to tell if a Database is an in-memory one #590

simonw opened this issue Sep 3, 2023 · 1 comment
Labels
enhancement New feature or request python-library

Comments

@simonw
Copy link
Owner

simonw commented Sep 3, 2023

Currently the constructor accepts memory=True or memory_name=... and uses those to create a connection, but does not record what those values were:

def __init__(
self,
filename_or_conn: Optional[Union[str, pathlib.Path, sqlite3.Connection]] = None,
memory: bool = False,
memory_name: Optional[str] = None,
recreate: bool = False,
recursive_triggers: bool = True,
tracer: Optional[Callable] = None,
use_counts_table: bool = False,
execute_plugins: bool = True,
):
assert (filename_or_conn is not None and (not memory and not memory_name)) or (
filename_or_conn is None and (memory or memory_name)
), "Either specify a filename_or_conn or pass memory=True"
if memory_name:
uri = "file:{}?mode=memory&cache=shared".format(memory_name)
self.conn = sqlite3.connect(
uri,
uri=True,
check_same_thread=False,
)
elif memory or filename_or_conn == ":memory:":
self.conn = sqlite3.connect(":memory:")
elif isinstance(filename_or_conn, (str, pathlib.Path)):
if recreate and os.path.exists(filename_or_conn):
try:
os.remove(filename_or_conn)
except OSError:
# Avoid mypy and __repr__ errors, see:
# https://github.com/simonw/sqlite-utils/issues/503
self.conn = sqlite3.connect(":memory:")
raise
self.conn = sqlite3.connect(str(filename_or_conn))
else:
assert not recreate, "recreate cannot be used with connections, only paths"
self.conn = filename_or_conn
self._tracer = tracer
if recursive_triggers:
self.execute("PRAGMA recursive_triggers=on;")
self._registered_functions: set = set()
self.use_counts_table = use_counts_table
if execute_plugins:
pm.hook.prepare_connection(conn=self.conn)

This makes it hard to tell if a database object is to an in-memory or a file-based database, which is sometimes useful to know.

@simonw simonw added enhancement New feature or request python-library labels Sep 3, 2023
@simonw
Copy link
Owner Author

simonw commented Sep 3, 2023

Maybe just populate db.memory: bool and db.memory_name: Optional[str] for this, then document them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request python-library
Projects
None yet
Development

No branches or pull requests

1 participant