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; +}