fix: sync relays table to config on startup — removes stale relay rows
This commit is contained in:
@@ -406,6 +406,30 @@ pub async fn upsert_relay(pool: &SqlitePool, url: &str) -> anyhow::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Sync the relays table to exactly match the configured relay list.
|
||||
/// Removes stale rows (old config), upserts current ones.
|
||||
pub async fn sync_relays(pool: &SqlitePool, urls: &[String]) -> anyhow::Result<()> {
|
||||
// Delete rows not in the current config.
|
||||
// SQLite doesn't support array binding, so build the IN clause manually.
|
||||
if urls.is_empty() {
|
||||
sqlx::query("DELETE FROM relays").execute(pool).await?;
|
||||
return Ok(());
|
||||
}
|
||||
let placeholders = urls.iter().map(|_| "?").collect::<Vec<_>>().join(",");
|
||||
let sql = format!("DELETE FROM relays WHERE url NOT IN ({placeholders})");
|
||||
let mut q = sqlx::query(&sql);
|
||||
for url in urls {
|
||||
q = q.bind(url);
|
||||
}
|
||||
q.execute(pool).await?;
|
||||
|
||||
// Upsert current relays.
|
||||
for url in urls {
|
||||
upsert_relay(pool, url).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn get_relay_last_event(pool: &SqlitePool, url: &str) -> anyhow::Result<Option<i64>> {
|
||||
let row: Option<(Option<i64>,)> =
|
||||
sqlx::query_as("SELECT last_event FROM relays WHERE url = ?")
|
||||
|
||||
@@ -47,6 +47,8 @@ async fn main() -> anyhow::Result<()> {
|
||||
let pool = db::open(&cfg.database.path).await?;
|
||||
info!("database ready");
|
||||
|
||||
db::sync_relays(&pool, &cfg.relays).await?;
|
||||
|
||||
let relay_count = Arc::new(AtomicI32::new(0));
|
||||
let cfg = Arc::new(cfg);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user