From 51af36c0304e86bb146b08c97fd4c0316b7b8382 Mon Sep 17 00:00:00 2001 From: Trevor Gross Date: Sun, 10 Sep 2023 06:44:10 -0400 Subject: [PATCH] Migrate from Diesel to just MySql --- test-integration/Cargo.toml | 2 +- test-integration/tests/backend/mod.rs | 49 ++++++------ test-integration/tests/test_uuid.rs | 111 +++++++++++++------------- 3 files changed, 82 insertions(+), 80 deletions(-) diff --git a/test-integration/Cargo.toml b/test-integration/Cargo.toml index 9b2c658..d82d1ae 100644 --- a/test-integration/Cargo.toml +++ b/test-integration/Cargo.toml @@ -9,11 +9,11 @@ license = "Apache-2.0 OR GPL-2.0-or-later" [dev-dependencies] regex = "1.9.5" -diesel = { version = "2.1.1", features = ["mysql"] } lazy_static = "1.4.0" uuid = { version = "1.4.1", features = ["v1", "v3", "v4", "v5", "fast-rng"] } hex-literal = "0.4.1" hex = "0.4.3" +mysql = "24.0.0" [features] # Used to optionally enable integration tests diff --git a/test-integration/tests/backend/mod.rs b/test-integration/tests/backend/mod.rs index 55d1702..ce5c6ad 100644 --- a/test-integration/tests/backend/mod.rs +++ b/test-integration/tests/backend/mod.rs @@ -9,24 +9,22 @@ #![cfg(feature = "backend")] use std::collections::HashSet; use std::env; -use std::sync::{Mutex, Once}; +use std::sync::{Mutex, OnceLock}; -use diesel::dsl::sql; -use diesel::mysql::MysqlConnection; -use diesel::prelude::*; -use diesel::sql_types::Untyped; -use diesel::Connection; use lazy_static::lazy_static; +use mysql::prelude::*; +use mysql::{Pool, PooledConn}; const URI_ENV: &str = "UDF_TEST_BACKEND_URI"; const DEFAULT_DATABASE_URI: &str = "mysql://root:example@0.0.0.0:12305/udf_tests"; -static INIT: Once = Once::new(); +static POOL: OnceLock = OnceLock::new(); lazy_static! { static ref SETUP_STATE: Mutex> = Mutex::new(HashSet::new()); } +/// Allow overriding the default URI fn get_database_uri() -> String { match env::var(URI_ENV) { Ok(s) => s, @@ -34,32 +32,37 @@ fn get_database_uri() -> String { } } -/// Ensures that init items have been run -pub fn get_db_connection(init: &[&str]) -> MysqlConnection { +fn build_pool() -> Pool { let db_url = get_database_uri(); + let (url, db) = db_url.rsplit_once('/').unwrap(); + let pool = Pool::new(url).expect("pool failed"); + let mut conn = pool.get_conn().expect("initial connection failed"); - INIT.call_once(|| { - let mut conn = MysqlConnection::establish(db_url.rsplit_once('/').unwrap().0) - .expect("initial connection failed"); + // Create default database + conn.query_drop(format!("CREATE OR REPLACE DATABASE {db}")) + .unwrap(); - sql::<(Untyped,)>("create or replace database udf_tests") - .execute(&mut conn) - .expect("could not create databases"); - }); + pool +} - let hset = &mut *SETUP_STATE.lock().unwrap(); - let mut conn = MysqlConnection::establish(&db_url).expect("initial connection failed"); +/// Ensures that init items have been run +pub fn get_db_connection(init: &[&str]) -> PooledConn { + let mut conn = POOL + .get_or_init(build_pool) + .get_conn() + .expect("failed to get conn"); + + let ran_stmts = &mut *SETUP_STATE.lock().unwrap(); // Store a list of our init calls so we don't repeat them for stmt in init { - if hset.contains(*stmt) { + if ran_stmts.contains(*stmt) { continue; } - sql::<(Untyped,)>(stmt) - .execute(&mut conn) - .expect("could not run setup"); - hset.insert((*stmt).to_owned()); + conn.query_drop(stmt).expect("could not run setup"); + + ran_stmts.insert((*stmt).to_owned()); } conn diff --git a/test-integration/tests/test_uuid.rs b/test-integration/tests/test_uuid.rs index c17d766..d663f15 100644 --- a/test-integration/tests/test_uuid.rs +++ b/test-integration/tests/test_uuid.rs @@ -2,11 +2,8 @@ mod backend; -// use backend::get_db_connection; use backend::get_db_connection; -use diesel::dsl::sql; -use diesel::prelude::*; -use diesel::sql_types::{Binary, Integer, Text}; +use mysql::prelude::*; use uuid::{Bytes as UuidBytes, Uuid}; const SETUP: &[&str] = &[ @@ -61,7 +58,7 @@ const SETUP: &[&str] = &[ fn test_nil() { let conn = &mut get_db_connection(SETUP); - let res: String = sql::("select uuid_nil()").get_result(conn).unwrap(); + let res: String = conn.query_first("select uuid_nil()").unwrap().unwrap(); assert_eq!(res, "00000000-0000-0000-0000-000000000000"); assert_eq!(res, Uuid::nil().hyphenated().to_string()); @@ -71,7 +68,7 @@ fn test_nil() { fn test_max() { let conn = &mut get_db_connection(SETUP); - let res: String = sql::("select uuid_max()").get_result(conn).unwrap(); + let res: String = conn.query_first("select uuid_max()").unwrap().unwrap(); assert_eq!(res, "ffffffff-ffff-ffff-ffff-ffffffffffff"); assert_eq!(res, Uuid::max().hyphenated().to_string()); @@ -81,9 +78,7 @@ fn test_max() { fn test_ns_dns() { let conn = &mut get_db_connection(SETUP); - let res: String = sql::("select uuid_ns_dns()") - .get_result(conn) - .unwrap(); + let res: String = conn.query_first("select uuid_ns_dns()").unwrap().unwrap(); assert_eq!(res, "6ba7b810-9dad-11d1-80b4-00c04fd430c8"); assert_eq!(res, Uuid::NAMESPACE_DNS.hyphenated().to_string()); @@ -93,9 +88,7 @@ fn test_ns_dns() { fn test_ns_url() { let conn = &mut get_db_connection(SETUP); - let res: String = sql::("select uuid_ns_url()") - .get_result(conn) - .unwrap(); + let res: String = conn.query_first("select uuid_ns_url()").unwrap().unwrap(); assert_eq!(res, "6ba7b811-9dad-11d1-80b4-00c04fd430c8"); assert_eq!(res, Uuid::NAMESPACE_URL.hyphenated().to_string()); @@ -105,9 +98,7 @@ fn test_ns_url() { fn test_ns_oid() { let conn = &mut get_db_connection(SETUP); - let res: String = sql::("select uuid_ns_oid()") - .get_result(conn) - .unwrap(); + let res: String = conn.query_first("select uuid_ns_oid()").unwrap().unwrap(); assert_eq!(res, "6ba7b812-9dad-11d1-80b4-00c04fd430c8"); assert_eq!(res, Uuid::NAMESPACE_OID.hyphenated().to_string()); @@ -117,9 +108,7 @@ fn test_ns_oid() { fn test_ns_x500() { let conn = &mut get_db_connection(SETUP); - let res: String = sql::("select uuid_ns_x500()") - .get_result(conn) - .unwrap(); + let res: String = conn.query_first("select uuid_ns_x500()").unwrap().unwrap(); assert_eq!(res, "6ba7b814-9dad-11d1-80b4-00c04fd430c8"); assert_eq!(res, Uuid::NAMESPACE_X500.hyphenated().to_string()); @@ -129,8 +118,9 @@ fn test_ns_x500() { fn test_generate_v1() { let conn = &mut get_db_connection(SETUP); - let res: String = sql::("select uuid_generate_v1()") - .get_result(conn) + let res: String = conn + .query_first("select uuid_generate_v1()") + .unwrap() .unwrap(); let uuid = Uuid::try_parse(&res).unwrap(); @@ -142,8 +132,9 @@ fn test_generate_v1() { fn test_generate_v1mc() { let conn = &mut get_db_connection(SETUP); - let res: String = sql::("select uuid_generate_v1mc()") - .get_result(conn) + let res: String = conn + .query_first("select uuid_generate_v1mc()") + .unwrap() .unwrap(); let uuid = Uuid::try_parse(&res).unwrap(); @@ -155,8 +146,9 @@ fn test_generate_v1mc() { fn test_generate_v4() { let conn = &mut get_db_connection(SETUP); - let res: String = sql::("select uuid_generate_v4()") - .get_result(conn) + let res: String = conn + .query_first("select uuid_generate_v4()") + .unwrap() .unwrap(); let uuid = Uuid::try_parse(&res).unwrap(); @@ -168,8 +160,9 @@ fn test_generate_v4() { fn test_generate_v6() { let conn = &mut get_db_connection(SETUP); - let res: String = sql::("select uuid_generate_v6()") - .get_result(conn) + let res: String = conn + .query_first("select uuid_generate_v6()") + .unwrap() .unwrap(); let uuid = Uuid::try_parse(&res).unwrap(); @@ -177,10 +170,9 @@ fn test_generate_v6() { assert_eq!(uuid.get_version_num(), 6); let node_id = "abcdef"; - let res: String = sql::("select uuid_generate_v6(") - .bind::(node_id) - .sql(")") - .get_result(conn) + let res: String = conn + .exec_first("select uuid_generate_v6(?)", (node_id,)) + .unwrap() .unwrap(); let uuid = Uuid::try_parse(res.as_str()).unwrap(); @@ -193,8 +185,9 @@ fn test_generate_v6() { fn test_generate_v7() { let conn = &mut get_db_connection(SETUP); - let res: String = sql::("select uuid_generate_v7()") - .get_result(conn) + let res: String = conn + .query_first("select uuid_generate_v7()") + .unwrap() .unwrap(); let uuid = Uuid::try_parse(&res).unwrap(); @@ -206,8 +199,9 @@ fn test_generate_v7() { fn test_valid() { let conn = &mut get_db_connection(SETUP); - let res: i32 = sql::("select uuid_is_valid(uuid_generate_v4())") - .get_result(conn) + let res: i32 = conn + .query_first("select uuid_is_valid(uuid_generate_v4())") + .unwrap() .unwrap(); assert_eq!(res, 1); @@ -225,46 +219,51 @@ fn test_uuid_to_from_bin() { let conn = &mut get_db_connection(SETUP); - let u2b_res: Vec = sql::(&format!("select uuid_to_bin('{INPUT}')")) - .get_result(conn) + let u2b_res: Vec = conn + .exec_first("select uuid_to_bin(?)", (INPUT,)) + .unwrap() .unwrap(); assert_eq!(u2b_res, NORMAL); - let u2b_swp_res: Vec = sql::(&format!("select uuid_to_bin('{INPUT}', true)")) - .get_result(conn) + let u2b_swp_res: Vec = conn + .exec_first("select uuid_to_bin(?, true)", (INPUT,)) + .unwrap() .unwrap(); assert_eq!(u2b_swp_res, SWAPPED); - let b2u_res: String = sql::(&format!( - "select {from_fn}(unhex('{}'))", - hex::encode(NORMAL) - )) - .get_result(conn) - .unwrap(); + let b2u_res: String = conn + .exec_first( + &format!("select {from_fn}(unhex(?))"), + (hex::encode(NORMAL),), + ) + .unwrap() + .unwrap(); assert_eq!(b2u_res, INPUT); - let b2u_swp_res: String = sql::(&format!( - "select {from_fn}(unhex('{}'), true)", - hex::encode(SWAPPED) - )) - .get_result(conn) - .unwrap(); + let b2u_swp_res: String = conn + .exec_first( + &format!("select {from_fn}(unhex(?), true)"), + (hex::encode(SWAPPED),), + ) + .unwrap() + .unwrap(); assert_eq!(b2u_swp_res, INPUT); - let roundtrip: String = sql::(&format!("select {from_fn}(uuid_to_bin('{INPUT}'))")) - .get_result(conn) + let roundtrip: String = conn + .exec_first(&format!("select {from_fn}(uuid_to_bin(?))"), (INPUT,)) + .unwrap() .unwrap(); assert_eq!(roundtrip, INPUT); - let roundtrip_swp: String = - sql::(&format!("select {from_fn}(uuid_to_bin('{INPUT}', 1), 1)")) - .get_result(conn) - .unwrap(); + let roundtrip_swp: String = conn + .exec_first(&format!("select {from_fn}(uuid_to_bin(?, 1), 1)"), (INPUT,)) + .unwrap() + .unwrap(); assert_eq!(roundtrip_swp, INPUT); }