Add a PostgreSQL podcast store with relay import

PodcastStore implements the music_dht::podcast contract per account:
catalog, feed aliases, episodes with a derived season view, the
subscription, share and progress registers, listening sessions and the
local-only fetch state. Received operations, snapshot records and local
actions share one apply path (materializers plus merge functions) under
a per-account advisory lock. Local actions append their operation to the
device-sync log with the next stamp of the web device and apply exactly
that operation in the same transaction.

Migration m_0056 creates the tables; m_0057 imports the registers that
0.13 kept in furumusic__fed_podcast_relay and the catalog rows of the
episode references in the operation log. The relay table is kept.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
sanapachandClaude Opus 5.5 committed 2026-11-20 15:12:38 +02:00
1 parent cc1705457e
commit 7f07cb206a
11 files changed
+4914 -31

No files matched your search

+85 -31
View File
@@ -1782,20 +1782,7 @@ async fn record_local_op(
);
}
let now = now_ms();
let row = sqlx::query(
"UPDATE furumusic__fed_device_identity
SET local_seq = local_seq + 1,
last_hlc_ms = GREATEST($2, last_hlc_ms + 1)
WHERE user_id = $1
RETURNING local_seq, last_hlc_ms",
)
.bind(user_id)
.bind(now)
.fetch_one(pool)
.await?;
let seq: i64 = row.get("local_seq");
let hlc_ms: i64 = row.get("last_hlc_ms");
let (seq, hlc_ms) = reserve_local_stamp(pool, user_id).await?;
let op_id = format!("{}:{seq}", identity.device_id);
let op = SyncOpWire {
op_id,
@@ -1805,6 +1792,40 @@ async fn record_local_op(
payload,
};
insert_op(pool, user_id, &op).await?;
advance_vector(pool, user_id, &op.origin_device_id, op.seq).await?;
let _ = apply_op(pool, Arc::new(PlayerDeviceHub::default()), user_id, &op).await?;
Ok(())
}
/// Reserves the next local `(seq, hlc_ms)` of the account's web device.
///
/// `hlc_ms` is the wall clock, but always past the previous local
/// operation. Inside a transaction the identity row stays locked until it
/// ends, so local operations of one account are stamped in commit order.
async fn reserve_local_stamp<'e>(
db: impl sqlx::PgExecutor<'e>,
user_id: i64,
) -> Result<(i64, i64)> {
let row = sqlx::query(
"UPDATE furumusic__fed_device_identity
SET local_seq = local_seq + 1,
last_hlc_ms = GREATEST($2, last_hlc_ms + 1)
WHERE user_id = $1
RETURNING local_seq, last_hlc_ms",
)
.bind(user_id)
.bind(now_ms())
.fetch_one(db)
.await?;
Ok((row.get("local_seq"), row.get("last_hlc_ms")))
}
async fn advance_vector<'e>(
db: impl sqlx::PgExecutor<'e>,
user_id: i64,
device_id: &str,
seq: i64,
) -> Result<()> {
sqlx::query(
"INSERT INTO furumusic__fed_sync_vector (user_id, device_id, max_seq)
VALUES ($1, $2, $3)
@@ -1812,14 +1833,53 @@ async fn record_local_op(
max_seq = GREATEST(furumusic__fed_sync_vector.max_seq, EXCLUDED.max_seq)",
)
.bind(user_id)
.bind(&op.origin_device_id)
.bind(op.seq)
.execute(pool)
.bind(device_id)
.bind(seq)
.execute(db)
.await?;
let _ = apply_op(pool, Arc::new(PlayerDeviceHub::default()), user_id, &op).await?;
Ok(())
}
/// Device id of the account's web device, creating the identity on first
/// use. Local podcast actions stamp their operations with it.
#[cfg_attr(not(test), allow(dead_code))] // podcast UI and RSS job: later commits
pub(crate) async fn local_device_id(pool: &sqlx::PgPool, user_id: i64) -> Result<String> {
Ok(ensure_identity(pool, user_id, "").await?.device_id)
}
/// Appends a local operation of `device_id` (the account's web device, see
/// [`local_device_id`]) to the sync log inside the caller's transaction.
///
/// The stamp (`seq`, `hlc_ms`, `op_id = <device>:<seq>`) is reserved here;
/// `make` receives the `op_id`. The caller applies the returned operation
/// itself in the same transaction, so the log and the materialized state
/// change together (the podcast store does this for its local actions).
#[cfg_attr(not(test), allow(dead_code))] // podcast UI and RSS job: later commits
pub(crate) async fn append_local_op(
conn: &mut sqlx::PgConnection,
user_id: i64,
device_id: &str,
make: impl FnOnce(&str) -> SyncOpPayload,
) -> Result<SyncOpWire> {
let (seq, hlc_ms) = reserve_local_stamp(&mut *conn, user_id).await?;
let op_id = format!("{device_id}:{seq}");
let payload = make(&op_id);
let op = SyncOpWire {
op_id,
origin_device_id: device_id.to_string(),
seq,
hlc_ms,
payload,
};
anyhow::ensure!(
insert_op(&mut *conn, user_id, &op).await?,
"local operation {} already exists",
op.op_id
);
advance_vector(&mut *conn, user_id, device_id, seq).await?;
Ok(op)
}
async fn apply_ops(
pool: &sqlx::PgPool,
hub: Arc<PlayerDeviceHub>,
@@ -1837,23 +1897,17 @@ async fn apply_ops(
} else if insert_op(pool, user_id, &op).await? {
let _ = apply_op(pool, Arc::clone(&hub), user_id, &op).await?;
}
sqlx::query(
"INSERT INTO furumusic__fed_sync_vector (user_id, device_id, max_seq)
VALUES ($1, $2, $3)
ON CONFLICT (user_id, device_id) DO UPDATE SET
max_seq = GREATEST(furumusic__fed_sync_vector.max_seq, EXCLUDED.max_seq)",
)
.bind(user_id)
.bind(&op.origin_device_id)
.bind(op.seq)
.execute(pool)
.await?;
advance_vector(pool, user_id, &op.origin_device_id, op.seq).await?;
}
Ok(())
}
/// Appends an operation to the log; `false` when it was already stored.
async fn insert_op(pool: &sqlx::PgPool, user_id: i64, op: &SyncOpWire) -> Result<bool> {
async fn insert_op<'e>(
db: impl sqlx::PgExecutor<'e>,
user_id: i64,
op: &SyncOpWire,
) -> Result<bool> {
let inserted = sqlx::query(
"INSERT INTO furumusic__fed_sync_ops
(user_id, op_id, origin_device_id, seq, kind, payload_json, hlc_ms,
@@ -1870,7 +1924,7 @@ async fn insert_op(pool: &sqlx::PgPool, user_id: i64, op: &SyncOpWire) -> Result
.bind(op.hlc_ms)
.bind(now_ms())
.bind(op.payload.is_tombstone())
.execute(pool)
.execute(db)
.await?
.rows_affected();
Ok(inserted > 0)