diff --git a/docs/python-api.rst b/docs/python-api.rst index a8fb705..04703de 100644 --- a/docs/python-api.rst +++ b/docs/python-api.rst @@ -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``. :: diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index 2fedc4f..719d03c 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -272,6 +272,11 @@ class Database: ).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] diff --git a/tests/test_introspect.py b/tests/test_introspect.py index ce68155..9a37001 100644 --- a/tests/test_introspect.py +++ b/tests/test_introspect.py @@ -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 ) @@ -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(