disable-fts and .disable_fts(), closes #88

This commit is contained in:
Simon Willison 2020-02-26 20:40:35 -08:00
commit f9473ace14
6 changed files with 96 additions and 0 deletions

View file

@ -300,6 +300,19 @@ def populate_fts(path, table, column):
db[table].populate_fts(column)
@cli.command(name="disable-fts")
@click.argument(
"path",
type=click.Path(exists=True, file_okay=True, dir_okay=False, allow_dash=False),
required=True,
)
@click.argument("table")
def disable_fts(path, table):
"Disable FTS for specific table"
db = sqlite_utils.Database(path)
db[table].disable_fts()
def insert_upsert_options(fn):
for decorator in reversed(
(

View file

@ -794,6 +794,25 @@ class Table(Queryable):
self.db.conn.executescript(sql)
return self
def disable_fts(self):
fts_table = self.detect_fts()
if fts_table:
self.db[fts_table].drop()
# Now delete the triggers that related to that table
sql = """
SELECT name FROM sqlite_master
WHERE type = 'trigger'
AND sql LIKE '% INSERT INTO [{}]%'
""".format(
fts_table
)
trigger_names = []
for row in self.db.conn.execute(sql).fetchall():
trigger_names.append(row[0])
with self.db.conn:
for trigger_name in trigger_names:
self.db.conn.execute("DROP TRIGGER IF EXISTS [{}]".format(trigger_name))
def detect_fts(self):
"Detect if table has a corresponding FTS virtual table and return it"
sql = """