forked from Furumi/furumi-web
Undo metadata deletions archived before the typed queue column
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a9736e5914
commit
19139ea830
2 files changed
+50
-1
No files matched your search
@@ -1524,7 +1524,9 @@ async fn restore_deleted(conn: &mut PgConnection, archived: &[ArchivedRows]) ->
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join(",");
|
||||
let affected = sqlx::query(&format!("UPDATE {table} t SET {assignments} FROM jsonb_array_elements($1::jsonb) WITH ORDINALITY b(value,n) JOIN jsonb_array_elements($2::jsonb) WITH ORDINALITY a(value,n) USING(n) WHERE to_jsonb(t)=a.value"))
|
||||
// Archives written before m_0054 have no `queue_items_json` key;
|
||||
// compare them without the column added since.
|
||||
let affected = sqlx::query(&format!("UPDATE {table} t SET {assignments} FROM jsonb_array_elements($1::jsonb) WITH ORDINALITY b(value,n) JOIN jsonb_array_elements($2::jsonb) WITH ORDINALITY a(value,n) USING(n) WHERE (CASE WHEN a.value ? 'queue_items_json' THEN to_jsonb(t) ELSE to_jsonb(t) - 'queue_items_json' END)=a.value"))
|
||||
.bind(entry.rows.to_string()).bind(after.to_string()).execute(&mut *conn).await?.rows_affected();
|
||||
anyhow::ensure!(
|
||||
affected as usize == after.as_array().map_or(0, Vec::len),
|
||||
@@ -1813,6 +1815,51 @@ pub(super) async fn undo(
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "requires a migrated disposable database in FURU_METADATA_FIX_TEST_DATABASE_URL"]
|
||||
async fn undo_accepts_playback_archives_written_before_the_typed_queue() {
|
||||
let pool =
|
||||
sqlx::PgPool::connect(&std::env::var("FURU_METADATA_FIX_TEST_DATABASE_URL").unwrap())
|
||||
.await
|
||||
.unwrap();
|
||||
let mut tx = pool.begin().await.unwrap();
|
||||
let user_id = 9_000_000_000 + i64::from(std::process::id());
|
||||
// State after the deletion, read back like an archive written by 0.12
|
||||
// (`to_jsonb` of the row before m_0054 added `queue_items_json`).
|
||||
let after: serde_json::Value = sqlx::query_scalar(
|
||||
"INSERT INTO furumusic__playback_state (user_id,current_track_id,position_ms,queue_json,queue_position,shuffle,repeat_mode,volume,updated_at) VALUES ($1,NULL,0,'[]',0,false,'off',0.7,'now') RETURNING to_jsonb(furumusic__playback_state) - 'queue_items_json'",
|
||||
)
|
||||
.bind(user_id)
|
||||
.fetch_one(&mut *tx)
|
||||
.await
|
||||
.unwrap();
|
||||
let mut before = after.clone();
|
||||
before["queue_json"] = serde_json::json!("[123456789]");
|
||||
before["position_ms"] = serde_json::json!(5000);
|
||||
let archive = ArchivedRows {
|
||||
table: "furumusic__playback_state".into(),
|
||||
rows: serde_json::json!([before]),
|
||||
after: Some(serde_json::json!([after])),
|
||||
};
|
||||
restore_deleted(&mut tx, std::slice::from_ref(&archive))
|
||||
.await
|
||||
.unwrap();
|
||||
let (queue_json, position_ms, typed): (String, i32, Option<serde_json::Value>) =
|
||||
sqlx::query_as(
|
||||
"SELECT queue_json,position_ms,queue_items_json FROM furumusic__playback_state WHERE user_id=$1",
|
||||
)
|
||||
.bind(user_id)
|
||||
.fetch_one(&mut *tx)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(queue_json, "[123456789]");
|
||||
assert_eq!(position_ms, 5000);
|
||||
assert_eq!(typed, None);
|
||||
// A row changed since the deletion is still refused.
|
||||
assert!(restore_deleted(&mut tx, &[archive]).await.is_err());
|
||||
tx.rollback().await.unwrap();
|
||||
}
|
||||
|
||||
fn snapshot() -> Snapshot {
|
||||
Snapshot {
|
||||
artists: vec![ArtistState {
|
||||
|
||||
Reference in new issue
Block a user