Fix index_url using wrong page_id after upsert

lastrowid returns 0 when ON CONFLICT DO UPDATE fires on an existing
row, causing links to not be cleaned up or associated correctly on
re-index. Now fetches the actual row ID with a SELECT after upsert.
Also adds try/finally for connection safety.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Derick Phan 2026-03-26 11:24:01 -07:00
parent c10aa7955c
commit 86e4c1f151
No known key found for this signature in database

26
db.py
View file

@ -249,18 +249,20 @@ def index_url(url, note=""):
url = clean_url(url) url = clean_url(url)
title, body, links = fetch_page(url) title, body, links = fetch_page(url)
db = get_db() db = get_db()
cur = db.execute( try:
"INSERT INTO pages (url, title, body, note) VALUES (?, ?, ?, ?) "
"ON CONFLICT(url) DO UPDATE SET title=excluded.title, body=excluded.body, note=excluded.note",
(url, title, body, note),
)
page_id = cur.lastrowid
db.execute("DELETE FROM links WHERE page_id = ?", (page_id,))
for href, label in links:
db.execute( db.execute(
"INSERT INTO links (page_id, url, label) VALUES (?, ?, ?)", "INSERT INTO pages (url, title, body, note) VALUES (?, ?, ?, ?) "
(page_id, href, label), "ON CONFLICT(url) DO UPDATE SET title=excluded.title, body=excluded.body, note=excluded.note",
(url, title, body, note),
) )
db.commit() page_id = db.execute("SELECT id FROM pages WHERE url = ?", (url,)).fetchone()[0]
db.close() db.execute("DELETE FROM links WHERE page_id = ?", (page_id,))
for href, label in links:
db.execute(
"INSERT INTO links (page_id, url, label) VALUES (?, ?, ?)",
(page_id, href, label),
)
db.commit()
finally:
db.close()
return title return title