Skip to content

Commit 2258b43

Browse files
committed
Neater return_db pattern for reusing memory command, closes #643
1 parent 8906f57 commit 2258b43

File tree

3 files changed

+18
-36
lines changed

3 files changed

+18
-36
lines changed

docs/plugins.rst

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,10 @@ Example implementation:
117117
118118
New commands implemented by plugins can invoke existing commands using the `context.invoke <https://click.palletsprojects.com/en/stable/api/#click.Context.invoke>`__ mechanism.
119119

120-
As a special niche feature, if your plugin needs to import some files and then act against an in-memory database containing those files you can forward to the :ref:`sqlite-utils memory command <cli_memory>` and then access a named in-memory database called ``sqlite_utils_memory`` like this:
120+
As a special niche feature, if your plugin needs to import some files and then act against an in-memory database containing those files you can forward to the :ref:`sqlite-utils memory command <cli_memory>` and pass it ``return_db=True``:
121121

122122
.. code-block:: python
123123
124-
from contextlib import redirect_stdout
125-
import io
126-
127124
@cli.command()
128125
@click.pass_context
129126
@click.argument(
@@ -134,9 +131,7 @@ As a special niche feature, if your plugin needs to import some files and then a
134131
)
135132
def show_schema_for_files(ctx, paths):
136133
from sqlite_utils.cli import memory
137-
with redirect_stdout(io.StringIO()):
138-
ctx.invoke(memory, paths=paths, sql="select 1")
139-
db = sqlite_utils.Database(memory_name="sqlite_utils_memory")
134+
db = ctx.invoke(memory, paths=paths, _return_db=True)
140135
# Now do something with that database
141136
click.echo(db.schema)
142137

sqlite_utils/cli.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,10 +1815,6 @@ def query(
18151815
)
18161816

18171817

1818-
# So named memory db "sqlite_utils_memory" isn't dropped when it goes out of scope
1819-
_sqlite_utils_memory_db = []
1820-
1821-
18221818
@cli.command()
18231819
@click.argument(
18241820
"paths",
@@ -1898,6 +1894,7 @@ def memory(
18981894
save,
18991895
analyze,
19001896
load_extension,
1897+
return_db=False,
19011898
):
19021899
"""Execute SQL query against an in-memory database, optionally populated by imported data
19031900
@@ -1925,13 +1922,7 @@ def memory(
19251922
\b
19261923
sqlite-utils memory animals.csv --schema
19271924
"""
1928-
if getattr(sys, "_sqlite_utils_memory_test", False) or not getattr(
1929-
sys, "_called_from_test", False
1930-
):
1931-
db = sqlite_utils.Database(memory_name="sqlite_utils_memory")
1932-
_sqlite_utils_memory_db.append(db)
1933-
else:
1934-
db = sqlite_utils.Database(memory=True)
1925+
db = sqlite_utils.Database(memory=True)
19351926

19361927
# If --dump or --save or --analyze used but no paths detected, assume SQL query is a path:
19371928
if (dump or save or schema or analyze) and not paths:
@@ -2007,6 +1998,9 @@ def memory(
20071998
if functions:
20081999
_register_functions(db, functions)
20092000

2001+
if return_db:
2002+
return db
2003+
20102004
_execute_query(
20112005
db,
20122006
sql,

tests/test_cli_memory.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import click
12
import json
23
import pytest
3-
import sys
44
from click.testing import CliRunner
55

66
from sqlite_utils import Database, cli
@@ -307,21 +307,14 @@ def test_memory_functions():
307307
assert result.output.strip() == '[{"hello()": "Hello"}]'
308308

309309

310-
@pytest.mark.parametrize("enabled", (False, True))
311-
def test_memory_named_database_hack(enabled):
310+
def test_memory_return_db(tmpdir):
312311
# https://github.com/simonw/sqlite-utils/issues/643
313-
sys._sqlite_utils_memory_test = enabled
314-
try:
315-
result = CliRunner().invoke(
316-
cli.cli,
317-
["memory", "-", "--analyze"],
318-
input="id,name\n1,Cleo\n2,Bants",
319-
)
320-
assert result.exit_code == 0
321-
db = Database(memory_name="sqlite_utils_memory")
322-
if enabled:
323-
assert db.table_names() == ["stdin"]
324-
else:
325-
assert db.table_names() == []
326-
finally:
327-
sys._sqlite_utils_memory_test = False
312+
from sqlite_utils.cli import cli
313+
314+
path = str(tmpdir / "dogs.csv")
315+
open(path, "w").write("id,name\n1,Cleo")
316+
317+
with click.Context(cli) as ctx:
318+
db = ctx.invoke(cli.commands["memory"], paths=(path,), return_db=True)
319+
320+
assert db.table_names() == ["dogs"]

0 commit comments

Comments
 (0)