bugfix: wrap manga + chapter uploads in a DB transaction

Previously a storage failure mid-chapter-upload left a partial chapter
row pointing at a `page_count` that didn't match what was on disk, plus
any successfully-inserted page rows. Same shape for a manga create
where the cover put or cover_image_path UPDATE failed after the manga
row was already inserted.

Fix at the DB layer: open `pool.begin()` at the start of the create,
do all DB writes against `&mut *tx`, commit only after the full
sequence succeeds. If anything before commit fails, the transaction is
rolled back on drop and the DB stays consistent. Bytes already written
to storage on a rolled-back transaction become orphans on disk; a
future reaper can sweep them, and we prioritise DB consistency over
storage tidiness in this branch.

- repo::manga::create / set_cover_image_path: signature changed to
  `impl PgExecutor<'_>` so handlers can pass either `&PgPool` or
  `&mut *tx`. set_cover_image_path is new — replaces the inline
  `UPDATE` in the manga upload handler so the call site stays
  consistent.
- repo::chapter::create / set_page_count: same shape.
- repo::page::create: same.
- api::mangas::create and api::chapters::create both open a
  transaction around their DB writes; storage puts happen inside the
  transaction window (since they must precede the page-row insert), so
  a failed put aborts before commit.

New integration test (api_uploads::chapter_upload_rolls_back_when_
storage_fails_mid_loop) uses a `FailingStorage` helper that errors on
the N-th `put`. With N=1 (page 2 fails), the handler returns 500 and
the chapter + page tables stay empty.

`harness_with_failing_storage` is exposed alongside the existing
`harness` so future tests can reuse it for other fault-injection
cases.

Lockstep version bump to 0.9.3.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
MechaCat02
2026-05-16 23:25:13 +02:00
parent 563524d51e
commit 80ab119750
10 changed files with 187 additions and 35 deletions

View File

@@ -16,7 +16,10 @@ use tower::ServiceExt;
use mangalord::app::{router, AppState};
use mangalord::config::{AuthConfig, UploadConfig};
use mangalord::storage::LocalStorage;
use mangalord::storage::{LocalStorage, Storage, StorageError, StreamingFile};
use async_trait::async_trait;
use std::sync::atomic::{AtomicUsize, Ordering};
pub struct Harness {
pub app: Router,
@@ -26,9 +29,29 @@ pub struct Harness {
pub fn harness(pool: PgPool) -> Harness {
let storage_dir = tempfile::tempdir().expect("tempdir");
let storage = Arc::new(LocalStorage::new(storage_dir.path()));
harness_inner(pool, storage, storage_dir)
}
/// Variant of `harness` that swaps in a `Storage` that errors on the
/// `fail_on_put_index`-th `put` call (0-indexed). Used to exercise the
/// upload handlers' transactional rollback path without resorting to
/// fault injection at lower layers.
pub fn harness_with_failing_storage(pool: PgPool, fail_on_put_index: usize) -> Harness {
let storage_dir = tempfile::tempdir().expect("tempdir");
let inner = LocalStorage::new(storage_dir.path());
let storage = Arc::new(FailingStorage::new(inner, fail_on_put_index));
harness_inner(pool, storage, storage_dir)
}
fn harness_inner(
pool: PgPool,
storage: Arc<dyn Storage>,
storage_dir: TempDir,
) -> Harness {
let state = AppState {
db: pool,
storage: Arc::new(LocalStorage::new(storage_dir.path())),
storage,
auth: AuthConfig { cookie_secure: false, ..AuthConfig::default() },
upload: UploadConfig {
// Keep file caps small in tests so the size-cap path is cheap to
@@ -40,6 +63,50 @@ pub fn harness(pool: PgPool) -> Harness {
Harness { app: router(state), _storage_dir: storage_dir }
}
/// Wraps a real `Storage` and fails on the N-th `put` call so tests can
/// assert that handlers roll their DB writes back when storage errors
/// mid-upload. Reads and other operations delegate to `inner`.
pub struct FailingStorage {
inner: LocalStorage,
counter: AtomicUsize,
fail_on_put_index: usize,
}
impl FailingStorage {
pub fn new(inner: LocalStorage, fail_on_put_index: usize) -> Self {
Self {
inner,
counter: AtomicUsize::new(0),
fail_on_put_index,
}
}
}
#[async_trait]
impl Storage for FailingStorage {
async fn put(&self, key: &str, bytes: &[u8]) -> Result<(), StorageError> {
let n = self.counter.fetch_add(1, Ordering::SeqCst);
if n == self.fail_on_put_index {
return Err(StorageError::Io(std::io::Error::other(
"FailingStorage: injected put failure",
)));
}
self.inner.put(key, bytes).await
}
async fn get(&self, key: &str) -> Result<Vec<u8>, StorageError> {
self.inner.get(key).await
}
async fn get_stream(&self, key: &str) -> Result<StreamingFile, StorageError> {
self.inner.get_stream(key).await
}
async fn delete(&self, key: &str) -> Result<(), StorageError> {
self.inner.delete(key).await
}
async fn exists(&self, key: &str) -> Result<bool, StorageError> {
self.inner.exists(key).await
}
}
pub async fn body_json(response: axum::response::Response) -> serde_json::Value {
let bytes = response.into_body().collect().await.unwrap().to_bytes();
serde_json::from_slice(&bytes).expect("body is JSON")