forked from Furumi/furumi-web
Compare podcast alias sets in byte order on any collation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
047fc9e887
commit
99b2935b35
2 files changed
+129
-9
No files matched your search
+16
-9
@@ -101,16 +101,20 @@ pub(crate) async fn load_aliases(
|
||||
) -> Result<Vec<FeedUrl>> {
|
||||
let rows = sqlx::query(
|
||||
"SELECT feed_url FROM furumusic__podcast_feed_alias
|
||||
WHERE user_id = $1 AND podcast_id = $2 ORDER BY feed_url",
|
||||
WHERE user_id = $1 AND podcast_id = $2 ORDER BY feed_url COLLATE \"C\"",
|
||||
)
|
||||
.bind(user_id)
|
||||
.bind(podcast_id.as_str())
|
||||
.fetch_all(&mut *conn)
|
||||
.await?;
|
||||
Ok(rows
|
||||
let mut aliases = rows
|
||||
.iter()
|
||||
.map(|row| feed_url_col(row, "feed_url"))
|
||||
.collect::<sqlx::Result<Vec<_>>>()?)
|
||||
.collect::<sqlx::Result<Vec<_>>>()?;
|
||||
// `merge_feed_aliases` returns byte order; the database collation may
|
||||
// differ (ICU, glibc locales), and the comparisons below need equality.
|
||||
aliases.sort();
|
||||
Ok(aliases)
|
||||
}
|
||||
|
||||
pub(crate) fn share_from_row(row: &sqlx::postgres::PgRow) -> sqlx::Result<ShareRecord> {
|
||||
@@ -562,7 +566,7 @@ pub(crate) async fn known_episodes(
|
||||
let rows = sqlx::query(
|
||||
"SELECT episode_id, guid, audio_url, link, published_at_ms, title
|
||||
FROM furumusic__podcast_episode
|
||||
WHERE user_id = $1 AND podcast_id = $2 ORDER BY episode_id",
|
||||
WHERE user_id = $1 AND podcast_id = $2 ORDER BY episode_id COLLATE \"C\"",
|
||||
)
|
||||
.bind(user_id)
|
||||
.bind(podcast_id.as_str())
|
||||
@@ -761,13 +765,14 @@ impl PodcastStore {
|
||||
.await?;
|
||||
let rows = sqlx::query(
|
||||
"SELECT s.podcast_id, s.subscribed, s.podcast_json, s.generation, s.hlc_ms, s.op_id,
|
||||
COALESCE((SELECT array_agg(a.feed_url ORDER BY a.feed_url)
|
||||
COALESCE((SELECT array_agg(a.feed_url ORDER BY a.feed_url COLLATE \"C\")
|
||||
FROM furumusic__podcast_feed_alias a
|
||||
WHERE a.user_id = s.user_id AND a.podcast_id = s.podcast_id),
|
||||
'{}') AS aliases
|
||||
FROM furumusic__podcast_subscription s
|
||||
WHERE s.user_id = $1
|
||||
ORDER BY s.generation DESC, s.hlc_ms DESC, s.op_id DESC, s.podcast_id DESC
|
||||
ORDER BY s.generation DESC, s.hlc_ms DESC, s.op_id COLLATE \"C\" DESC,
|
||||
s.podcast_id COLLATE \"C\" DESC
|
||||
LIMIT $2",
|
||||
)
|
||||
.bind(user_id)
|
||||
@@ -796,7 +801,8 @@ impl PodcastStore {
|
||||
let shares = sqlx::query(
|
||||
"SELECT podcast_id, shared, generation, hlc_ms, op_id FROM furumusic__podcast_share
|
||||
WHERE user_id = $1
|
||||
ORDER BY generation DESC, hlc_ms DESC, op_id DESC, podcast_id DESC
|
||||
ORDER BY generation DESC, hlc_ms DESC, op_id COLLATE \"C\" DESC,
|
||||
podcast_id COLLATE \"C\" DESC
|
||||
LIMIT $2",
|
||||
)
|
||||
.bind(user_id)
|
||||
@@ -809,7 +815,8 @@ impl PodcastStore {
|
||||
let progress = sqlx::query(&format!(
|
||||
"SELECT {} FROM furumusic__podcast_progress p
|
||||
WHERE p.user_id = $1
|
||||
ORDER BY p.generation DESC, p.hlc_ms DESC, p.op_id DESC, p.episode_id DESC
|
||||
ORDER BY p.generation DESC, p.hlc_ms DESC, p.op_id COLLATE \"C\" DESC,
|
||||
p.episode_id COLLATE \"C\" DESC
|
||||
LIMIT $2",
|
||||
progress_columns("p", "")
|
||||
))
|
||||
@@ -823,7 +830,7 @@ impl PodcastStore {
|
||||
let sessions = sqlx::query(&format!(
|
||||
"SELECT {SESSION_COLUMNS} FROM furumusic__podcast_listen_session s
|
||||
WHERE s.user_id = $1
|
||||
ORDER BY s.ended_at_ms DESC, s.session_id DESC
|
||||
ORDER BY s.ended_at_ms DESC, s.session_id COLLATE \"C\" DESC
|
||||
LIMIT $2"
|
||||
))
|
||||
.bind(user_id)
|
||||
|
||||
@@ -1469,6 +1469,119 @@ async fn postgres_podcast_migrations_import_the_relay_state() {
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
/// Alias sets are compared and exported in byte order (as
|
||||
/// `merge_feed_aliases` returns them) whatever the database collation is:
|
||||
/// an ICU database sorts "a" before "B", bytes sort "B" first.
|
||||
#[tokio::test]
|
||||
#[ignore = "requires a disposable database in FURU_DEVICE_SYNC_TEST_DATABASE_URL"]
|
||||
async fn postgres_podcast_alias_sets_ignore_the_database_collation() {
|
||||
let admin_url = std::env::var(DATABASE_URL).unwrap();
|
||||
let admin = sqlx::PgPool::connect(&admin_url).await.unwrap();
|
||||
let name = format!("furu_podcast_icu_{}", random_hex(6));
|
||||
sqlx::query(&format!(
|
||||
"CREATE DATABASE {name} TEMPLATE template0 LOCALE_PROVIDER icu ICU_LOCALE 'en-US' LOCALE 'C'"
|
||||
))
|
||||
.execute(&admin)
|
||||
.await
|
||||
.unwrap();
|
||||
let (base, _) = admin_url.rsplit_once('/').unwrap();
|
||||
let url = format!("{base}/{name}");
|
||||
migrate(&url, all_migrations()).await;
|
||||
let pool = sqlx::PgPool::connect(&url).await.unwrap();
|
||||
let ordered: Vec<String> =
|
||||
sqlx::query_scalar("SELECT array_agg(x ORDER BY x) FROM (VALUES ('B'), ('a')) v(x)")
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(ordered, ["a", "B"], "the database must not sort bytes");
|
||||
|
||||
let store = PodcastStore::new(pool.clone());
|
||||
let user_id = 1;
|
||||
let feed = feed_url("feed.xml");
|
||||
let podcast_id = PodcastId::from_feed_url(&feed);
|
||||
let mixed = vec![feed_url("a"), feed_url("B"), feed_url("a-c")];
|
||||
let mut sorted = mixed.clone();
|
||||
sorted.push(feed.clone());
|
||||
sorted.sort();
|
||||
let subscribe = op(
|
||||
"tui_a",
|
||||
1,
|
||||
100,
|
||||
SyncOpPayload::PodcastSubscriptionSet {
|
||||
podcast_id: podcast_id.clone(),
|
||||
subscribed: true,
|
||||
podcast: PodcastMeta {
|
||||
feed_url: feed.clone(),
|
||||
title: "Refresh Show".into(),
|
||||
author: None,
|
||||
description: None,
|
||||
link: None,
|
||||
image_url: None,
|
||||
},
|
||||
feed_aliases: mixed.clone(),
|
||||
generation: 1,
|
||||
},
|
||||
);
|
||||
let aliases = op(
|
||||
"tui_a",
|
||||
2,
|
||||
200,
|
||||
SyncOpPayload::PodcastFeedAliasesAdded {
|
||||
podcast_id: podcast_id.clone(),
|
||||
feed_aliases: mixed.clone(),
|
||||
},
|
||||
);
|
||||
assert_eq!(
|
||||
store.apply_remote_op(user_id, &subscribe).await.unwrap(),
|
||||
ApplyOutcome::Changed
|
||||
);
|
||||
for op in [&subscribe, &aliases, &aliases, &subscribe] {
|
||||
assert_eq!(
|
||||
store.apply_remote_op(user_id, op).await.unwrap(),
|
||||
ApplyOutcome::Unchanged,
|
||||
"{}",
|
||||
op.op_id
|
||||
);
|
||||
}
|
||||
assert_eq!(
|
||||
store.feed_aliases(user_id, &podcast_id).await.unwrap(),
|
||||
sorted
|
||||
);
|
||||
let snapshot = store.export_snapshot(user_id).await.unwrap();
|
||||
assert_eq!(snapshot.podcast_subscriptions[0].feed_aliases, sorted);
|
||||
|
||||
// A permanent move to an alias already known emits nothing.
|
||||
let document = parse(
|
||||
&rss(r#"
|
||||
<item><guid>g-1</guid><title>One</title>
|
||||
<enclosure url="https://cdn.example/a1.mp3" type="audio/mpeg"/></item>"#),
|
||||
&feed_url("B"),
|
||||
);
|
||||
let refresh = store
|
||||
.apply_feed(
|
||||
user_id,
|
||||
&FeedRefresh {
|
||||
podcast_id: &podcast_id,
|
||||
feed: &document,
|
||||
fetched_from: &feed_url("B"),
|
||||
moved_permanently: true,
|
||||
},
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(refresh.ops.is_empty(), "{:?}", refresh.ops);
|
||||
assert_eq!(
|
||||
store.feed_aliases(user_id, &podcast_id).await.unwrap(),
|
||||
sorted
|
||||
);
|
||||
|
||||
pool.close().await;
|
||||
sqlx::query(&format!("DROP DATABASE {name} WITH (FORCE)"))
|
||||
.execute(&admin)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// RSS refresh job
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
Reference in new issue
Block a user