From b067f1ff57372be7f520d536510adc808764243a Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Fri, 1 Jan 2021 18:10:04 -0800 Subject: [PATCH] table.triggers_dict property, closes #211 --- docs/python-api.rst | 9 +++++++++ sqlite_utils/db.py | 5 +++++ tests/test_introspect.py | 9 ++++++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/docs/python-api.rst b/docs/python-api.rst index ac7a830..62fa535 100644 --- a/docs/python-api.rst +++ b/docs/python-api.rst @@ -1458,6 +1458,15 @@ The ``.triggers`` property lists database triggers. It can be used on both datab >>> db.triggers ... similar output to db["authors"].triggers +The ``table.triggers_dict`` property returns the triggers for that table as a dictionary mapping their names to their SQL definitions. + +:: + + >>> db["authors"].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 90427bf..18ac664 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -759,6 +759,11 @@ class Table(Queryable): ).fetchall() ] + @property + def triggers_dict(self): + "Returns {trigger_name: sql} dictionary" + return {trigger.name: trigger.sql for trigger in self.triggers} + def create( self, columns, diff --git a/tests/test_introspect.py b/tests/test_introspect.py index 80cfee6..ce68155 100644 --- a/tests/test_introspect.py +++ b/tests/test_introspect.py @@ -117,7 +117,7 @@ def test_pks(fresh_db, pk, expected): assert expected == fresh_db["foo"].pks -def test_triggers(fresh_db): +def test_triggers_and_triggers_dict(fresh_db): assert [] == fresh_db.triggers authors = fresh_db["authors"] authors.insert_all( @@ -128,6 +128,7 @@ def test_triggers(fresh_db): ) fresh_db["other"].insert({"foo": "bar"}) assert [] == authors.triggers + assert {} == authors.triggers_dict assert [] == fresh_db["other"].triggers authors.enable_fts( ["name", "famous_works"], fts_version="FTS4", create_triggers=True @@ -141,7 +142,13 @@ def test_triggers(fresh_db): assert expected_triggers == { (t.name, t.table) for t in fresh_db["authors"].triggers } + assert authors.triggers_dict == { + "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 @pytest.mark.parametrize(