From c82aa17c3184e4d6cd09439cbcc9a68a64ad7eb6 Mon Sep 17 00:00:00 2001 From: Harry Stern Date: Sun, 9 Nov 2025 22:46:05 -0500 Subject: [PATCH] Fail when loading database from dump errors At some point someone will probably file an issue saying "I don't want to error when my dump is partially corrupt" but oh well. Co-authored-by: Markus Schirp Co-authored-by: Brage <5640782+2xic@users.noreply.github.com> --- src/lib.rs | 8 +++++++- tests/dump.rs | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 6cd2090..e7f5e29 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -116,7 +116,13 @@ impl PgTempDB { let load_output = std::process::Command::new("psql") .arg(self.connection_uri()) - .args(["--file", path_str]) + .args([ + "--file", + path_str, + "--set", + "ON_ERROR_STOP=1", + "--single-transaction", + ]) .output() .expect("failed to start psql. Is it installed and on your path?"); diff --git a/tests/dump.rs b/tests/dump.rs index 34421af..438a222 100644 --- a/tests/dump.rs +++ b/tests/dump.rs @@ -1,5 +1,7 @@ //! Tests for the dump and restore functionality +use std::io::Write; + use pgtemp::PgTempDB; use sqlx::postgres::PgConnection; use sqlx::prelude::*; @@ -75,3 +77,22 @@ async fn dump_and_restore() { assert_eq!(id, 10); assert_eq!(name, "example name 9"); } + +#[tokio::test] +/// make sure that we correctly error on bad database dumps. +#[should_panic(expected = "syntax error at or near \\\"INVALID\\")] +async fn panic_on_load_error() { + let temp = tempfile::tempdir().unwrap(); + let db_dump_path = temp.path().join("dump.sql"); + + // Create some bad database dumps + let mut f = std::fs::File::create(&db_dump_path).unwrap(); + f.write_all(b"INVALID SQL").unwrap(); + f.flush().expect("Failed to flush file"); + + // Try to load it (it should fail) + PgTempDB::builder() + .load_database(&db_dump_path) + .start_async() + .await; +}