feat(grid): add conditional bulk updates for MySQL
This commit is contained in:
29 files changed
+1813
-85
No files matched your search
@@ -275,6 +275,19 @@ pub struct DataGridCountSqlOptions {
|
||||
pub count_hint: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct DataGridConditionalUpdateSqlOptions {
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub database_type: Option<DatabaseType>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub identifier_quote: Option<String>,
|
||||
pub table_meta: DataGridTableMeta,
|
||||
pub column_name: String,
|
||||
pub value: Value,
|
||||
pub where_input: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct HiveTablePropertiesSqlOptions {
|
||||
@@ -891,6 +904,50 @@ pub fn build_data_grid_count_sql(options: DataGridCountSqlOptions) -> String {
|
||||
format!("SELECT{hint_part} COUNT(*) AS cnt FROM {table}{where_clause}")
|
||||
}
|
||||
|
||||
pub fn build_data_grid_conditional_update_sql(options: DataGridConditionalUpdateSqlOptions) -> Option<String> {
|
||||
if options.database_type != Some(DatabaseType::Mysql) {
|
||||
return None;
|
||||
}
|
||||
let predicate = crate::sql_dialect::normalize_where_input(Some(&options.where_input));
|
||||
if predicate.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let columns = options.table_meta.columns.as_deref()?;
|
||||
let column_info = column_info_for(columns, &options.column_name)?;
|
||||
let primary_key_set: Vec<String> =
|
||||
options.table_meta.primary_keys.iter().map(|primary_key| normalize_column_name(primary_key)).collect();
|
||||
if primary_key_set.contains(&normalize_column_name(&column_info.name))
|
||||
|| column_info.is_primary_key
|
||||
|| is_grid_update_omitted_column(
|
||||
options.database_type,
|
||||
Some(column_info),
|
||||
Some(&column_info.name),
|
||||
&primary_key_set,
|
||||
)
|
||||
{
|
||||
return None;
|
||||
}
|
||||
|
||||
let table = data_grid_qualified_table_name(
|
||||
options.database_type,
|
||||
options.table_meta.catalog.as_deref(),
|
||||
options.table_meta.schema.as_deref(),
|
||||
options.table_meta.database.as_deref(),
|
||||
&options.table_meta.table_name,
|
||||
options.identifier_quote.as_deref(),
|
||||
);
|
||||
let sets = format!(
|
||||
"{} = {}",
|
||||
data_grid_identifier(options.database_type, &column_info.name, options.identifier_quote.as_deref()),
|
||||
format_grid_save_sql_literal(&options.value, options.database_type, Some(column_info))
|
||||
);
|
||||
Some(data_grid_statement(
|
||||
options.database_type,
|
||||
data_grid_update_sql(options.database_type, &table, &sets, &format!("({predicate})")),
|
||||
))
|
||||
}
|
||||
|
||||
pub fn build_hive_table_properties_sql(options: HiveTablePropertiesSqlOptions) -> String {
|
||||
let table = qualified_table_name(Some(DatabaseType::Hive), options.schema.as_deref(), &options.table_name);
|
||||
let property = options.property_name.replace('\'', "''");
|
||||
@@ -3229,6 +3286,45 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mysql_conditional_update_uses_typed_value_and_requires_a_writable_column_and_where() {
|
||||
let options = DataGridConditionalUpdateSqlOptions {
|
||||
database_type: Some(DatabaseType::Mysql),
|
||||
identifier_quote: None,
|
||||
table_meta: DataGridTableMeta {
|
||||
catalog: None,
|
||||
database: None,
|
||||
schema: Some("app".to_string()),
|
||||
table_name: "people".to_string(),
|
||||
primary_keys: vec!["id".to_string()],
|
||||
columns: Some(vec![column("id", "bigint", false, None), column("status", "varchar(32)", true, None)]),
|
||||
},
|
||||
column_name: "status".to_string(),
|
||||
value: json!("O'Reilly"),
|
||||
where_input: "WHERE tenant_id = 7;".to_string(),
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
build_data_grid_conditional_update_sql(options.clone()),
|
||||
Some("UPDATE `app`.`people` SET `status` = 'O''Reilly' WHERE (tenant_id = 7);".to_string())
|
||||
);
|
||||
|
||||
let mut condition_only = options.clone();
|
||||
condition_only.where_input = "tenant_id = 7".to_string();
|
||||
assert_eq!(
|
||||
build_data_grid_conditional_update_sql(condition_only),
|
||||
Some("UPDATE `app`.`people` SET `status` = 'O''Reilly' WHERE (tenant_id = 7);".to_string())
|
||||
);
|
||||
|
||||
let mut empty_where = options.clone();
|
||||
empty_where.where_input = " ".to_string();
|
||||
assert_eq!(build_data_grid_conditional_update_sql(empty_where), None);
|
||||
|
||||
let mut primary_key = options;
|
||||
primary_key.column_name = "id".to_string();
|
||||
assert_eq!(build_data_grid_conditional_update_sql(primary_key), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn iris_cache_data_grid_save_uses_unquoted_ordinary_identifiers() {
|
||||
let result = prepare_data_grid_save(DataGridSaveStatementOptions {
|
||||
|
||||
@@ -943,6 +943,9 @@ pub struct QueryExecutionOptions {
|
||||
/// Query timeout in seconds. `None` uses the default (30s).
|
||||
/// `Some(0)` disables the timeout entirely.
|
||||
pub timeout_secs: Option<u64>,
|
||||
/// Keep awaiting the database response after cancellation so callers can
|
||||
/// distinguish an interrupt request from a confirmed terminal state.
|
||||
pub await_cancel_completion: bool,
|
||||
pub execution_id: Option<String>,
|
||||
/// When `Some(true)`, multiple statements are executed within a single transaction
|
||||
/// (BEGIN … COMMIT) instead of auto-commit mode. `None` and `Some(false)` behave
|
||||
@@ -1558,7 +1561,7 @@ async fn sqlserver_pool_is_current(
|
||||
matches!(connections.get(pool_key), Some(PoolKind::SqlServer(current)) if Arc::ptr_eq(current, client))
|
||||
}
|
||||
|
||||
fn resolve_query_timeout(timeout_secs: Option<u64>) -> Option<Duration> {
|
||||
pub fn query_timeout_duration(timeout_secs: Option<u64>) -> Option<Duration> {
|
||||
match timeout_secs {
|
||||
Some(0) => None,
|
||||
Some(n) => Some(Duration::from_secs(n)),
|
||||
@@ -1566,6 +1569,10 @@ fn resolve_query_timeout(timeout_secs: Option<u64>) -> Option<Duration> {
|
||||
}
|
||||
}
|
||||
|
||||
fn resolve_query_timeout(timeout_secs: Option<u64>) -> Option<Duration> {
|
||||
query_timeout_duration(timeout_secs)
|
||||
}
|
||||
|
||||
fn query_pool_database<'a>(database: &'a str, catalog: Option<&str>) -> Option<&'a str> {
|
||||
if database.is_empty() || catalog.is_some() {
|
||||
None
|
||||
@@ -1735,8 +1742,9 @@ async fn do_execute_typed(
|
||||
),
|
||||
)
|
||||
.await?;
|
||||
let execution_cancel_token = if options.await_cancel_completion { None } else { cancel_token };
|
||||
wait_for_result_opt(
|
||||
cancel_token,
|
||||
execution_cancel_token,
|
||||
query_timeout,
|
||||
db::mysql::execute_query_on_conn_with_limits(
|
||||
&mut conn,
|
||||
|
||||
@@ -2,6 +2,7 @@ use std::collections::HashMap;
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Duration;
|
||||
use tokio::sync::watch;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
/// How long a [`RegisteredQuery::detach`]ed registration stays reachable for
|
||||
@@ -65,11 +66,20 @@ impl RunningTaskMetadata {
|
||||
struct RunningTask {
|
||||
registration_id: u64,
|
||||
token: CancellationToken,
|
||||
completion: watch::Sender<bool>,
|
||||
remember_completion: bool,
|
||||
metadata: RunningTaskMetadata,
|
||||
pool_key: Option<String>,
|
||||
interrupt: Option<InterruptFn>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, serde::Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct CancellationWaitResult {
|
||||
pub requested: bool,
|
||||
pub terminal: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct RunningQueries {
|
||||
// Interrupt closures live inside `RunningTask` rather than a second map
|
||||
@@ -80,6 +90,7 @@ pub struct RunningQueries {
|
||||
// before its interrupt handle was ready — inserted a closure keyed to
|
||||
// an execution id nothing would ever look at again, leaking it forever.
|
||||
inner: Arc<Mutex<HashMap<String, RunningTask>>>,
|
||||
completed: Arc<Mutex<HashMap<String, std::time::Instant>>>,
|
||||
next_registration_id: Arc<AtomicU64>,
|
||||
}
|
||||
|
||||
@@ -89,15 +100,50 @@ impl RunningQueries {
|
||||
}
|
||||
|
||||
pub fn register_task(&self, execution_id: String, metadata: RunningTaskMetadata) -> RegisteredQuery {
|
||||
self.register_task_with_completion_tracking(execution_id, metadata, false)
|
||||
}
|
||||
|
||||
/// Register an operation whose terminal state may need to be confirmed
|
||||
/// after the caller has already observed a timeout.
|
||||
pub fn register_task_for_terminal_confirmation(
|
||||
&self,
|
||||
execution_id: String,
|
||||
metadata: RunningTaskMetadata,
|
||||
) -> RegisteredQuery {
|
||||
self.register_task_with_completion_tracking(execution_id, metadata, true)
|
||||
}
|
||||
|
||||
fn register_task_with_completion_tracking(
|
||||
&self,
|
||||
execution_id: String,
|
||||
metadata: RunningTaskMetadata,
|
||||
remember_completion: bool,
|
||||
) -> RegisteredQuery {
|
||||
let token = CancellationToken::new();
|
||||
let registration_id = self.next_registration_id.fetch_add(1, Ordering::Relaxed) + 1;
|
||||
let previous = self.inner.lock().unwrap_or_else(|e| e.into_inner()).insert(
|
||||
execution_id.clone(),
|
||||
RunningTask { registration_id, token: token.clone(), metadata, pool_key: None, interrupt: None },
|
||||
);
|
||||
if let Some(previous) = previous {
|
||||
let (completion, _) = watch::channel(false);
|
||||
let previous = {
|
||||
let mut inner = self.inner.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let mut completed = self.completed.lock().unwrap_or_else(|e| e.into_inner());
|
||||
completed.retain(|_, completed_at| completed_at.elapsed() < DETACHED_REGISTRATION_GRACE_PERIOD);
|
||||
completed.remove(&execution_id);
|
||||
inner.insert(
|
||||
execution_id.clone(),
|
||||
RunningTask {
|
||||
registration_id,
|
||||
token: token.clone(),
|
||||
completion,
|
||||
remember_completion,
|
||||
metadata,
|
||||
pool_key: None,
|
||||
interrupt: None,
|
||||
},
|
||||
)
|
||||
};
|
||||
if let Some(mut previous) = previous {
|
||||
previous.token.cancel();
|
||||
if let Some(interrupt) = previous.interrupt {
|
||||
previous.completion.send_replace(true);
|
||||
if let Some(interrupt) = previous.interrupt.take() {
|
||||
interrupt();
|
||||
}
|
||||
}
|
||||
@@ -105,6 +151,16 @@ impl RunningQueries {
|
||||
RegisteredQuery { execution_id, registration_id, token, running_queries: self.clone(), detached: false }
|
||||
}
|
||||
|
||||
fn completed_execution_is_terminal(&self, execution_id: &str) -> bool {
|
||||
let inner = self.inner.lock().unwrap_or_else(|e| e.into_inner());
|
||||
if inner.contains_key(execution_id) {
|
||||
return false;
|
||||
}
|
||||
let mut completed = self.completed.lock().unwrap_or_else(|e| e.into_inner());
|
||||
completed.retain(|_, completed_at| completed_at.elapsed() < DETACHED_REGISTRATION_GRACE_PERIOD);
|
||||
completed.contains_key(execution_id)
|
||||
}
|
||||
|
||||
/// Hands over the driver-specific interrupt handle (e.g. a MySQL
|
||||
/// `KILL QUERY` or a DuckDB worker cancel) once it becomes available,
|
||||
/// which is necessarily *after* the task itself was registered.
|
||||
@@ -115,30 +171,74 @@ impl RunningQueries {
|
||||
/// of being stored, which would otherwise leak it forever (nothing ever
|
||||
/// visits an interrupt for a task that no longer exists).
|
||||
pub fn register_interrupt(&self, execution_id: &str, interrupt: impl Fn() + Send + 'static) {
|
||||
let mut inner = self.inner.lock().unwrap_or_else(|e| e.into_inner());
|
||||
if let Some(task) = inner.get_mut(execution_id) {
|
||||
task.interrupt = Some(Box::new(interrupt));
|
||||
return;
|
||||
let mut interrupt = Some(Box::new(interrupt) as InterruptFn);
|
||||
{
|
||||
let mut inner = self.inner.lock().unwrap_or_else(|e| e.into_inner());
|
||||
if let Some(task) = inner.get_mut(execution_id) {
|
||||
if !task.token.is_cancelled() {
|
||||
task.interrupt = interrupt.take();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
drop(inner);
|
||||
interrupt();
|
||||
interrupt.expect("interrupt must remain available for a cancelled or missing task")();
|
||||
}
|
||||
|
||||
pub fn cancel(&self, execution_id: &str) -> bool {
|
||||
let task = {
|
||||
let (interrupt, completed_task) = {
|
||||
let mut inner = self.inner.lock().unwrap_or_else(|e| e.into_inner());
|
||||
inner.remove(execution_id)
|
||||
};
|
||||
match task {
|
||||
Some(task) => {
|
||||
if let Some(interrupt) = task.interrupt {
|
||||
interrupt();
|
||||
}
|
||||
task.token.cancel();
|
||||
true
|
||||
let Some(task) = inner.get_mut(execution_id) else {
|
||||
return false;
|
||||
};
|
||||
task.token.cancel();
|
||||
if task.remember_completion {
|
||||
(task.interrupt.take(), None)
|
||||
} else {
|
||||
let mut task = inner.remove(execution_id).expect("registered task must still exist");
|
||||
(task.interrupt.take(), Some(task))
|
||||
}
|
||||
None => false,
|
||||
};
|
||||
if let Some(interrupt) = interrupt {
|
||||
interrupt();
|
||||
}
|
||||
if let Some(task) = completed_task {
|
||||
task.completion.send_replace(true);
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// A cancellation signal is not a terminal execution result. Wait for the
|
||||
/// owning task to finish before reporting that callers may reconcile data.
|
||||
pub async fn cancel_and_wait(&self, execution_id: &str, timeout: Duration) -> CancellationWaitResult {
|
||||
let Some(mut completion) = ({
|
||||
let inner = self.inner.lock().unwrap_or_else(|e| e.into_inner());
|
||||
inner.get(execution_id).map(|task| task.completion.subscribe())
|
||||
}) else {
|
||||
return CancellationWaitResult {
|
||||
requested: false,
|
||||
terminal: self.completed_execution_is_terminal(execution_id),
|
||||
};
|
||||
};
|
||||
if !self.cancel(execution_id) {
|
||||
return CancellationWaitResult {
|
||||
requested: false,
|
||||
terminal: self.completed_execution_is_terminal(execution_id),
|
||||
};
|
||||
}
|
||||
|
||||
let terminal = tokio::time::timeout(timeout, async {
|
||||
loop {
|
||||
if *completion.borrow() {
|
||||
return;
|
||||
}
|
||||
if completion.changed().await.is_err() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
})
|
||||
.await
|
||||
.is_ok();
|
||||
CancellationWaitResult { requested: true, terminal }
|
||||
}
|
||||
|
||||
pub fn cancel_connection(&self, connection_id: &str) -> usize {
|
||||
@@ -218,9 +318,22 @@ impl RunningQueries {
|
||||
}
|
||||
|
||||
fn remove(&self, execution_id: &str, registration_id: u64) {
|
||||
let mut inner = self.inner.lock().unwrap_or_else(|e| e.into_inner());
|
||||
if inner.get(execution_id).is_some_and(|task| task.registration_id == registration_id) {
|
||||
inner.remove(execution_id);
|
||||
let removed = {
|
||||
let mut inner = self.inner.lock().unwrap_or_else(|e| e.into_inner());
|
||||
let should_remove = inner.get(execution_id).is_some_and(|task| task.registration_id == registration_id);
|
||||
if !should_remove {
|
||||
return;
|
||||
}
|
||||
if inner.get(execution_id).is_some_and(|task| task.remember_completion) {
|
||||
self.completed
|
||||
.lock()
|
||||
.unwrap_or_else(|e| e.into_inner())
|
||||
.insert(execution_id.to_string(), std::time::Instant::now());
|
||||
}
|
||||
inner.remove(execution_id)
|
||||
};
|
||||
if let Some(task) = removed {
|
||||
task.completion.send_replace(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -314,7 +427,7 @@ impl Drop for RegisteredQuery {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{RunningQueries, RunningTaskKind, RunningTaskMetadata, DETACHED_REGISTRATION_GRACE_PERIOD};
|
||||
use std::time::Duration;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
#[test]
|
||||
fn cancel_marks_registered_query_as_cancelled() {
|
||||
@@ -425,6 +538,66 @@ mod tests {
|
||||
assert!(running.cancel("exec-client"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn cancel_and_wait_requires_the_execution_guard_to_finish() {
|
||||
let running = RunningQueries::default();
|
||||
let registered = running.register_task_for_terminal_confirmation("exec-1".to_string(), Default::default());
|
||||
let waiting = {
|
||||
let running = running.clone();
|
||||
tokio::spawn(async move { running.cancel_and_wait("exec-1", Duration::from_secs(1)).await })
|
||||
};
|
||||
|
||||
tokio::task::yield_now().await;
|
||||
assert!(!waiting.is_finished());
|
||||
drop(registered);
|
||||
|
||||
let result = waiting.await.unwrap();
|
||||
assert!(result.requested);
|
||||
assert!(result.terminal);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn cancel_and_wait_recognizes_a_recently_completed_execution() {
|
||||
let running = RunningQueries::default();
|
||||
let registered = running.register_task_for_terminal_confirmation("exec-1".to_string(), Default::default());
|
||||
drop(registered);
|
||||
|
||||
let result = running.cancel_and_wait("exec-1", Duration::from_secs(1)).await;
|
||||
assert!(!result.requested);
|
||||
assert!(result.terminal);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn terminal_completion_is_available_after_cancel_wait_times_out() {
|
||||
let running = RunningQueries::default();
|
||||
let registered = running.register_task_for_terminal_confirmation("exec-1".to_string(), Default::default());
|
||||
|
||||
let first = running.cancel_and_wait("exec-1", Duration::from_millis(1)).await;
|
||||
assert!(first.requested);
|
||||
assert!(!first.terminal);
|
||||
|
||||
drop(registered);
|
||||
let later = running.cancel_and_wait("exec-1", Duration::from_secs(1)).await;
|
||||
assert!(!later.requested);
|
||||
assert!(later.terminal);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn stale_terminal_completion_is_reclaimed() {
|
||||
let running = RunningQueries::default();
|
||||
running
|
||||
.completed
|
||||
.lock()
|
||||
.unwrap_or_else(|error| error.into_inner())
|
||||
.insert("exec-1".to_string(), Instant::now() - DETACHED_REGISTRATION_GRACE_PERIOD);
|
||||
|
||||
let result = running.cancel_and_wait("exec-1", Duration::from_secs(1)).await;
|
||||
|
||||
assert!(!result.requested);
|
||||
assert!(!result.terminal);
|
||||
assert!(running.completed.lock().unwrap_or_else(|error| error.into_inner()).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stale_registration_drop_does_not_remove_replacement() {
|
||||
let running = RunningQueries::default();
|
||||
@@ -535,6 +708,26 @@ mod tests {
|
||||
assert_eq!(running.registration_counts(), (0, 0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn late_interrupt_after_tracked_cancel_runs_immediately_but_waits_for_guard_cleanup() {
|
||||
let running = RunningQueries::default();
|
||||
let registered = running.register_task_for_terminal_confirmation("exec-1".to_string(), Default::default());
|
||||
|
||||
assert!(running.cancel("exec-1"));
|
||||
|
||||
let interrupted = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
|
||||
let flag = interrupted.clone();
|
||||
running.register_interrupt("exec-1", move || {
|
||||
flag.store(true, std::sync::atomic::Ordering::SeqCst);
|
||||
});
|
||||
|
||||
assert!(interrupted.load(std::sync::atomic::Ordering::SeqCst));
|
||||
assert_eq!(running.registration_counts(), (1, 0));
|
||||
|
||||
drop(registered);
|
||||
assert_eq!(running.registration_counts(), (0, 0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn completion_after_cancel_removes_all_registrations() {
|
||||
let running = RunningQueries::default();
|
||||
|
||||
@@ -7,8 +7,8 @@ use dbx_core::connection::AppState;
|
||||
use dbx_core::models::connection::{ConnectionConfig, DatabaseType};
|
||||
use dbx_core::query::{
|
||||
execute_multi_core, execute_multi_core_with_options_for_client_and_progress,
|
||||
execute_multi_core_with_options_for_client_typed, execute_sql_statement, ExecuteMultiProgressCallback,
|
||||
QueryExecutionOptions,
|
||||
execute_multi_core_with_options_for_client_typed, execute_sql_statement, execute_sql_statement_with_options_typed,
|
||||
ExecuteMultiProgressCallback, QueryExecutionOptions,
|
||||
};
|
||||
use dbx_core::query_result_export::{export_query_result_core, ExportStatus, QueryResultExportRequest};
|
||||
use dbx_core::sql::{split_sql_statements_for_database, SqlFileRequest};
|
||||
@@ -761,6 +761,124 @@ async fn live_mysql_query_cancel_kills_running_sleep() {
|
||||
assert_eq!(result.rows, vec![vec![serde_json::json!("1")]]);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "requires DBX_LIVE_SQL_FILE_MYSQL_* env vars pointing at a writable MySQL connection"]
|
||||
async fn live_mysql_conditional_update_timeout_cancels_before_reload() {
|
||||
let suffix = uuid::Uuid::new_v4().simple().to_string();
|
||||
let connection_id = format!("live-mysql-conditional-update-{suffix}");
|
||||
let database = std::env::var("DBX_LIVE_SQL_FILE_MYSQL_DATABASE").expect("DBX_LIVE_SQL_FILE_MYSQL_DATABASE");
|
||||
let config = live_mysql_sql_file_config(&connection_id);
|
||||
let (app_state, db_path) = app_state_with_config(config).await;
|
||||
let state = Arc::new(app_state);
|
||||
let table = format!("dbx_conditional_update_{}", &suffix[..8]);
|
||||
let trigger = format!("dbx_conditional_update_delay_{}", &suffix[..8]);
|
||||
|
||||
execute_sql_statement(
|
||||
&state,
|
||||
&connection_id,
|
||||
&database,
|
||||
&format!("CREATE TABLE `{table}` (id INT PRIMARY KEY, value INT NOT NULL)"),
|
||||
None,
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.expect("create conditional update table");
|
||||
execute_sql_statement(
|
||||
&state,
|
||||
&connection_id,
|
||||
&database,
|
||||
&format!("INSERT INTO `{table}` (id, value) VALUES (1, 1)"),
|
||||
None,
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.expect("seed conditional update table");
|
||||
execute_sql_statement(
|
||||
&state,
|
||||
&connection_id,
|
||||
&database,
|
||||
&format!("CREATE TRIGGER `{trigger}` BEFORE UPDATE ON `{table}` FOR EACH ROW BEGIN DO SLEEP(30); END"),
|
||||
None,
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.expect("create delayed update trigger");
|
||||
|
||||
let execution_id = format!("conditional-update-{suffix}");
|
||||
let registered =
|
||||
state.running_queries.register_task_for_terminal_confirmation(execution_id.clone(), Default::default());
|
||||
let cancel_token = registered.token();
|
||||
let (result_tx, mut result_rx) = tokio::sync::oneshot::channel();
|
||||
let task_state = state.clone();
|
||||
let task_connection_id = connection_id.clone();
|
||||
let task_database = database.clone();
|
||||
let task_table = table.clone();
|
||||
let task_execution_id = execution_id.clone();
|
||||
tokio::spawn(async move {
|
||||
let result = execute_sql_statement_with_options_typed(
|
||||
&task_state,
|
||||
&task_connection_id,
|
||||
&task_database,
|
||||
&format!("UPDATE `{task_table}` SET value = 2 WHERE id = 1"),
|
||||
None,
|
||||
Some(cancel_token),
|
||||
QueryExecutionOptions {
|
||||
// The request-facing timeout belongs to the caller. Keep this
|
||||
// database task running so its registration remains cancellable.
|
||||
timeout_secs: Some(0),
|
||||
await_cancel_completion: true,
|
||||
execution_id: Some(task_execution_id),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.await;
|
||||
let _ = result_tx.send(result);
|
||||
drop(registered);
|
||||
});
|
||||
|
||||
tokio::time::timeout(Duration::from_secs(5), async {
|
||||
while state.running_queries.diagnostics().interrupt_registrations != 1 {
|
||||
tokio::time::sleep(Duration::from_millis(10)).await;
|
||||
}
|
||||
})
|
||||
.await
|
||||
.expect("delayed update should register a MySQL interrupt");
|
||||
assert!(tokio::time::timeout(Duration::from_secs(1), &mut result_rx).await.is_err());
|
||||
|
||||
let cancellation = state.running_queries.cancel_and_wait(&execution_id, Duration::from_secs(10)).await;
|
||||
assert!(cancellation.requested);
|
||||
assert!(cancellation.terminal);
|
||||
let _ = result_rx.await.expect("conditional update task should report a terminal result");
|
||||
|
||||
let reload = execute_sql_statement(
|
||||
&state,
|
||||
&connection_id,
|
||||
&database,
|
||||
&format!("SELECT value FROM `{table}` WHERE id = 1"),
|
||||
None,
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.expect("reload row after cancellation");
|
||||
assert_eq!(reload.rows.len(), 1);
|
||||
assert_eq!(json_cell_text(&reload.rows[0][0]), "1");
|
||||
|
||||
execute_sql_statement(
|
||||
&state,
|
||||
&connection_id,
|
||||
&database,
|
||||
&format!("DROP TRIGGER IF EXISTS `{trigger}`"),
|
||||
None,
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.expect("drop delayed update trigger");
|
||||
execute_sql_statement(&state, &connection_id, &database, &format!("DROP TABLE IF EXISTS `{table}`"), None, None)
|
||||
.await
|
||||
.expect("drop conditional update table");
|
||||
let _ = std::fs::remove_file(db_path);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "requires a remote MySQL endpoint"]
|
||||
async fn live_mysql_recovers_after_server_idle_disconnect() {
|
||||
|
||||
Reference in new issue
Block a user