Skip to content

Commit 374a816

Browse files
committed
Database(..., execute_plugins=False) mechanism, refs #575
1 parent 3f80a02 commit 374a816

3 files changed

Lines changed: 24 additions & 15 deletions

File tree

docs/plugins.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ You can see a JSON list of plugins that have been installed by running this:
2020
2121
sqlite-utils plugins
2222
23+
Plugin hooks such as :ref:`plugins_hooks_prepare_connection` affect each instance of the ``Database`` class. You can opt-out of these plugins by creating that class instance like so:
24+
25+
.. code-block:: python
26+
27+
db = Database(memory=True, execute_plugins=False)
28+
2329
.. _plugins_building:
2430

2531
Building a plugin

sqlite_utils/db.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ def __init__(
311311
recursive_triggers: bool = True,
312312
tracer: Optional[Callable] = None,
313313
use_counts_table: bool = False,
314+
execute_plugins: bool = True,
314315
):
315316
assert (filename_or_conn is not None and (not memory and not memory_name)) or (
316317
filename_or_conn is None and (memory or memory_name)
@@ -342,8 +343,8 @@ def __init__(
342343
self.execute("PRAGMA recursive_triggers=on;")
343344
self._registered_functions: set = set()
344345
self.use_counts_table = use_counts_table
345-
346-
pm.hook.prepare_connection(conn=self.conn)
346+
if execute_plugins:
347+
pm.hook.prepare_connection(conn=self.conn)
347348

348349
def close(self):
349350
"Close the SQLite connection, and the underlying database file"

tests/test_plugins.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,16 @@ def prepare_connection(self, conn):
4949
conn.create_function("hello", 1, lambda name: f"Hello, {name}!")
5050

5151
db = Database(memory=True)
52-
functions = db.execute(
53-
"select distinct name from pragma_function_list order by 1"
54-
).fetchall()
55-
assert "hello" not in functions
52+
53+
def _functions(db):
54+
return [
55+
row[0]
56+
for row in db.execute(
57+
"select distinct name from pragma_function_list order by 1"
58+
).fetchall()
59+
]
60+
61+
assert "hello" not in _functions(db)
5662

5763
try:
5864
plugins.pm.register(HelloFunctionPlugin(), name="HelloFunctionPlugin")
@@ -62,18 +68,14 @@ def prepare_connection(self, conn):
6268
]
6369

6470
db = Database(memory=True)
65-
66-
functions = [
67-
row[0]
68-
for row in db.execute(
69-
"select distinct name from pragma_function_list order by 1"
70-
).fetchall()
71-
]
72-
assert "hello" in functions
73-
71+
assert "hello" in _functions(db)
7472
result = db.execute('select hello("world")').fetchone()[0]
7573
assert result == "Hello, world!"
7674

75+
# Test execute_plugins=False
76+
db2 = Database(memory=True, execute_plugins=False)
77+
assert "hello" not in _functions(db2)
78+
7779
finally:
7880
plugins.pm.unregister(name="HelloFunctionPlugin")
7981
assert plugins.get_plugins() == []

0 commit comments

Comments
 (0)