More database tests, test redirect

This commit is contained in:
John Doty 2024-07-17 09:43:25 -07:00
parent 52c12785c8
commit a08257ec76
3 changed files with 258 additions and 23 deletions

View file

@ -96,7 +96,7 @@ def subscribe(url, literal):
result = d
# Check to see if this URL is already in the database.
existing = db.load_feed(result.meta.url)
existing = db.load_feed(result.meta.url) # TODO: Replace with 'load_meta'?
if existing is not None:
click.echo(f"This feed already exists (as {result.meta.url})")
return 1
@ -129,7 +129,7 @@ def import_opml(opml_file):
click.echo(f"{url} does not seem to be a feed, skipping...")
continue
existing = db.load_feed(meta.url)
existing = db.load_feed(meta.url) # TODO: Replace with 'load_meta'?
if existing is not None:
LOG.info(f"{url} already exists (as {meta.url})")
continue
@ -151,7 +151,7 @@ def refresh(url):
db = database.Database.local()
if url:
f = db.load_feed(url)
f = db.load_feed(url) # TODO: Replace with 'load_meta'?
if f is None:
click.echo(f"Not subscribed to {url}")
return 1

View file

@ -91,7 +91,15 @@ class Database:
if not isinstance(path, str):
path.parent.mkdir(parents=True, exist_ok=True)
db = sqlite3.Connection(str(path), autocommit=False)
db.execute("PRAGMA foreign_keys = ON")
# This is WILD that I need to do this because you cannot enable or
# disable foreign keys with a transaction active, and
# `autocommit=False` means that a transaction is *always* active.
db.executescript("COMMIT;PRAGMA foreign_keys = ON;BEGIN;")
cursor = db.execute("PRAGMA foreign_keys")
rows = cursor.fetchall()
assert str(rows[0][0]) == "1", f"Foreign keys not enabled! {rows[0][0]}"
self.db = db
self.origin = origin
@ -433,30 +441,21 @@ class Database:
"UPDATE feeds SET url = ? WHERE url = ?", [new_url, old_url]
)
else:
# Preserve the entries that were under the old url.
# First update all the entries that you can with the old url.
self.db.execute(
"""
UPDATE entries
UPDATE OR IGNORE entries
SET feed_url = ?
WHERE feed_url = ?
ON CONFLICT DO UPDATE
SET
-- NOTE: This is also part of the feed merge algorithm, BUT
-- we implement it here. See the comment in store_feed
-- for the rationale.
inserted_at=MIN(inserted_at, excluded.inserted_at),
title=CASE
WHEN inserted_at < excluded.inserted_at THEN title
ELSE excluded.title
END,
link=CASE
WHEN inserted_at < excluded.inserted_at THEN link
ELSE excluded.link
END
"""
""",
[new_url, old_url],
)
# Mark the old feed dead.
# TODO: It is expensive and not worth it to try to load and
# re-insert all the old stuff so I'm not going to
# bother.
# Mark the old feed unsubscribed.
self.db.execute(
"""
UPDATE feeds
@ -464,5 +463,5 @@ class Database:
last_fetched_ts = ?
WHERE url = ?
""",
[feed.FEED_STATUS_DEAD, int(time.time()), old_url],
[feed.FEED_STATUS_UNSUBSCRIBED, int(time.time()), old_url],
)