fix: upsert publisher row on torrent ingest, backfill migration

insert_torrent now upserts into publishers so torrent authors appear
in the publishers table immediately, making the name/nip05 join work
on the indexed and dashboard pages. Migration 006 backfills existing
torrent pubkeys on fresh DB opens.
This commit is contained in:
2026-05-17 18:48:07 -07:00
parent fa7e6052fa
commit 184d42907b
2 changed files with 22 additions and 0 deletions
@@ -0,0 +1,9 @@
-- Backfill publishers table from existing torrent pubkeys.
-- Safe to run on databases that already have publisher rows (ON CONFLICT is a no-op for those).
INSERT INTO publishers (pubkey, torrents_n, last_seen)
SELECT pubkey, COUNT(*), MAX(ingested_at)
FROM torrents
GROUP BY pubkey
ON CONFLICT(pubkey) DO UPDATE SET
torrents_n = MAX(publishers.torrents_n, excluded.torrents_n),
last_seen = MAX(publishers.last_seen, excluded.last_seen);
+13
View File
@@ -87,6 +87,19 @@ pub async fn insert_torrent(pool: &SqlitePool, r: &TorrentRecord) -> anyhow::Res
return Ok(());
}
// Ensure the publisher row exists and bump their counters
sqlx::query(
"INSERT INTO publishers (pubkey, torrents_n, last_seen)
VALUES (?, 1, ?)
ON CONFLICT(pubkey) DO UPDATE SET
torrents_n = torrents_n + 1,
last_seen = excluded.last_seen",
)
.bind(&r.pubkey)
.bind(r.ingested_at)
.execute(&mut *tx)
.await?;
for url in &r.trackers {
sqlx::query("INSERT OR IGNORE INTO trackers (event_id, url) VALUES (?,?)")
.bind(&r.event_id)