Skip to content

Commit

Permalink
Migrate from Diesel to just MySql
Browse files Browse the repository at this point in the history
  • Loading branch information
tgross35 committed Sep 10, 2023
1 parent a153e63 commit 51af36c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 80 deletions.
2 changes: 1 addition & 1 deletion test-integration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 26 additions & 23 deletions test-integration/tests/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,57 +9,60 @@
#![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:[email protected]:12305/udf_tests";

static INIT: Once = Once::new();
static POOL: OnceLock<Pool> = OnceLock::new();

lazy_static! {
static ref SETUP_STATE: Mutex<HashSet<String>> = Mutex::new(HashSet::new());
}

/// Allow overriding the default URI
fn get_database_uri() -> String {
match env::var(URI_ENV) {
Ok(s) => s,
Err(_) => DEFAULT_DATABASE_URI.to_owned(),
}
}

/// 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
Expand Down
111 changes: 55 additions & 56 deletions test-integration/tests/test_uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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] = &[
Expand Down Expand Up @@ -61,7 +58,7 @@ const SETUP: &[&str] = &[
fn test_nil() {
let conn = &mut get_db_connection(SETUP);

let res: String = sql::<Text>("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());
Expand All @@ -71,7 +68,7 @@ fn test_nil() {
fn test_max() {
let conn = &mut get_db_connection(SETUP);

let res: String = sql::<Text>("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());
Expand All @@ -81,9 +78,7 @@ fn test_max() {
fn test_ns_dns() {
let conn = &mut get_db_connection(SETUP);

let res: String = sql::<Text>("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());
Expand All @@ -93,9 +88,7 @@ fn test_ns_dns() {
fn test_ns_url() {
let conn = &mut get_db_connection(SETUP);

let res: String = sql::<Text>("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());
Expand All @@ -105,9 +98,7 @@ fn test_ns_url() {
fn test_ns_oid() {
let conn = &mut get_db_connection(SETUP);

let res: String = sql::<Text>("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());
Expand All @@ -117,9 +108,7 @@ fn test_ns_oid() {
fn test_ns_x500() {
let conn = &mut get_db_connection(SETUP);

let res: String = sql::<Text>("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());
Expand All @@ -129,8 +118,9 @@ fn test_ns_x500() {
fn test_generate_v1() {
let conn = &mut get_db_connection(SETUP);

let res: String = sql::<Text>("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();
Expand All @@ -142,8 +132,9 @@ fn test_generate_v1() {
fn test_generate_v1mc() {
let conn = &mut get_db_connection(SETUP);

let res: String = sql::<Text>("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();
Expand All @@ -155,8 +146,9 @@ fn test_generate_v1mc() {
fn test_generate_v4() {
let conn = &mut get_db_connection(SETUP);

let res: String = sql::<Text>("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();
Expand All @@ -168,19 +160,19 @@ fn test_generate_v4() {
fn test_generate_v6() {
let conn = &mut get_db_connection(SETUP);

let res: String = sql::<Text>("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();

assert_eq!(uuid.get_version_num(), 6);

let node_id = "abcdef";
let res: String = sql::<Text>("select uuid_generate_v6(")
.bind::<Text, _>(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();
Expand All @@ -193,8 +185,9 @@ fn test_generate_v6() {
fn test_generate_v7() {
let conn = &mut get_db_connection(SETUP);

let res: String = sql::<Text>("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();
Expand All @@ -206,8 +199,9 @@ fn test_generate_v7() {
fn test_valid() {
let conn = &mut get_db_connection(SETUP);

let res: i32 = sql::<Integer>("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);
Expand All @@ -225,46 +219,51 @@ fn test_uuid_to_from_bin() {

let conn = &mut get_db_connection(SETUP);

let u2b_res: Vec<u8> = sql::<Binary>(&format!("select uuid_to_bin('{INPUT}')"))
.get_result(conn)
let u2b_res: Vec<u8> = conn
.exec_first("select uuid_to_bin(?)", (INPUT,))
.unwrap()
.unwrap();

assert_eq!(u2b_res, NORMAL);

let u2b_swp_res: Vec<u8> = sql::<Binary>(&format!("select uuid_to_bin('{INPUT}', true)"))
.get_result(conn)
let u2b_swp_res: Vec<u8> = conn
.exec_first("select uuid_to_bin(?, true)", (INPUT,))
.unwrap()
.unwrap();

assert_eq!(u2b_swp_res, SWAPPED);

let b2u_res: String = sql::<Text>(&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::<Text>(&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::<Text>(&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::<Text>(&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);
}
Expand Down

0 comments on commit 51af36c

Please sign in to comment.