pgtemp icon indicating copy to clipboard operation
pgtemp copied to clipboard

Change to fail on load errors

Open mbj opened this issue 9 months ago • 6 comments

  • Before any errors would silently be ignored.
  • And the DB would be partially initialized.
  • This created very hard to debug states.

mbj avatar Mar 27 '25 20:03 mbj

Hi! Thanks for the contribution, and for using pgtemp!

Could you add a test for this? You can copy and paste the setup from tests/dump.rs and then truncate or otherwise modify the file that's output so that it fails to load, or if you have a small sample broken load file maybe use that directly. Then I guess since we just panic on errors the easiest thing to do is to just mark the test as #[should_panic(<whatever the error message is when psql fails to load>)]

I also wonder if:

  • we could just have a generic "psql load options" that you could pass here rather than these specific options
  • could we add these options to the output of our dumps

but I doubt adding this will break any clients so that can happen later unless you feel like doing it now.

boustrophedon avatar Mar 27 '25 21:03 boustrophedon

@mbj do you want help adding a test for this?

2xic avatar Apr 09 '25 10:04 2xic

@mbj do you want help adding a test for this?

I'm to busy ATM to get back to that sorry. I do not need help, I need time ;) If you wanted to spend the time feel free to just close tis PR and take my single line change as a base.

mbj avatar Apr 09 '25 18:04 mbj

I'm to busy ATM to get back to that sorry. I do not need help, I need time ;) If you wanted to spend the time feel free to just close tis PR and take my single line change as a base.

I just wrote a test for it, feel free to add it to your PR. If you don't have time for that, I can also fork your code.

(this test fails on main, but succeeds on your branch)

#[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;
}

2xic avatar Apr 18 '25 14:04 2xic

That test looks good, can @mbj confirm that's the expected error message they see with their corrupted files? Thanks both of you for the contributions!

boustrophedon avatar Apr 19 '25 23:04 boustrophedon

That test looks good, can @mbj confirm that's the expected error message they see with their corrupted files? Thanks both of you for the contributions!

I think its good enough.

mbj avatar Apr 21 '25 22:04 mbj

Fixed in #25 , thanks everyone!

boustrophedon avatar Nov 10 '25 04:11 boustrophedon