FIX: first_seen re-stamp bug — persist true source publish date, idempotent upsert, backfill DB
This commit is contained in:
+9
-21
@@ -25,6 +25,7 @@ from datetime import datetime, timezone
|
||||
sys.path.insert(0, os.path.dirname(__file__))
|
||||
|
||||
from adapters import SourceAdapter
|
||||
from adapters._store import upsert_entries
|
||||
|
||||
# Adapter registry — add new adapters here (one line each)
|
||||
ADAPTERS = {
|
||||
@@ -51,27 +52,14 @@ def init_db(db_path: str, schema_path: str) -> sqlite3.Connection:
|
||||
|
||||
|
||||
def store_entries(conn: sqlite3.Connection, entries: list[dict]) -> int:
|
||||
"""Store entries using INSERT OR REPLACE (dedup by source+source_id)."""
|
||||
cur = conn.cursor()
|
||||
stored = 0
|
||||
for entry in entries:
|
||||
try:
|
||||
cur.execute("""
|
||||
INSERT OR REPLACE INTO entries
|
||||
(source, source_id, url, title, extracted_text, summary,
|
||||
category_tags, signal_score, raw_metadata, first_seen, last_updated)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""", (
|
||||
entry["source"], entry["source_id"], entry["url"], entry["title"],
|
||||
entry["extracted_text"] or "", entry["summary"], # None → NULL in DB
|
||||
entry["category_tags"], entry["signal_score"],
|
||||
entry["raw_metadata"], entry["first_seen"], entry["last_updated"],
|
||||
))
|
||||
stored += 1
|
||||
except Exception as e:
|
||||
print(f" ⚠ DB error on {entry.get('source', '?')}/{entry.get('source_id', '?')}: {e}")
|
||||
conn.commit()
|
||||
return stored
|
||||
"""Store entries idempotently (dedup by url).
|
||||
|
||||
FIX (2026-07-13, Tony): was INSERT OR REPLACE which OVERWROTE first_seen
|
||||
with the harvest time on every re-harvest, turning stale stories into
|
||||
"today". Now uses an UPSERT that preserves the ORIGINAL first_seen and only
|
||||
bumps last_updated. Entries must already carry first_seen = true publish date.
|
||||
"""
|
||||
return upsert_entries(conn, entries)
|
||||
|
||||
|
||||
def verify_entries(conn: sqlite3.Connection, source: str, sample_size: int = 3):
|
||||
|
||||
Reference in New Issue
Block a user