sqlite-utils rebuild-fts command, closes #155

This commit is contained in:
Simon Willison 2020-09-08 16:16:03 -07:00
commit 176f4e0ef4
5 changed files with 93 additions and 2 deletions

View file

@ -232,6 +232,34 @@ def optimize(path, tables, no_vacuum):
db.vacuum()
@cli.command(name="rebuild-fts")
@click.argument(
"path",
type=click.Path(exists=True, file_okay=True, dir_okay=False, allow_dash=False),
required=True,
)
@click.argument("tables", nargs=-1)
def rebuild_fts(path, tables):
"""Rebuild specific FTS tables, or all FTS tables if none are specified"""
db = sqlite_utils.Database(path)
if not tables:
tables = db.table_names(fts4=True) + db.table_names(fts5=True)
with db.conn:
for table in tables:
db[table].rebuild_fts()
@cli.command()
@click.argument(
"path",
type=click.Path(exists=True, file_okay=True, dir_okay=False, allow_dash=False),
required=True,
)
def vacuum(path):
"""Run VACUUM against the database"""
sqlite_utils.Database(path).vacuum()
@cli.command(name="add-column")
@click.argument(
"path",

View file

@ -173,9 +173,9 @@ class Database:
def table_names(self, fts4=False, fts5=False):
where = ["type = 'table'"]
if fts4:
where.append("sql like '%FTS4%'")
where.append("sql like '%USING FTS4%'")
if fts5:
where.append("sql like '%FTS5%'")
where.append("sql like '%USING FTS5%'")
sql = "select name from sqlite_master where {}".format(" AND ".join(where))
return [r[0] for r in self.execute(sql).fetchall()]