fix(arr): rewrite subtitle_files paths on relocate
relocate.rs gathered only media_files rows for a title/root move, leaving subtitle_files rows pointing at the old folder. Sidecars already ride along in the folder rename; only their rows were stale. Tags each rewrite with its owning table (media_files or subtitle_files) and updates both in the same transaction.
This commit is contained in:
@@ -1747,6 +1747,66 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
/// Issue #237: the folder rename #228 wired up already carries a sidecar
|
||||
/// with it, but the `subtitle_files` row pointing at the old path did not
|
||||
/// follow — it went stale silently, and `DELETE /api/subtitles/{id}`
|
||||
/// (#218) would then report success while leaving the real file on disk.
|
||||
#[tokio::test]
|
||||
async fn changing_root_moves_the_subtitle_row_with_its_sidecar() {
|
||||
let (_dir, state, base) = application().await;
|
||||
let movie = add_movie(&base, 693_134, 1).await;
|
||||
let id = movie["id"].as_i64().expect("id");
|
||||
let source = tempfile::tempdir().expect("source root");
|
||||
let destination = tempfile::tempdir().expect("destination root");
|
||||
let folder = library_on_disk(&state, id, source.path()).await;
|
||||
point_root_at(&state, 2, destination.path()).await;
|
||||
|
||||
let pool = state.database().expect("database").pool();
|
||||
let media_file_id: i64 = sqlx::query_scalar(
|
||||
"SELECT id FROM media_files WHERE owner_kind = 'movie' AND owner_id = ?",
|
||||
)
|
||||
.bind(id)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.expect("media file id");
|
||||
let sidecar = folder.join("dune.pt.srt");
|
||||
sqlx::query(
|
||||
"INSERT INTO subtitle_files (media_file_id, language, origin, provider, path)
|
||||
VALUES (?, 'pt-PT', 'provider', 'opensubtitles', ?)",
|
||||
)
|
||||
.bind(media_file_id)
|
||||
.bind(sidecar.to_str().expect("utf-8 path"))
|
||||
.execute(pool)
|
||||
.await
|
||||
.expect("subtitle file");
|
||||
|
||||
let updated: serde_json::Value = reqwest::Client::new()
|
||||
.patch(format!("{base}/api/movies/{id}"))
|
||||
.json(&serde_json::json!({"root_id": 2}))
|
||||
.send()
|
||||
.await
|
||||
.expect("move root")
|
||||
.json()
|
||||
.await
|
||||
.expect("updated json");
|
||||
assert_eq!(updated["root_id"], 2);
|
||||
|
||||
let path: String =
|
||||
sqlx::query_scalar("SELECT path FROM subtitle_files WHERE media_file_id = ?")
|
||||
.bind(media_file_id)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.expect("subtitle file row");
|
||||
assert!(
|
||||
std::path::Path::new(&path).starts_with(destination.path()),
|
||||
"the subtitle row follows the file: {path}"
|
||||
);
|
||||
assert!(
|
||||
std::path::Path::new(&path).exists(),
|
||||
"the rewritten subtitle path describes the disk"
|
||||
);
|
||||
}
|
||||
|
||||
/// Reported in production: moving a title into a root whose directory has
|
||||
/// never been written to failed with
|
||||
/// `could not move '...The Batman...': No such file or directory`, naming
|
||||
|
||||
+149
-51
@@ -1,9 +1,12 @@
|
||||
//! Moving library files when the layout under them changes: a title changing
|
||||
//! its `root_id` (issue #228), and a root changing its `path` (issue #236).
|
||||
//! Both rename §7.4 folders and rewrite the `media_files` rows to match, so
|
||||
//! the layout keeps describing the disk. This module only moves things: the
|
||||
//! stored verdicts a root change invalidates (§5.1) are re-derived by the
|
||||
//! calling handler through `reclassify`, after the row commits.
|
||||
//! Both rename §7.4 folders and rewrite the `media_files` and `subtitle_files`
|
||||
//! rows to match, so the layout keeps describing the disk. Sidecars (§15) live
|
||||
//! inside the same folder as the video, so the rename already carries them —
|
||||
//! only their rows need rewriting (issue #237). This module only moves
|
||||
//! things: the stored verdicts a root change invalidates (§5.1) are
|
||||
//! re-derived by the calling handler through `reclassify`, after the row
|
||||
//! commits.
|
||||
//!
|
||||
//! Every root shares one filesystem — one ZFS dataset, bind-mounted — so this
|
||||
//! is a directory rename, never a copy. Hardlinked files keep their inodes
|
||||
@@ -35,6 +38,15 @@ pub(crate) enum TitleKind {
|
||||
Series,
|
||||
}
|
||||
|
||||
/// Which table a rewritten row belongs to — `rewrite_rows` needs this to
|
||||
/// target the right `UPDATE`, since a `media_files` id and a `subtitle_files`
|
||||
/// id share no namespace.
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
enum FileTable {
|
||||
Media,
|
||||
Subtitle,
|
||||
}
|
||||
|
||||
/// One rename from the old root into the new one: a §7.4 title folder, or a
|
||||
/// loose file sitting straight in the root.
|
||||
#[derive(Debug)]
|
||||
@@ -49,7 +61,7 @@ struct PlannedRename {
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct Relocation {
|
||||
performed: Vec<PlannedRename>,
|
||||
rewrites: Vec<(i64, String)>,
|
||||
rewrites: Vec<(FileTable, i64, String)>,
|
||||
/// Every directory level this request materialised for the new root,
|
||||
/// deepest first. `create_dir_all` can make more than one — moving a
|
||||
/// root to `/mnt/media-v2/tv/kids` when `/mnt/media-v2` is all that
|
||||
@@ -112,13 +124,13 @@ pub(crate) async fn relocate_root(
|
||||
/// destination that already exists, then perform them, undoing what was
|
||||
/// performed if one fails.
|
||||
async fn relocate_files(
|
||||
files: &[(i64, String)],
|
||||
files: &[(FileTable, i64, String)],
|
||||
old_root: &str,
|
||||
new_root: &str,
|
||||
) -> Result<Relocation, ApiError> {
|
||||
let mut renames: Vec<PlannedRename> = Vec::new();
|
||||
let mut rewrites: Vec<(i64, String)> = Vec::new();
|
||||
for (file_id, path) in files {
|
||||
let mut rewrites: Vec<(FileTable, i64, String)> = Vec::new();
|
||||
for (table, file_id, path) in files {
|
||||
let Some(source) = title_target(old_root, path) else {
|
||||
// Outside its own root: not ours to move, and the row keeps
|
||||
// pointing at where the file really is.
|
||||
@@ -144,7 +156,7 @@ async fn relocate_files(
|
||||
"non-UTF-8 path under {new_root}"
|
||||
)));
|
||||
};
|
||||
rewrites.push((*file_id, rewritten.to_owned()));
|
||||
rewrites.push((*table, *file_id, rewritten.to_owned()));
|
||||
}
|
||||
|
||||
// Every destination is checked before anything is renamed, so a conflict
|
||||
@@ -275,21 +287,34 @@ impl Relocation {
|
||||
!self.performed.is_empty()
|
||||
}
|
||||
|
||||
/// Point the `media_files` rows at the new root, inside the caller's
|
||||
/// transaction so they land together with the `root_id` change or not at
|
||||
/// all.
|
||||
/// Point the `media_files` and `subtitle_files` rows at the new root,
|
||||
/// inside the caller's transaction so they land together with the
|
||||
/// `root_id` change or not at all.
|
||||
pub(crate) async fn rewrite_rows(
|
||||
&self,
|
||||
transaction: &mut sqlx::Transaction<'_, sqlx::Sqlite>,
|
||||
) -> Result<(), sqlx::Error> {
|
||||
for (file_id, path) in &self.rewrites {
|
||||
sqlx::query!(
|
||||
"UPDATE media_files SET path = ?, updated_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now') WHERE id = ?",
|
||||
path,
|
||||
file_id
|
||||
)
|
||||
.execute(&mut **transaction)
|
||||
.await?;
|
||||
for (table, file_id, path) in &self.rewrites {
|
||||
match table {
|
||||
FileTable::Media => {
|
||||
sqlx::query!(
|
||||
"UPDATE media_files SET path = ?, updated_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now') WHERE id = ?",
|
||||
path,
|
||||
file_id
|
||||
)
|
||||
.execute(&mut **transaction)
|
||||
.await?;
|
||||
}
|
||||
FileTable::Subtitle => {
|
||||
sqlx::query!(
|
||||
"UPDATE subtitle_files SET path = ?, updated_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now') WHERE id = ?",
|
||||
path,
|
||||
file_id
|
||||
)
|
||||
.execute(&mut **transaction)
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -346,12 +371,17 @@ async fn root_path(state: &AppState, root_id: i64) -> Result<String, ApiError> {
|
||||
.await?)
|
||||
}
|
||||
|
||||
/// Every file the service recorded under a root: the rows of every movie in
|
||||
/// it, and the rows of every episode of every series in it. Ordered so the
|
||||
/// Every file the service recorded under a root: the `media_files` rows of
|
||||
/// every movie and episode in it, and the `subtitle_files` sidecars (§15) on
|
||||
/// those files — an embedded track has no `path` and nothing on disk to
|
||||
/// carry, so it is excluded rather than rewritten to nothing. Ordered so the
|
||||
/// renames happen in a stable order, which is what makes a failure part-way
|
||||
/// through reproducible.
|
||||
async fn root_files(state: &AppState, root_id: i64) -> Result<Vec<(i64, String)>, ApiError> {
|
||||
let mut files: Vec<(i64, String)> = sqlx::query!(
|
||||
async fn root_files(
|
||||
state: &AppState,
|
||||
root_id: i64,
|
||||
) -> Result<Vec<(FileTable, i64, String)>, ApiError> {
|
||||
let mut files: Vec<(FileTable, i64, String)> = sqlx::query!(
|
||||
r#"SELECT mf.id AS "id!: i64", mf.path AS "path!: String"
|
||||
FROM media_files mf
|
||||
JOIN movies m ON mf.owner_kind = 'movie' AND m.id = mf.owner_id
|
||||
@@ -362,7 +392,7 @@ async fn root_files(state: &AppState, root_id: i64) -> Result<Vec<(i64, String)>
|
||||
.fetch_all(pool(state)?)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|row| (row.id, row.path))
|
||||
.map(|row| (FileTable::Media, row.id, row.path))
|
||||
.collect();
|
||||
files.extend(
|
||||
sqlx::query!(
|
||||
@@ -378,41 +408,109 @@ async fn root_files(state: &AppState, root_id: i64) -> Result<Vec<(i64, String)>
|
||||
.fetch_all(pool(state)?)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|row| (row.id, row.path)),
|
||||
.map(|row| (FileTable::Media, row.id, row.path)),
|
||||
);
|
||||
files.extend(
|
||||
sqlx::query!(
|
||||
r#"SELECT sf.id AS "id!: i64", sf.path AS "path!: String"
|
||||
FROM subtitle_files sf
|
||||
JOIN media_files mf ON mf.id = sf.media_file_id
|
||||
JOIN movies m ON mf.owner_kind = 'movie' AND m.id = mf.owner_id
|
||||
WHERE m.root_id = ? AND sf.path IS NOT NULL
|
||||
ORDER BY sf.id"#,
|
||||
root_id
|
||||
)
|
||||
.fetch_all(pool(state)?)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|row| (FileTable::Subtitle, row.id, row.path)),
|
||||
);
|
||||
files.extend(
|
||||
sqlx::query!(
|
||||
r#"SELECT sf.id AS "id!: i64", sf.path AS "path!: String"
|
||||
FROM subtitle_files sf
|
||||
JOIN media_files mf ON mf.id = sf.media_file_id
|
||||
JOIN episodes e ON mf.owner_kind = 'episode' AND e.id = mf.owner_id
|
||||
JOIN seasons se ON se.id = e.season_id
|
||||
JOIN series s ON s.id = se.series_id
|
||||
WHERE s.root_id = ? AND sf.path IS NOT NULL
|
||||
ORDER BY sf.id"#,
|
||||
root_id
|
||||
)
|
||||
.fetch_all(pool(state)?)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|row| (FileTable::Subtitle, row.id, row.path)),
|
||||
);
|
||||
Ok(files)
|
||||
}
|
||||
|
||||
/// Every file the service recorded for the title: a movie's own rows, or the
|
||||
/// rows of every episode below a series.
|
||||
/// rows of every episode below a series — plus the `subtitle_files` sidecars
|
||||
/// (§15) on those files. An embedded track has no `path` and nothing on disk
|
||||
/// to carry, so it is excluded rather than rewritten to nothing.
|
||||
async fn title_files(
|
||||
state: &AppState,
|
||||
kind: TitleKind,
|
||||
title_id: i64,
|
||||
) -> Result<Vec<(i64, String)>, ApiError> {
|
||||
) -> Result<Vec<(FileTable, i64, String)>, ApiError> {
|
||||
Ok(match kind {
|
||||
TitleKind::Movie => sqlx::query!(
|
||||
r#"SELECT id AS "id!: i64", path AS "path!: String"
|
||||
FROM media_files WHERE owner_kind = 'movie' AND owner_id = ?"#,
|
||||
title_id
|
||||
)
|
||||
.fetch_all(pool(state)?)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|row| (row.id, row.path))
|
||||
.collect(),
|
||||
TitleKind::Series => sqlx::query!(
|
||||
r#"SELECT mf.id AS "id!: i64", mf.path AS "path!: String"
|
||||
FROM media_files mf
|
||||
JOIN episodes e ON mf.owner_kind = 'episode' AND e.id = mf.owner_id
|
||||
JOIN seasons se ON se.id = e.season_id
|
||||
WHERE se.series_id = ?"#,
|
||||
title_id
|
||||
)
|
||||
.fetch_all(pool(state)?)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|row| (row.id, row.path))
|
||||
.collect(),
|
||||
TitleKind::Movie => {
|
||||
let mut files: Vec<(FileTable, i64, String)> = sqlx::query!(
|
||||
r#"SELECT id AS "id!: i64", path AS "path!: String"
|
||||
FROM media_files WHERE owner_kind = 'movie' AND owner_id = ?"#,
|
||||
title_id
|
||||
)
|
||||
.fetch_all(pool(state)?)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|row| (FileTable::Media, row.id, row.path))
|
||||
.collect();
|
||||
files.extend(
|
||||
sqlx::query!(
|
||||
r#"SELECT sf.id AS "id!: i64", sf.path AS "path!: String"
|
||||
FROM subtitle_files sf
|
||||
JOIN media_files mf ON mf.id = sf.media_file_id
|
||||
WHERE mf.owner_kind = 'movie' AND mf.owner_id = ? AND sf.path IS NOT NULL"#,
|
||||
title_id
|
||||
)
|
||||
.fetch_all(pool(state)?)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|row| (FileTable::Subtitle, row.id, row.path)),
|
||||
);
|
||||
files
|
||||
}
|
||||
TitleKind::Series => {
|
||||
let mut files: Vec<(FileTable, i64, String)> = sqlx::query!(
|
||||
r#"SELECT mf.id AS "id!: i64", mf.path AS "path!: String"
|
||||
FROM media_files mf
|
||||
JOIN episodes e ON mf.owner_kind = 'episode' AND e.id = mf.owner_id
|
||||
JOIN seasons se ON se.id = e.season_id
|
||||
WHERE se.series_id = ?"#,
|
||||
title_id
|
||||
)
|
||||
.fetch_all(pool(state)?)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|row| (FileTable::Media, row.id, row.path))
|
||||
.collect();
|
||||
files.extend(
|
||||
sqlx::query!(
|
||||
r#"SELECT sf.id AS "id!: i64", sf.path AS "path!: String"
|
||||
FROM subtitle_files sf
|
||||
JOIN media_files mf ON mf.id = sf.media_file_id
|
||||
JOIN episodes e ON mf.owner_kind = 'episode' AND e.id = mf.owner_id
|
||||
JOIN seasons se ON se.id = e.season_id
|
||||
WHERE se.series_id = ? AND sf.path IS NOT NULL"#,
|
||||
title_id
|
||||
)
|
||||
.fetch_all(pool(state)?)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|row| (FileTable::Subtitle, row.id, row.path)),
|
||||
);
|
||||
files
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user