use antfly_embedded::sqlx::AntflyArguments;
use antfly_embedded::sqlx::{Antfly, AntflyConnectOptions};
use futures_util::FutureExt;
use serde_json::Value;
use sqlx_core::sql_str::SqlSafeStr;
use sqlx_core::{
arguments::Arguments,
connection::ConnectOptions,
connection::Connection,
executor::Executor,
query::{query, query_with},
row::Row,
};
#[test]
fn sqlx_conformance_and_streaming() {
run_with_stack(sqlx_conformance_and_streaming_on_native_stack);
}
fn sqlx_conformance_and_streaming_on_native_stack() {
let directory = std::env::temp_dir().join(format!(
"antfly-sqlx-{}-{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
std::fs::create_dir_all(&directory).unwrap();
{
let fixture: Value = serde_json::from_str(include_str!(
"fixtures/sql/search-fixture.json"
))
.unwrap();
let database =
antfly_embedded::Database::create_default(directory.join("db.aflite")).unwrap();
database
.create_table_json(
fixture["table"].as_str().unwrap(),
serde_json::to_vec(&fixture["schema"]).unwrap(),
)
.unwrap();
let table = database
.open_table(fixture["table"].as_str().unwrap())
.unwrap();
database
.create_table_json(
"history_items",
serde_json::to_vec(&fixture["history"]).unwrap(),
)
.unwrap();
for index in fixture["indexes"].as_array().unwrap() {
table
.add_index_json(serde_json::to_vec(index).unwrap())
.unwrap();
}
table
.batch_json(serde_json::to_vec(&fixture["batch"]).unwrap())
.unwrap();
table.run_until_idle().unwrap();
}
tokio::runtime::Builder::new_current_thread()
.build()
.unwrap()
.block_on(async {
let options = AntflyConnectOptions::new(directory.join("db.aflite")).no_sync(true);
let mut connection = options.connect().await.unwrap();
let cases: Value = serde_json::from_str(include_str!(
"fixtures/sql/cases.json"
))
.unwrap();
for case in cases.as_array().unwrap() {
let statement = case["statement"].as_str().unwrap().to_owned();
let mut args = AntflyArguments::default();
if let Some(parameters) = case["parameters"].as_array() {
for value in parameters {
args.add(value.clone()).unwrap()
}
}
let rows =
query_with::<Antfly, _>(sqlx_core::sql_str::AssertSqlSafe(statement), args)
.fetch_all(&mut connection)
.await;
if let Some(state) = case["sqlstate"].as_str() {
assert_eq!(
rows.unwrap_err()
.as_database_error()
.unwrap()
.code()
.as_deref(),
Some(state)
);
continue;
}
let rows = rows.unwrap();
if let Some(expected) = case["rows"].as_array() {
let actual: Vec<Vec<Value>> = rows
.iter()
.map(|row| {
row.columns()
.iter()
.enumerate()
.map(|(i, col)| {
use sqlx_core::column::Column;
match col.type_info().0.as_str() {
"integer" => row
.try_get::<Option<i64>, _>(i)
.unwrap()
.map(|v| Value::String(v.to_string()))
.unwrap_or(Value::Null),
"boolean" => row
.try_get::<Option<bool>, _>(i)
.unwrap()
.map(Value::Bool)
.unwrap_or(Value::Null),
"number" => row
.try_get::<Option<f64>, _>(i)
.unwrap()
.map(|v| serde_json::json!(v))
.unwrap_or(Value::Null),
"json" => row
.try_get::<Option<Value>, _>(i)
.unwrap()
.unwrap_or(Value::Null),
_ => row
.try_get::<Option<String>, _>(i)
.unwrap()
.map(Value::String)
.unwrap_or(Value::Null),
}
})
.collect()
})
.collect();
assert_eq!(
serde_json::to_value(actual).unwrap(),
Value::Array(expected.clone())
);
}
}
connection
.execute("CREATE TABLE numbers (n BIGINT)".into_sql_str())
.await
.unwrap();
let mut transaction = connection.begin().await.unwrap();
for i in 0..300i64 {
query::<Antfly>("INSERT INTO numbers (n) VALUES ($1)".into_sql_str())
.bind(i)
.execute(&mut *transaction)
.await
.unwrap();
}
let mut other = options.connect().await.unwrap();
assert!(
query::<Antfly>("SELECT n FROM numbers".into_sql_str())
.fetch_all(&mut other)
.await
.unwrap()
.is_empty()
);
transaction.commit().await.unwrap();
assert_eq!(
query::<Antfly>("SELECT n FROM numbers".into_sql_str())
.fetch_all(&mut other)
.await
.unwrap()
.len(),
300
);
let described = connection
.describe("SELECT n FROM numbers WHERE n=$1".into_sql_str())
.await
.unwrap();
assert_eq!(described.columns().len(), 1);
for _ in 0..100 {
let _ = query::<Antfly>("SELECT n FROM numbers".into_sql_str())
.fetch_all(&mut connection)
.now_or_never();
}
assert_eq!(
query::<Antfly>("SELECT n FROM numbers".into_sql_str())
.fetch_all(&mut connection)
.await
.unwrap()
.len(),
300
);
{
let mut outer = connection.begin().await.unwrap();
{
let mut inner = outer.begin().await.unwrap();
inner
.execute("INSERT INTO numbers (n) VALUES (9000)".into_sql_str())
.await
.unwrap();
}
assert_eq!(
query::<Antfly>("SELECT n FROM numbers".into_sql_str())
.fetch_all(&mut *outer)
.await
.unwrap()
.len(),
300
);
outer.commit().await.unwrap();
}
connection.close().await.unwrap();
other.close().await.unwrap();
let mut reopened = options.connect().await.unwrap();
assert_eq!(
query::<Antfly>("SELECT n FROM numbers".into_sql_str())
.fetch_all(&mut reopened)
.await
.unwrap()
.len(),
300
);
reopened.close().await.unwrap();
});
std::fs::remove_dir_all(directory).unwrap();
}
#[test]
fn sqlx_pool_uses_independent_native_connections() {
run_with_stack(sqlx_pool_uses_independent_native_connections_on_native_stack);
}
fn sqlx_pool_uses_independent_native_connections_on_native_stack() {
let directory = std::env::temp_dir().join(format!(
"antfly-sqlx-pool-{}-{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
std::fs::create_dir_all(&directory).unwrap();
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(async {
let pool = sqlx_core::pool::PoolOptions::<Antfly>::new()
.min_connections(2)
.max_connections(2)
.connect_with(AntflyConnectOptions::new(directory.join("db.aflite")))
.await
.unwrap();
let mut first = pool.acquire().await.unwrap();
let mut second = pool.acquire().await.unwrap();
first
.execute("CREATE TABLE items (id BIGINT PRIMARY KEY, name TEXT)".into_sql_str())
.await
.unwrap();
let mut transaction = first.begin().await.unwrap();
transaction
.execute("INSERT INTO items (id,name) VALUES (1, 'pending')".into_sql_str())
.await
.unwrap();
assert!(
query::<Antfly>("SELECT id FROM items".into_sql_str())
.fetch_all(&mut *second)
.await
.unwrap()
.is_empty()
);
second
.execute("INSERT INTO items (id,name) VALUES (2, 'other')".into_sql_str())
.await
.unwrap();
transaction.commit().await.unwrap();
assert_eq!(
query::<Antfly>("SELECT id FROM items".into_sql_str())
.fetch_all(&mut *second)
.await
.unwrap()
.len(),
2
);
first.close().await.unwrap();
second
.execute("INSERT INTO items (id,name) VALUES (3, 'after close')".into_sql_str())
.await
.unwrap();
drop(second);
pool.close().await;
});
std::fs::remove_dir_all(directory).unwrap();
}
#[test]
fn sqlx_shares_an_open_database_with_the_document_api() {
run_with_stack(sqlx_shares_an_open_database_with_the_document_api_on_native_stack);
}
fn sqlx_shares_an_open_database_with_the_document_api_on_native_stack() {
let directory = std::env::temp_dir().join(format!(
"antfly-sqlx-shared-{}-{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
std::fs::create_dir_all(&directory).unwrap();
let path = directory.join("db.aflite");
std::thread::Builder::new()
.stack_size(antfly_embedded::MIN_THREAD_STACK_SIZE)
.spawn(move || {
let database = std::sync::Arc::new(
antfly_embedded::Database::create(
&path,
&antfly_embedded::OpenOptions::new().no_sync(true),
)
.unwrap(),
);
database
.batch_json(r#"{"inserts":{"doc":{"text":"document api"}}}"#)
.unwrap();
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
runtime.block_on(async {
let independent = AntflyConnectOptions::new(&path)
.no_sync(true)
.connect()
.await
.unwrap();
independent.close().await.unwrap();
let options = AntflyConnectOptions::new(&path)
.with_database(std::sync::Arc::clone(&database));
let pool = sqlx_core::pool::PoolOptions::<Antfly>::new()
.max_connections(2)
.connect_with(options)
.await
.unwrap();
pool.execute(
"CREATE TABLE threads (id TEXT PRIMARY KEY, title TEXT)".into_sql_str(),
)
.await
.unwrap();
let mut transaction = pool.begin().await.unwrap();
transaction
.execute(
"INSERT INTO threads (id, title) VALUES ('t1', 'first')".into_sql_str(),
)
.await
.unwrap();
transaction.commit().await.unwrap();
let table = database.open_table("threads").unwrap();
let scanned = String::from_utf8(table.scan_json("{}").unwrap()).unwrap();
assert!(scanned.contains("\"hashes\""), "scan: {scanned}");
drop(table);
let rows = pool
.fetch_all(query::<Antfly>("SELECT title FROM threads"))
.await
.unwrap();
assert_eq!(rows.len(), 1);
assert_eq!(rows[0].get::<String, _>("title"), "first");
assert!(database.lookup_json("doc").is_ok());
pool.close().await;
});
database
.batch_json(r#"{"inserts":{"after":{"text":"still open"}}}"#)
.unwrap();
assert!(database.lookup_json("after").is_ok());
database.close().unwrap();
})
.unwrap()
.join()
.unwrap();
}
#[test]
fn sqlx_external_commit_and_document_read_close_inference_promptly() {
run_with_stack(sqlx_external_commit_and_document_read_close_inference_promptly_on_native_stack);
}
fn sqlx_external_commit_and_document_read_close_inference_promptly_on_native_stack() {
fn on_executor<T: Send + 'static>(f: impl FnOnce() -> T + Send + 'static) -> T {
std::thread::Builder::new()
.stack_size(antfly_embedded::MIN_THREAD_STACK_SIZE)
.spawn(f)
.unwrap()
.join()
.unwrap()
}
let directory = std::env::temp_dir().join(format!(
"antfly-sqlx-inference-close-{}-{}",
std::process::id(),
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos()
));
std::fs::create_dir_all(&directory).unwrap();
let path = directory.join("db.aflite");
let document_path = path.clone();
let database = on_executor(move || {
let database = std::sync::Arc::new(
antfly_embedded::Database::create(
document_path,
&antfly_embedded::OpenOptions::new()
.no_sync(true)
.local_runtime_configured(true)
.busy_timeout(std::time::Duration::from_secs(5)),
)
.unwrap(),
);
database
.batch_json(r#"{"inserts":{"doc":{"text":"document api"}}}"#)
.unwrap();
database
});
let runtime = tokio::runtime::Builder::new_multi_thread()
.worker_threads(2)
.thread_stack_size(antfly_embedded::MIN_THREAD_STACK_SIZE)
.enable_all()
.build()
.unwrap();
runtime.block_on(async {
let pool = sqlx_core::pool::PoolOptions::<Antfly>::new()
.max_connections(4)
.connect_with(AntflyConnectOptions::new(&path).no_sync(true))
.await
.unwrap();
pool.execute("CREATE TABLE codex_t (id TEXT PRIMARY KEY)".into_sql_str())
.await
.unwrap();
pool.execute("INSERT INTO codex_t (id) VALUES ('a')".into_sql_str())
.await
.unwrap();
let reader = std::sync::Arc::clone(&database);
on_executor(move || assert!(reader.lookup_json("doc").is_ok()));
pool.close().await;
});
on_executor(move || {
let start = std::time::Instant::now();
database.close().unwrap();
let elapsed = start.elapsed();
eprintln!("inference shutdown took {elapsed:?}");
assert!(
elapsed < std::time::Duration::from_secs(2),
"inference shutdown took {elapsed:?}"
);
});
drop(runtime);
std::fs::remove_dir_all(directory).unwrap();
}
fn run_with_stack<F: FnOnce() + Send + 'static>(task: F) {
std::thread::Builder::new()
.stack_size(antfly_embedded::MIN_THREAD_STACK_SIZE)
.spawn(task)
.expect("spawn native test thread")
.join()
.unwrap_or_else(|payload| std::panic::resume_unwind(payload));
}
#[test]
fn sqlx_errors_identify_statement_and_nul_parameter() {
run_with_stack(|| {
let directory =
std::env::temp_dir().join(format!("antfly-sqlx-nul-{}", std::process::id()));
std::fs::create_dir_all(&directory).unwrap();
let path = directory.join("db.aflite");
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
runtime.block_on(async {
let mut conn = AntflyConnectOptions::new(&path).no_sync(true).connect().await.unwrap();
conn.execute("CREATE TABLE items (id BIGINT PRIMARY KEY, item JSONB)".into_sql_str()).await.unwrap();
let statement = "INSERT INTO items (id,item) VALUES ($1,$2) ON CONFLICT (id) DO UPDATE SET item=excluded.item";
let error = query::<Antfly>(statement.into_sql_str()).bind(1_i64).bind(serde_json::json!({"output":"bad\0output"})).execute(&mut conn).await.unwrap_err();
let error = error.as_database_error().unwrap();
assert_eq!(error.code().as_deref(), Some("22P05"));
assert!(error.message().contains("parameter $2"));
assert!(error.message().contains(statement));
let native = error.downcast_ref::<antfly_embedded::sql::SqlError>();
let diagnostic: Value = serde_json::from_slice(&native.body).unwrap();
assert_eq!(diagnostic["error"]["parameter_index"], 2);
assert_eq!(diagnostic["error"]["statement"], statement);
assert!(!error.message().contains("bad\0output"));
let statement = "SELECT 1 / $1";
let error = query::<Antfly>(statement.into_sql_str()).bind(0_i64).fetch_all(&mut conn).await.unwrap_err();
assert_eq!(error.as_database_error().unwrap().code().as_deref(), Some("22012"));
assert!(error.to_string().contains(statement));
let long_statement = format!("SELECT * FROM missing_table /*{}*/", "é".repeat(300));
let error = conn.execute(sqlx_core::sql_str::AssertSqlSafe(long_statement.clone()).into_sql_str()).await.unwrap_err();
let error = error.as_database_error().unwrap().downcast_ref::<antfly_embedded::sql::SqlError>();
let diagnostic: Value = serde_json::from_slice(&error.body).unwrap();
let excerpt = diagnostic["error"]["statement"].as_str().unwrap();
assert!(excerpt.ends_with('…'));
assert!(excerpt.len() <= 515);
assert!(long_statement.starts_with(excerpt.trim_end_matches('…')));
conn.close().await.unwrap();
});
let database = antfly_embedded::Database::open_default(&path).unwrap();
let table = database.open_table("items").unwrap();
let error = table.sql_json(br#"{"statement":"SELECT 1"}"#).unwrap_err();
assert!(error.sqlstate.is_none());
assert!(error.body.is_empty());
assert!(
error
.message
.contains("SQL sessions require a root database handle")
);
assert!(
error
.to_string()
.contains("SQL sessions require a root database handle")
);
assert!(error.to_string().contains("[statement: SELECT 1]"));
drop(table);
drop(database);
std::fs::remove_dir_all(directory).unwrap();
});
}