mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-09-09 18:14:09 +02:00
.optimize() no longer cleans up _docsize
This isn't necessary any more since the new .rebuild_fts() method can achieve the same thing. Refs #155, #153
This commit is contained in:
parent
4c0f79398f
commit
64799df78b
4 changed files with 22 additions and 38 deletions
|
|
@ -1228,6 +1228,10 @@ This method can be called on a table that has been configured for full-text sear
|
||||||
|
|
||||||
db["dogs_fts"].rebuild_fts()
|
db["dogs_fts"].rebuild_fts()
|
||||||
|
|
||||||
|
This runs the following SQL::
|
||||||
|
|
||||||
|
INSERT INTO dogs_fts (dogs_fts) VALUES ("rebuild");
|
||||||
|
|
||||||
Optimizing a full-text search table
|
Optimizing a full-text search table
|
||||||
===================================
|
===================================
|
||||||
|
|
||||||
|
|
@ -1240,10 +1244,6 @@ Once you have populated a FTS table you can optimize it to dramatically reduce i
|
||||||
This runs the following SQL::
|
This runs the following SQL::
|
||||||
|
|
||||||
INSERT INTO dogs_fts (dogs_fts) VALUES ("optimize");
|
INSERT INTO dogs_fts (dogs_fts) VALUES ("optimize");
|
||||||
DELETE FROM [dogs_fts_docsize] WHERE id NOT IN (
|
|
||||||
SELECT rowid FROM [dogs_fts]);
|
|
||||||
|
|
||||||
That ``DELETE`` statement cleans up rows that may have been created by `an obscure bug <https://github.com/simonw/sqlite-utils/issues/153>`__ in previous versions of ``sqlite-utils``.
|
|
||||||
|
|
||||||
Creating indexes
|
Creating indexes
|
||||||
================
|
================
|
||||||
|
|
|
||||||
|
|
@ -951,20 +951,6 @@ class Table(Queryable):
|
||||||
table=fts_table
|
table=fts_table
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
self.db.execute(
|
|
||||||
textwrap.dedent(
|
|
||||||
"""
|
|
||||||
DELETE FROM [{table}_docsize] WHERE {column} NOT IN (
|
|
||||||
SELECT rowid FROM [{table}]);
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
.strip()
|
|
||||||
.format(
|
|
||||||
# FTS5 uses 'id' but FTS4 uses 'docid'
|
|
||||||
column=self.db["{}_docsize".format(fts_table)].columns[0].name,
|
|
||||||
table=fts_table,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def search(self, q):
|
def search(self, q):
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import pytest
|
import pytest
|
||||||
|
from sqlite_utils import Database
|
||||||
from sqlite_utils.utils import sqlite3
|
from sqlite_utils.utils import sqlite3
|
||||||
|
|
||||||
search_records = [
|
search_records = [
|
||||||
|
|
@ -199,3 +200,20 @@ def test_rebuild_fts_invalid(fresh_db, invalid_table):
|
||||||
# Raise OperationalError on invalid table
|
# Raise OperationalError on invalid table
|
||||||
with pytest.raises(sqlite3.OperationalError):
|
with pytest.raises(sqlite3.OperationalError):
|
||||||
fresh_db[invalid_table].rebuild_fts()
|
fresh_db[invalid_table].rebuild_fts()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("fts_version", ["FTS4", "FTS5"])
|
||||||
|
def test_rebuild_removes_junk_docsize_rows(tmpdir, fts_version):
|
||||||
|
# Recreating https://github.com/simonw/sqlite-utils/issues/149
|
||||||
|
path = tmpdir / "test.db"
|
||||||
|
db = Database(str(path), recursive_triggers=False)
|
||||||
|
licenses = [{"key": "apache2", "name": "Apache 2"}, {"key": "bsd", "name": "BSD"}]
|
||||||
|
db["licenses"].insert_all(licenses, pk="key", replace=True)
|
||||||
|
db["licenses"].enable_fts(["name"], create_triggers=True, fts_version=fts_version)
|
||||||
|
assert db["licenses_fts_docsize"].count == 2
|
||||||
|
# Bug: insert with replace increases the number of rows in _docsize:
|
||||||
|
db["licenses"].insert_all(licenses, pk="key", replace=True)
|
||||||
|
assert db["licenses_fts_docsize"].count == 4
|
||||||
|
# rebuild should fix this:
|
||||||
|
db["licenses_fts"].rebuild_fts()
|
||||||
|
assert db["licenses_fts_docsize"].count == 2
|
||||||
|
|
|
||||||
|
|
@ -1,20 +0,0 @@
|
||||||
import pytest
|
|
||||||
from sqlite_utils import Database
|
|
||||||
import sqlite3
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("fts_version", ["FTS4", "FTS5"])
|
|
||||||
def test_optimizes_removes_junk_docsize_rows(tmpdir, fts_version):
|
|
||||||
# Recreating https://github.com/simonw/sqlite-utils/issues/149
|
|
||||||
path = tmpdir / "test.db"
|
|
||||||
db = Database(str(path), recursive_triggers=False)
|
|
||||||
licenses = [{"key": "apache2", "name": "Apache 2"}, {"key": "bsd", "name": "BSD"}]
|
|
||||||
db["licenses"].insert_all(licenses, pk="key", replace=True)
|
|
||||||
db["licenses"].enable_fts(["name"], create_triggers=True, fts_version=fts_version)
|
|
||||||
assert db["licenses_fts_docsize"].count == 2
|
|
||||||
# Bug: insert with replace increases the number of rows in _docsize:
|
|
||||||
db["licenses"].insert_all(licenses, pk="key", replace=True)
|
|
||||||
assert db["licenses_fts_docsize"].count == 4
|
|
||||||
# Optimize should fix this:
|
|
||||||
db["licenses_fts"].optimize()
|
|
||||||
assert db["licenses_fts_docsize"].count == 2
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue