//! 用 SQLite 的真实提交钩子拒绝提交,验证 RETURNING 后的错误没有被析构吞掉。 use super::*; use std::{ sync::{ Arc, atomic::{AtomicBool, Ordering}, }, time::Duration, }; #[test] fn single_row_reports_a_commit_failure_after_returning_the_row() { let path = std::env::temp_dir().join(format!("dever-single-commit-{}.sqlite", std::process::id())); assert!(!path.exists()); crate::task::run(true, 16, async |scope| { let database = super::super::Database::sqlite( &path, true, super::super::Options { max_page_size: 100, max_connections: 1, wait_timeout: Duration::from_secs(1), io_timeout: Duration::from_secs(2), }, None, ) .await?; let mut executor = database.acquire().await?; executor .execute( scope, Sql::same("CREATE TABLE article(id INTEGER PRIMARY KEY)"), vec![], ) .await?; let called = Arc::new(AtomicBool::new(false)); let observed = called.clone(); match &mut executor.lease { Lease::Sqlite(lease) => { lease .call(scope, move |connection, _| { connection.commit_hook(Some(move || { observed.store(true, Ordering::SeqCst); true }))?; Ok(()) }) .await? } #[cfg(feature = "postgres")] Lease::Postgres(_) => unreachable!(), }; let result = executor .query_one( scope, Sql::same("INSERT INTO article DEFAULT VALUES RETURNING id"), vec![], |row| row.get::(0), ) .await; assert!( called.load(Ordering::SeqCst), "SQLite must attempt the commit" ); assert!( result.is_err(), "uncommitted write reported success: {result:?}" ); assert_eq!(result.unwrap_err().kind, Kind::Constraint); assert_eq!( executor .query_one( scope, Sql::same("SELECT COUNT(*) FROM article"), vec![], |row| row.get::(0) ) .await?, Some(0) ); Ok::<_, Error>(()) }) .unwrap() .unwrap(); std::fs::remove_file(path).unwrap(); } #[cfg(feature = "stored-bytes")] #[test] fn deferred_constraint_removes_staged_file_after_publication() { let dir = tempfile::tempdir().unwrap(); let config: crate::upload::Config = serde_json::from_value(serde_json::json!({"directory": dir.path()})).unwrap(); crate::task::run(true, 16, async |scope| { let db = super::super::Database::sqlite(&dir.path().join("test.sqlite"), true, super::super::Options { max_page_size: 10, max_connections: 1, wait_timeout: Duration::from_secs(2), io_timeout: Duration::from_secs(2) }, None).await?; let mut e = db.acquire().await?; e.execute(scope,Sql::same("CREATE TABLE parent(id INTEGER PRIMARY KEY)"), vec![]).await?; e.execute(scope,Sql::same("CREATE TABLE child(parent_id INTEGER REFERENCES parent(id) DEFERRABLE INITIALLY DEFERRED)"), vec![]).await?; e.begin(scope).await?; e.execute(scope,Sql::same("INSERT INTO child VALUES (1)"),vec![]).await?; let key = e.files.stage(&config, Some(3), crate::Bytes::from_static(b"attachment")).unwrap(); assert_eq!(e.finish(scope, true, true).await.unwrap_err().kind, Kind::Constraint); let folder = dir.path().join("tenant/3"); assert!(folder.is_dir() && !folder.join(key.to_string()).exists()); Ok::<_, Error>(()) }).unwrap().unwrap(); }