Skip to content

Commit

Permalink
database.triggers_dict, closes #216
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Jan 3, 2021
1 parent 3d041d3 commit de08096
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
9 changes: 9 additions & 0 deletions docs/python-api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1476,6 +1476,15 @@ The ``table.triggers_dict`` property returns the triggers for that table as a di
'authors_ad': 'CREATE TRIGGER [authors_ad] AFTER DELETE...',
'authors_au': 'CREATE TRIGGER [authors_au] AFTER UPDATE'}
The same property exists on the database, and will return all triggers across all tables:
::
>>> db.triggers_dict
{'authors_ai': 'CREATE TRIGGER [authors_ai] AFTER INSERT...',
'authors_ad': 'CREATE TRIGGER [authors_ad] AFTER DELETE...',
'authors_au': 'CREATE TRIGGER [authors_au] AFTER UPDATE'}
The ``detect_fts()`` method returns the associated SQLite FTS table name, if one exists for this table. If the table has not been configured for full-text search it returns ``None``.
::
Expand Down
5 changes: 5 additions & 0 deletions sqlite_utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ def triggers(self):
).fetchall()
]

@property
def triggers_dict(self):
"Returns {trigger_name: sql} dictionary"
return {trigger.name: trigger.sql for trigger in self.triggers}

@property
def journal_mode(self):
return self.execute("PRAGMA journal_mode;").fetchone()[0]
Expand Down
15 changes: 9 additions & 6 deletions tests/test_introspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,10 @@ def test_triggers_and_triggers_dict(fresh_db):
]
)
fresh_db["other"].insert({"foo": "bar"})
assert [] == authors.triggers
assert {} == authors.triggers_dict
assert [] == fresh_db["other"].triggers
assert authors.triggers == []
assert authors.triggers_dict == {}
assert fresh_db["other"].triggers == []
assert fresh_db.triggers_dict == {}
authors.enable_fts(
["name", "famous_works"], fts_version="FTS4", create_triggers=True
)
Expand All @@ -142,13 +143,15 @@ def test_triggers_and_triggers_dict(fresh_db):
assert expected_triggers == {
(t.name, t.table) for t in fresh_db["authors"].triggers
}
assert authors.triggers_dict == {
expected_triggers = {
"authors_ai": "CREATE TRIGGER [authors_ai] AFTER INSERT ON [authors] BEGIN\n INSERT INTO [authors_fts] (rowid, [name], [famous_works]) VALUES (new.rowid, new.[name], new.[famous_works]);\nEND",
"authors_ad": "CREATE TRIGGER [authors_ad] AFTER DELETE ON [authors] BEGIN\n INSERT INTO [authors_fts] ([authors_fts], rowid, [name], [famous_works]) VALUES('delete', old.rowid, old.[name], old.[famous_works]);\nEND",
"authors_au": "CREATE TRIGGER [authors_au] AFTER UPDATE ON [authors] BEGIN\n INSERT INTO [authors_fts] ([authors_fts], rowid, [name], [famous_works]) VALUES('delete', old.rowid, old.[name], old.[famous_works]);\n INSERT INTO [authors_fts] (rowid, [name], [famous_works]) VALUES (new.rowid, new.[name], new.[famous_works]);\nEND",
}
assert [] == fresh_db["other"].triggers
assert {} == fresh_db["other"].triggers_dict
assert authors.triggers_dict == expected_triggers
assert fresh_db["other"].triggers == []
assert fresh_db["other"].triggers_dict == {}
assert fresh_db.triggers_dict == expected_triggers


@pytest.mark.parametrize(
Expand Down

0 comments on commit de08096

Please sign in to comment.