// PostgreSQL 只读取显式测试连接;每个比较进程创建并删除独占 schema。 struct PostgresSchema { database: dever_runtime::database::Database, drop_sql: &'static str, } impl PostgresSchema { fn close(self, runtime: &Runtime) { runtime .run(async |scope| { self.database .executor() .await .unwrap() .initialize(scope, self.drop_sql) .await .unwrap(); }) .unwrap(); self.database.close(); } } fn database_settings( runtime: &Runtime, directory: &std::path::Path, settings: &std::path::Path, relative: &str, ) -> Option { if !POSTGRES { std::fs::write(settings, format!(r#"{{"database":{{"default":{{"type":"sqlite","path":"{relative}/case.db","max_connections":1,"max_page_size":20}}}}}}"#)).unwrap(); return None; } let test_settings = std::env::var_os("DEVER_BENCH_POSTGRES_SETTINGS") .expect("PostgreSQL benchmarks require an explicit isolated test configuration"); let config: serde_json::Value = serde_json::from_slice(&std::fs::read(test_settings).unwrap()).unwrap(); let mut connection = config["database"]["postgres_test"].clone(); let options: dever_runtime::database::Options = serde_json::from_value(connection.clone()).unwrap(); assert_eq!(options.driver, "postgres"); let name = format!("dever_bench_{}", std::process::id()); let create_sql: &'static str = Box::leak(format!("CREATE SCHEMA {name}").into_boxed_str()); let drop_sql = Box::leak(format!("DROP SCHEMA {name} CASCADE").into_boxed_str()); let database = runtime .run(async |scope| { let database = dever_runtime::database::Database::open(directory, options, scope) .await .unwrap(); database .executor() .await .unwrap() .initialize(scope, create_sql) .await .unwrap(); database }) .unwrap(); let url = connection["url"].as_str().unwrap(); assert!( !url.contains('?'), "test connection must not override schema options" ); connection["url"] = format!("{url}?options=-csearch_path%3D{name}").into(); connection["max_connections"] = 1.into(); connection["max_page_size"] = 20.into(); std::fs::write( settings, serde_json::to_vec(&serde_json::json!({"database":{"default":connection}})).unwrap(), ) .unwrap(); Some(PostgresSchema { database, drop_sql }) }