mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-08-01 15:04:11 +02:00
sqlite-utils optimize command, .optimize() and .detect_fts() table methods
This commit is contained in:
parent
015bd2a840
commit
0a8194e730
7 changed files with 146 additions and 4 deletions
|
|
@ -1,5 +1,6 @@
|
|||
from sqlite_utils import cli, Database
|
||||
from click.testing import CliRunner
|
||||
import os
|
||||
import pytest
|
||||
import sqlite3
|
||||
|
||||
|
|
@ -38,3 +39,26 @@ def test_table_names_fts5(db_path):
|
|||
def test_vacuum(db_path):
|
||||
result = CliRunner().invoke(cli.cli, ["vacuum", db_path])
|
||||
assert 0 == result.exit_code
|
||||
|
||||
|
||||
def test_optimize(db_path):
|
||||
db = Database(db_path)
|
||||
with db.conn:
|
||||
for table in ("Gosh", "Gosh2"):
|
||||
db[table].insert_all(
|
||||
[
|
||||
{
|
||||
"c1": "verb{}".format(i),
|
||||
"c2": "noun{}".format(i),
|
||||
"c3": "adjective{}".format(i),
|
||||
}
|
||||
for i in range(10000)
|
||||
]
|
||||
)
|
||||
db["Gosh"].enable_fts(["c1", "c2", "c3"], fts_version="FTS4")
|
||||
db["Gosh2"].enable_fts(["c1", "c2", "c3"], fts_version="FTS5")
|
||||
size_before_optimize = os.stat(db_path).st_size
|
||||
result = CliRunner().invoke(cli.cli, ["optimize", db_path])
|
||||
assert 0 == result.exit_code
|
||||
size_after_optimize = os.stat(db_path).st_size
|
||||
assert size_after_optimize < size_before_optimize
|
||||
|
|
|
|||
|
|
@ -32,3 +32,19 @@ def test_populate_fts(fresh_db):
|
|||
# Now run populate_fts to make this record available
|
||||
table.populate_fts(["text", "country"])
|
||||
assert [("racoons are trash pandas", "USA", "bar")] == table.search("usa")
|
||||
|
||||
|
||||
def test_optimize_fts(fresh_db):
|
||||
for fts_version in ("4", "5"):
|
||||
table_name = "searchable_{}".format(fts_version)
|
||||
table = fresh_db[table_name]
|
||||
table.insert_all(search_records)
|
||||
table.enable_fts(["text", "country"], fts_version="FTS{}".format(fts_version))
|
||||
# You can call optimize successfully against the tables OR their _fts equivalents:
|
||||
for table_name in (
|
||||
"searchable_4",
|
||||
"searchable_5",
|
||||
"searchable_4_fts",
|
||||
"searchable_5_fts",
|
||||
):
|
||||
fresh_db[table_name].optimize()
|
||||
|
|
|
|||
|
|
@ -6,14 +6,30 @@ def test_table_names(existing_db):
|
|||
|
||||
|
||||
def test_table_names_fts4(existing_db):
|
||||
existing_db["woo"].insert({"title": "Hello"})
|
||||
existing_db["woo2"].insert({"title": "Hello"})
|
||||
existing_db["woo"].enable_fts(["title"], fts_version="FTS4")
|
||||
existing_db["woo2"].enable_fts(["title"], fts_version="FTS5")
|
||||
existing_db["woo"].insert({"title": "Hello"}).enable_fts(
|
||||
["title"], fts_version="FTS4"
|
||||
)
|
||||
existing_db["woo2"].insert({"title": "Hello"}).enable_fts(
|
||||
["title"], fts_version="FTS5"
|
||||
)
|
||||
assert ["woo_fts"] == existing_db.table_names(fts4=True)
|
||||
assert ["woo2_fts"] == existing_db.table_names(fts5=True)
|
||||
|
||||
|
||||
def test_detect_fts(existing_db):
|
||||
existing_db["woo"].insert({"title": "Hello"}).enable_fts(
|
||||
["title"], fts_version="FTS4"
|
||||
)
|
||||
existing_db["woo2"].insert({"title": "Hello"}).enable_fts(
|
||||
["title"], fts_version="FTS5"
|
||||
)
|
||||
assert "woo_fts" == existing_db["woo"].detect_fts()
|
||||
assert "woo_fts" == existing_db["woo_fts"].detect_fts()
|
||||
assert "woo2_fts" == existing_db["woo2"].detect_fts()
|
||||
assert "woo2_fts" == existing_db["woo2_fts"].detect_fts()
|
||||
assert None == existing_db["foo"].detect_fts()
|
||||
|
||||
|
||||
def test_tables(existing_db):
|
||||
assert 1 == len(existing_db.tables)
|
||||
assert "foo" == existing_db.tables[0].name
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue