fixed index_url page_id mismatch
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.
This commit is contained in:
parent
449174b0ca
commit
6d649616ca
1 changed files with 14 additions and 12 deletions
6
db.py
6
db.py
|
|
@ -249,12 +249,13 @@ 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:
|
||||||
|
db.execute(
|
||||||
"INSERT INTO pages (url, title, body, note) VALUES (?, ?, ?, ?) "
|
"INSERT INTO pages (url, title, body, note) VALUES (?, ?, ?, ?) "
|
||||||
"ON CONFLICT(url) DO UPDATE SET title=excluded.title, body=excluded.body, note=excluded.note",
|
"ON CONFLICT(url) DO UPDATE SET title=excluded.title, body=excluded.body, note=excluded.note",
|
||||||
(url, title, body, note),
|
(url, title, body, note),
|
||||||
)
|
)
|
||||||
page_id = cur.lastrowid
|
page_id = db.execute("SELECT id FROM pages WHERE url = ?", (url,)).fetchone()[0]
|
||||||
db.execute("DELETE FROM links WHERE page_id = ?", (page_id,))
|
db.execute("DELETE FROM links WHERE page_id = ?", (page_id,))
|
||||||
for href, label in links:
|
for href, label in links:
|
||||||
db.execute(
|
db.execute(
|
||||||
|
|
@ -262,5 +263,6 @@ def index_url(url, note=""):
|
||||||
(page_id, href, label),
|
(page_id, href, label),
|
||||||
)
|
)
|
||||||
db.commit()
|
db.commit()
|
||||||
|
finally:
|
||||||
db.close()
|
db.close()
|
||||||
return title
|
return title
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue