diff --git a/docs/plugins.rst b/docs/plugins.rst index 83b6688..d8facdc 100644 --- a/docs/plugins.rst +++ b/docs/plugins.rst @@ -20,6 +20,12 @@ You can see a JSON list of plugins that have been installed by running this: sqlite-utils plugins +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: + +.. code-block:: python + + db = Database(memory=True, execute_plugins=False) + .. _plugins_building: Building a plugin diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index 945458f..78c0667 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -311,6 +311,7 @@ class Database: 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) @@ -342,8 +343,8 @@ class Database: self.execute("PRAGMA recursive_triggers=on;") self._registered_functions: set = set() self.use_counts_table = use_counts_table - - pm.hook.prepare_connection(conn=self.conn) + if execute_plugins: + pm.hook.prepare_connection(conn=self.conn) def close(self): "Close the SQLite connection, and the underlying database file" diff --git a/tests/test_plugins.py b/tests/test_plugins.py index 10c4bb5..e82ed31 100644 --- a/tests/test_plugins.py +++ b/tests/test_plugins.py @@ -49,10 +49,16 @@ def test_prepare_connection(): conn.create_function("hello", 1, lambda name: f"Hello, {name}!") db = Database(memory=True) - functions = db.execute( - "select distinct name from pragma_function_list order by 1" - ).fetchall() - assert "hello" not in functions + + def _functions(db): + return [ + row[0] + for row in db.execute( + "select distinct name from pragma_function_list order by 1" + ).fetchall() + ] + + assert "hello" not in _functions(db) try: plugins.pm.register(HelloFunctionPlugin(), name="HelloFunctionPlugin") @@ -62,18 +68,14 @@ def test_prepare_connection(): ] db = Database(memory=True) - - functions = [ - row[0] - for row in db.execute( - "select distinct name from pragma_function_list order by 1" - ).fetchall() - ] - assert "hello" in functions - + assert "hello" in _functions(db) result = db.execute('select hello("world")').fetchone()[0] assert result == "Hello, world!" + # Test execute_plugins=False + db2 = Database(memory=True, execute_plugins=False) + assert "hello" not in _functions(db2) + finally: plugins.pm.unregister(name="HelloFunctionPlugin") assert plugins.get_plugins() == []