Skip to content

Commit

Permalink
recursive_triggers=on by default, closes #152
Browse files Browse the repository at this point in the history
Refs #149
  • Loading branch information
simonw committed Sep 7, 2020
1 parent e878f2a commit de10590
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
6 changes: 6 additions & 0 deletions docs/python-api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ If you want to create an in-memory database, you can do so like this:
db = Database(memory=True)
Connections use ``PRAGMA recursive_triggers=on`` by default. If you don't want to use `recursive triggers <https://www.sqlite.org/pragma.html#pragma_recursive_triggers>`__ you can turn them off using:

.. code-block:: python
db = Database(memory=True, recursive_triggers=False)
Tables are accessed using the indexing operator, like so:

.. code-block:: python
Expand Down
10 changes: 9 additions & 1 deletion sqlite_utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,13 @@ class PrimaryKeyRequired(Exception):


class Database:
def __init__(self, filename_or_conn=None, memory=False, recreate=False):
def __init__(
self,
filename_or_conn=None,
memory=False,
recreate=False,
recursive_triggers=True,
):
assert (filename_or_conn is not None and not memory) or (
filename_or_conn is None and memory
), "Either specify a filename_or_conn or pass memory=True"
Expand All @@ -116,6 +122,8 @@ def __init__(self, filename_or_conn=None, memory=False, recreate=False):
else:
assert not recreate, "recreate cannot be used with connections, only paths"
self.conn = filename_or_conn
if recursive_triggers:
self.conn.execute("PRAGMA recursive_triggers=on;")

def __getitem__(self, table_name):
return self.table(table_name)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_constructor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from sqlite_utils import Database
import pytest


def test_recursive_triggers():
db = Database(memory=True)
assert db.conn.execute("PRAGMA recursive_triggers").fetchone()[0]


def test_recursive_triggers_off():
db = Database(memory=True, recursive_triggers=False)
assert not db.conn.execute("PRAGMA recursive_triggers").fetchone()[0]

0 comments on commit de10590

Please sign in to comment.