feat(schema-diff): preserve dialect-specific DDL semantics

This commit is contained in:
dbin0123 authored and GitHub committed 2026-07-29 03:47:19 +08:00
1 parent 820b8cd26f
commit 002a2c85c0
146 files changed
+36532 -1375

No files matched your search

+110
View File
@@ -241,6 +241,33 @@ pub async fn execute_in_transaction(
.await
}
/// Schema Diff deploy entrypoint (legacy command name kept for API compatibility).
///
/// Executes as one single-connection transaction via [`execute_schema_diff_deploy`].
/// On failure, status is `rolled_back` when DDL/DML atomicity is guaranteed for the
/// target, otherwise `mixed` with a best-effort `executed_count` (e.g. MySQL/Oracle DDL).
pub async fn execute_script_with_2pc_core(
app: Arc<AppState>,
connection_id: String,
database: String,
statements: Vec<String>,
schema: Option<String>,
) -> dbx_core::query::SchemaDiffDeployResult {
dbx_core::query::execute_schema_diff_deploy(&app, &connection_id, &database, &statements, schema.as_deref()).await
}
#[tauri::command]
pub async fn execute_script_with_2pc(
state: State<'_, Arc<AppState>>,
connection_id: String,
database: String,
statements: Vec<String>,
schema: Option<String>,
) -> Result<dbx_core::query::SchemaDiffDeployResult, String> {
let app: Arc<AppState> = (*state).clone();
Ok(execute_script_with_2pc_core(app, connection_id, database, statements, schema).await)
}
#[tauri::command]
pub async fn begin_manual_transaction(
state: State<'_, Arc<AppState>>,
@@ -676,3 +703,86 @@ pub async fn get_explain_info(
pub fn build_create_user_sql(username: String, password: String, tablespace: String) -> Result<String, String> {
Ok(dbx_core::db_admin_sql::build_create_user_sql(&username, &password, &tablespace))
}
#[cfg(test)]
mod tests {
use super::*;
use dbx_core::storage::Storage;
use std::sync::Arc;
async fn test_app_state() -> Arc<AppState> {
let dir = std::env::temp_dir().join(format!("dbx-query-test-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&dir).unwrap();
let storage = Storage::open(&dir.join("storage.db")).await.unwrap();
Arc::new(AppState::new_with_plugin_dir(storage, dir.join("plugins")))
}
#[tokio::test]
async fn execute_script_with_2pc_returns_structured_result() {
let state = test_app_state().await;
let result = execute_script_with_2pc_core(
state,
"conn-1".to_string(),
"testdb".to_string(),
vec!["SELECT 1".to_string()],
None,
)
.await;
assert!(!result.transaction_id.is_empty());
assert!(!result.participants.is_empty());
// No real connection: rolled_back with error, not silent auto-commit success.
assert_eq!(result.status, "rolled_back");
assert!(result.error.as_ref().is_some_and(|e| !e.is_empty()));
assert_eq!(result.statement_count, 1);
assert_eq!(result.executed_count, 0);
}
#[tokio::test]
async fn execute_script_with_2pc_empty_statements_succeeds() {
let state = test_app_state().await;
let result =
execute_script_with_2pc_core(state, "conn-empty".to_string(), "testdb".to_string(), vec![], None).await;
assert_eq!(result.status, "committed");
assert_eq!(result.statement_count, 0);
assert_eq!(result.executed_count, 0);
assert!(result.error.is_none());
assert_eq!(result.participants.len(), 1);
}
#[tokio::test]
async fn execute_script_with_2pc_comment_only_is_empty_success() {
let state = test_app_state().await;
let result = execute_script_with_2pc_core(
state,
"conn-comment".to_string(),
"testdb".to_string(),
vec!["-- WARNING: incomplete\n-- manual only".to_string()],
None,
)
.await;
assert_eq!(result.status, "committed");
assert_eq!(result.statement_count, 0);
assert!(result.error.is_none());
}
#[tokio::test]
async fn execute_script_with_2pc_propagates_structured_failure_fields() {
let state = test_app_state().await;
let result = execute_script_with_2pc_core(
state,
"missing-conn".to_string(),
"testdb".to_string(),
vec!["CREATE TABLE t1 (id INT)".to_string(), "CREATE TABLE t2 (id INT)".to_string()],
None,
)
.await;
assert!(result.status == "rolled_back" || result.status == "mixed", "status={}", result.status);
assert_eq!(result.statement_count, 2);
assert!(result.error.as_ref().is_some_and(|e| !e.is_empty()));
assert_eq!(result.executed_count, 0);
}
}