Option to add triggers when enabling FTS (#57)

--create-triggers CLI option and create_triggers=True in the Python library

* Add an option to create triggers for fts table.
* Add cli option for the create-update-trigger.
* Add tests for the create-update-trigger option.
* Change FTS table escaping to square brackets.
This commit is contained in:
Amjith Ramanujam 2019-09-02 16:42:28 -07:00 committed by Simon Willison
commit 405e092d59
4 changed files with 128 additions and 17 deletions

View file

@ -265,7 +265,13 @@ def create_index(path, table, column, name, unique, if_not_exists):
@click.argument("column", nargs=-1, required=True)
@click.option("--fts4", help="Use FTS4", default=False, is_flag=True)
@click.option("--fts5", help="Use FTS5", default=False, is_flag=True)
def enable_fts(path, table, column, fts4, fts5):
@click.option(
"--create-triggers",
help="Create triggers to update the FTS tables when the parent table changes.",
default=False,
is_flag=True,
)
def enable_fts(path, table, column, fts4, fts5, create_triggers):
"Enable FTS for specific table and columns"
fts_version = "FTS5"
if fts4 and fts5:
@ -275,7 +281,9 @@ def enable_fts(path, table, column, fts4, fts5):
fts_version = "FTS4"
db = sqlite_utils.Database(path)
db[table].enable_fts(column, fts_version=fts_version)
db[table].enable_fts(
column, fts_version=fts_version, create_triggers=create_triggers
)
@cli.command(name="populate-fts")