Skip to content

Commit 1d2170e

Browse files
digithreeclaude
andcommitted
Fix OperationalError when items table doesn't exist in ensure_fts
- Add check for items table existence before enabling FTS - Add comprehensive tests for ensure_fts function edge cases: * When no items table exists (should not crash) * When items table exists (should create FTS) * When FTS already exists (should skip creation) Fixes the stack trace: sqlite3.OperationalError: no such table: items at utils.py line 68 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 299103a commit 1d2170e

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

pocket_to_sqlite/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def transform(item):
6464

6565

6666
def ensure_fts(db):
67-
if "items_fts" not in db.table_names():
67+
if "items_fts" not in db.table_names() and "items" in db.table_names():
6868
db["items"].enable_fts(["resolved_title", "excerpt"], create_triggers=True)
6969

7070

tests/test_save_pocket.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,49 @@ def test_fetch_items_handles_missing_since_key():
120120

121121
# Should handle missing 'since' key gracefully
122122
assert items == []
123+
124+
125+
def test_ensure_fts_with_no_items_table():
126+
"""Test that ensure_fts handles case when items table doesn't exist."""
127+
db = sqlite_utils.Database(":memory:")
128+
129+
# Call ensure_fts on empty database (no items table)
130+
utils.ensure_fts(db)
131+
132+
# Should not crash and should not create FTS table
133+
assert "items_fts" not in db.table_names()
134+
assert "items" not in db.table_names()
135+
136+
137+
def test_ensure_fts_with_items_table_creates_fts():
138+
"""Test that ensure_fts creates FTS when items table exists."""
139+
db = sqlite_utils.Database(":memory:")
140+
141+
# Create items table first
142+
db["items"].insert({"item_id": 1, "resolved_title": "Test", "excerpt": "Test excerpt"})
143+
144+
# Call ensure_fts
145+
utils.ensure_fts(db)
146+
147+
# Should create FTS table
148+
assert "items_fts" in db.table_names()
149+
assert "items" in db.table_names()
150+
151+
152+
def test_ensure_fts_skips_when_fts_already_exists():
153+
"""Test that ensure_fts skips creation when FTS already exists."""
154+
db = sqlite_utils.Database(":memory:")
155+
156+
# Create items table and FTS
157+
db["items"].insert({"item_id": 1, "resolved_title": "Test", "excerpt": "Test excerpt"})
158+
db["items"].enable_fts(["resolved_title", "excerpt"], create_triggers=True)
159+
160+
# Track table count before
161+
table_count_before = len(db.table_names())
162+
163+
# Call ensure_fts
164+
utils.ensure_fts(db)
165+
166+
# Should not create additional tables
167+
assert len(db.table_names()) == table_count_before
168+
assert "items_fts" in db.table_names()

0 commit comments

Comments
 (0)