fix(sql-file): preserve Oracle schema during file execution
Closes #10502
This commit is contained in:
20 files changed
+269
-41
No files matched your search
@@ -8653,6 +8653,21 @@ mod tests {
|
||||
assert_eq!(scoped.database.as_deref(), Some("ORCL"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn connection_root_schema_databases_keep_the_configured_database() {
|
||||
for database_type in [DatabaseType::Oracle, DatabaseType::Dameng, DatabaseType::OceanbaseOracle] {
|
||||
let mut config = mysql_config(Some("tenant_service"));
|
||||
config.db_type = database_type;
|
||||
|
||||
let scoped = database_connection_config(&config, Some("APP"));
|
||||
|
||||
assert_eq!(scoped.database.as_deref(), Some("tenant_service"));
|
||||
}
|
||||
|
||||
let mysql = database_connection_config(&mysql_config(Some("tenant_service")), Some("analytics"));
|
||||
assert_eq!(mysql.database.as_deref(), Some("analytics"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn oracle_reuses_connection_scoped_pool_for_schema_database_keys() {
|
||||
assert_eq!(
|
||||
|
||||
@@ -589,7 +589,7 @@ async fn set_relational_constraints_enabled(
|
||||
&request.connection_id,
|
||||
&request.database,
|
||||
sql,
|
||||
None,
|
||||
request.schema.as_deref(),
|
||||
Some(token.clone()),
|
||||
QueryExecutionOptions::default(),
|
||||
)
|
||||
@@ -604,10 +604,13 @@ async fn sql_file_transaction_schema(state: &AppState, request: &SqlFileRequest)
|
||||
let sessions = state.transaction_sessions.read().await;
|
||||
let session =
|
||||
sessions.get(session_id).ok_or("SQL file transaction session not found; no statements were retried")?;
|
||||
if session.connection_id != request.connection_id || session.database != request.database {
|
||||
if session.connection_id != request.connection_id
|
||||
|| session.database != request.database
|
||||
|| request.schema.as_ref().is_some_and(|schema| session.schema.as_ref() != Some(schema))
|
||||
{
|
||||
return Err("SQL file target does not match its manual transaction".to_string());
|
||||
}
|
||||
Ok(session.schema.clone())
|
||||
Ok(request.schema.clone().or_else(|| session.schema.clone()))
|
||||
}
|
||||
|
||||
async fn with_sql_file_transaction<T>(
|
||||
@@ -2592,7 +2595,7 @@ async fn execute_sql_file_statement(
|
||||
&request.connection_id,
|
||||
&request.database,
|
||||
sql,
|
||||
None,
|
||||
request.schema.as_deref(),
|
||||
Some(child_token),
|
||||
QueryExecutionOptions { execution_id: Some(execution_id), timeout_secs, ..Default::default() },
|
||||
)
|
||||
@@ -2704,6 +2707,7 @@ mod tests {
|
||||
execution_id: "file-missing-session".to_string(),
|
||||
connection_id: "unconfigured".to_string(),
|
||||
database: String::new(),
|
||||
schema: None,
|
||||
file_path: String::new(),
|
||||
continue_on_error: false,
|
||||
selected_tables: None,
|
||||
@@ -3056,6 +3060,7 @@ mod tests {
|
||||
execution_id: "file-progress".to_string(),
|
||||
connection_id: "unconfigured".to_string(),
|
||||
database: String::new(),
|
||||
schema: None,
|
||||
file_path: path.to_string_lossy().to_string(),
|
||||
continue_on_error: false,
|
||||
selected_tables: None,
|
||||
@@ -3488,6 +3493,7 @@ mod tests {
|
||||
execution_id: "gauss-stop-on-error".to_string(),
|
||||
connection_id: "gauss-stream".to_string(),
|
||||
database: String::new(),
|
||||
schema: None,
|
||||
file_path: path.to_string_lossy().to_string(),
|
||||
continue_on_error: true,
|
||||
selected_tables: None,
|
||||
|
||||
@@ -11427,7 +11427,11 @@ for line in sys.stdin:
|
||||
request = json.loads(line)
|
||||
method = request.get("method")
|
||||
if method == "execute_query":
|
||||
sql = request.get("params", {{}}).get("sql", "")
|
||||
params = request.get("params", {{}})
|
||||
sql = params.get("sql", "")
|
||||
if "REQUIRE_APP_SCHEMA" in sql and params.get("schema") != "APP":
|
||||
print(json.dumps({{"jsonrpc": "2.0", "id": request["id"], "error": {{"code": -32000, "message": "schema was not forwarded"}}}}), flush=True)
|
||||
continue
|
||||
if "RAISE_FILE_ERROR" in sql:
|
||||
print(json.dumps({{"jsonrpc": "2.0", "id": request["id"], "error": {{"code": -32000, "message": "file statement rejected"}}}}), flush=True)
|
||||
continue
|
||||
@@ -11491,7 +11495,7 @@ for line in sys.stdin:
|
||||
snapshot_rotation_safe: true,
|
||||
connection_id: "agent-conn".to_string(),
|
||||
database: "ORCL".to_string(),
|
||||
schema: None,
|
||||
schema: Some("APP".to_string()),
|
||||
},
|
||||
);
|
||||
(state, txn_session_id, dir)
|
||||
@@ -11503,6 +11507,7 @@ for line in sys.stdin:
|
||||
execution_id: "manual-file-test".to_string(),
|
||||
connection_id: "agent-conn".to_string(),
|
||||
database: "ORCL".to_string(),
|
||||
schema: Some("APP".to_string()),
|
||||
file_path: String::new(),
|
||||
continue_on_error: false,
|
||||
selected_tables: None,
|
||||
@@ -11521,7 +11526,7 @@ for line in sys.stdin:
|
||||
crate::data::sql_file_import::execute_sql_file_content(
|
||||
&state,
|
||||
&request,
|
||||
"UPDATE T SET V = 1; UPDATE T SET V = 2;",
|
||||
"SELECT REQUIRE_APP_SCHEMA; UPDATE T SET V = 2;",
|
||||
CancellationToken::new(),
|
||||
std::time::Instant::now(),
|
||||
|event| events.push(event),
|
||||
@@ -11637,6 +11642,23 @@ for line in sys.stdin:
|
||||
assert!(events.is_empty());
|
||||
assert!(state.transaction_sessions.read().await.contains_key(&session_id));
|
||||
}
|
||||
let mut request = manual_sql_file_request(&session_id);
|
||||
request.schema = Some("OTHER".to_string());
|
||||
let mut events = Vec::new();
|
||||
let error = crate::data::sql_file_import::execute_sql_file_content(
|
||||
&state,
|
||||
&request,
|
||||
"UPDATE T SET V = 1;",
|
||||
CancellationToken::new(),
|
||||
std::time::Instant::now(),
|
||||
|event| events.push(event),
|
||||
)
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert!(error.contains("target does not match"));
|
||||
assert!(events.is_empty());
|
||||
assert!(state.transaction_sessions.read().await.contains_key(&session_id));
|
||||
|
||||
rollback_manual_transaction(&state, &session_id).await.unwrap();
|
||||
let error = crate::data::sql_file_import::execute_sql_file_content(
|
||||
&state,
|
||||
|
||||
@@ -1297,6 +1297,7 @@ INSERT INTO install_check (id) VALUES (1), (2);
|
||||
execution_id: format!("exec-{suffix}"),
|
||||
connection_id: config.id.clone(),
|
||||
database: String::new(),
|
||||
schema: None,
|
||||
file_path: std::env::temp_dir()
|
||||
.join(format!("issue-2356-mysql-install-{suffix}.sql"))
|
||||
.to_string_lossy()
|
||||
@@ -1386,6 +1387,7 @@ INSERT INTO children (parent_id) VALUES (LAST_INSERT_ID());
|
||||
execution_id: format!("exec-{suffix}"),
|
||||
connection_id: config.id.clone(),
|
||||
database: String::new(),
|
||||
schema: None,
|
||||
file_path: std::env::temp_dir()
|
||||
.join(format!("issue-7738-mysql-order-{suffix}.sql"))
|
||||
.to_string_lossy()
|
||||
@@ -1462,6 +1464,7 @@ async fn live_sql_file_import_preserves_raw_mysql_binary_literal_bytes() {
|
||||
execution_id: format!("exec-{suffix}"),
|
||||
connection_id: config.id.clone(),
|
||||
database: String::new(),
|
||||
schema: None,
|
||||
file_path: std::env::temp_dir().join(format!("mysql-binary-dump-{suffix}.sql")).to_string_lossy().into_owned(),
|
||||
continue_on_error: false,
|
||||
selected_tables: None,
|
||||
|
||||
@@ -74,7 +74,7 @@ async fn live_mysql_selected_table_restore_preserves_unselected_tables() {
|
||||
assert_eq!(tables.len(), 2);
|
||||
let request = SqlFileRequest {
|
||||
txn_session_id: None,
|
||||
execution_id: format!("restore-{suffix}-{compressed}"), connection_id: connection_id.clone(), database: database.clone(), file_path: path.display().to_string(), continue_on_error: false,
|
||||
execution_id: format!("restore-{suffix}-{compressed}"), connection_id: connection_id.clone(), database: database.clone(), schema: None, file_path: path.display().to_string(), continue_on_error: false,
|
||||
selected_tables: Some(vec![SqlFileTable { database: Some(database.clone()), name: "chosen".into() }]),
|
||||
part_cooldown_ms: 0,
|
||||
skip_relational_constraints: false,
|
||||
@@ -91,7 +91,7 @@ async fn live_mysql_selected_table_restore_preserves_unselected_tables() {
|
||||
std::fs::write(&path, "DROP TABLE chosen; INSERT INTO chosen VALUES (1, 'bad'); CALL unexpected();").map_err(|e| e.to_string())?;
|
||||
let request = SqlFileRequest {
|
||||
txn_session_id: None,
|
||||
execution_id: format!("invalid-{suffix}"), connection_id: connection_id.clone(), database: database.clone(), file_path: path.display().to_string(), continue_on_error: true,
|
||||
execution_id: format!("invalid-{suffix}"), connection_id: connection_id.clone(), database: database.clone(), schema: None, file_path: path.display().to_string(), continue_on_error: true,
|
||||
selected_tables: Some(vec![SqlFileTable { database: None, name: "chosen".into() }]),
|
||||
part_cooldown_ms: 0,
|
||||
skip_relational_constraints: false,
|
||||
@@ -175,6 +175,7 @@ async fn live_mysql_database_export_restores_dependent_views() {
|
||||
execution_id: format!("live-mysql-import-{suffix}"),
|
||||
connection_id: connection_id.clone(),
|
||||
database: String::new(),
|
||||
schema: None,
|
||||
file_path: file_path.to_string_lossy().to_string(),
|
||||
continue_on_error: false,
|
||||
selected_tables: None,
|
||||
|
||||
@@ -65,7 +65,7 @@ async fn live_oceanbase_sql_files_commit_rollback_failure_and_cancel() {
|
||||
query(&state, "file-writer", &database, &format!("INSERT INTO {table} VALUES (1, 10)")).await?;
|
||||
let mut request = SqlFileRequest {
|
||||
execution_id: "live-manual-file".to_string(), connection_id: "file-writer".to_string(),
|
||||
database: database.clone(), file_path: String::new(), continue_on_error: false,
|
||||
database: database.clone(), schema: None, file_path: String::new(), continue_on_error: false,
|
||||
selected_tables: None, part_cooldown_ms: 0, skip_relational_constraints: false, txn_session_id: None,
|
||||
};
|
||||
let first = directory.path().join("first.sql");
|
||||
|
||||
@@ -149,6 +149,7 @@ async fn live_postgres_all_schema_export_restores_one_sql_file() {
|
||||
execution_id: format!("postgres-all-schema-import-{suffix}"),
|
||||
connection_id: target_connection_id.clone(),
|
||||
database: target_database.clone(),
|
||||
schema: None,
|
||||
file_path: export_path.display().to_string(),
|
||||
continue_on_error: false,
|
||||
selected_tables: None,
|
||||
|
||||
@@ -1301,6 +1301,7 @@ async fn live_sqlserver_sql_file_import_executes_go_batches() {
|
||||
execution_id: format!("live-sqlserver-file-{suffix}"),
|
||||
connection_id: connection_id.to_string(),
|
||||
database: database.clone(),
|
||||
schema: None,
|
||||
file_path: "fixture.sql".to_string(),
|
||||
continue_on_error: false,
|
||||
selected_tables: None,
|
||||
|
||||
@@ -210,6 +210,7 @@ async fn manual_e2e_sqlserver_default_split_zip_export_then_import_skip_relation
|
||||
execution_id: format!("e2e-import-{}", uuid::Uuid::new_v4()),
|
||||
connection_id: import_connection_id.to_string(),
|
||||
database: import_database.to_string(),
|
||||
schema: None,
|
||||
file_path: extracted_paths[0].to_string_lossy().to_string(),
|
||||
continue_on_error: false,
|
||||
txn_session_id: None,
|
||||
|
||||
@@ -57,6 +57,10 @@ pub struct SqlFileRequest {
|
||||
pub execution_id: String,
|
||||
pub connection_id: String,
|
||||
pub database: String,
|
||||
/// Optional schema/session namespace for databases where it is distinct
|
||||
/// from the database used to establish the connection.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub schema: Option<String>,
|
||||
pub file_path: String,
|
||||
pub continue_on_error: bool,
|
||||
/// Reuse a held manual transaction instead of committing through ordinary query execution.
|
||||
@@ -3008,9 +3012,36 @@ mod tests {
|
||||
fuzzy_subsequence_match, optimize_sql_file_import_statements, prepare_sql_file_statement, split_sql_script,
|
||||
split_sql_statement_ranges_with_options, split_sql_statements_for_database, starts_with_executable_sql_keyword,
|
||||
starts_with_executable_sql_keyword_for_database, starts_with_oracle_style_routine_body, SqlDialectProfile,
|
||||
SqlFileStatementAction, SqlParsingOptions, SqlStatementSplitter,
|
||||
SqlFileRequest, SqlFileStatementAction, SqlParsingOptions, SqlStatementSplitter,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn sql_file_request_schema_is_optional_and_round_trips_when_present() {
|
||||
let legacy = serde_json::json!({
|
||||
"executionId": "legacy",
|
||||
"connectionId": "mysql-1",
|
||||
"database": "app",
|
||||
"filePath": "backup.sql",
|
||||
"continueOnError": false
|
||||
});
|
||||
let legacy_request: SqlFileRequest = serde_json::from_value(legacy).unwrap();
|
||||
assert_eq!(legacy_request.schema, None);
|
||||
assert!(serde_json::to_value(&legacy_request).unwrap().get("schema").is_none());
|
||||
|
||||
let with_schema = serde_json::json!({
|
||||
"executionId": "oracle-schema",
|
||||
"connectionId": "oceanbase-oracle-1",
|
||||
"database": "tenant_service",
|
||||
"schema": "APP",
|
||||
"filePath": "restore.sql",
|
||||
"continueOnError": false
|
||||
});
|
||||
let request: SqlFileRequest = serde_json::from_value(with_schema).unwrap();
|
||||
assert_eq!(request.database, "tenant_service");
|
||||
assert_eq!(request.schema.as_deref(), Some("APP"));
|
||||
assert_eq!(serde_json::to_value(request).unwrap()["schema"], "APP");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fuzzy_subsequence_match_matches_ordered_characters() {
|
||||
assert!(fuzzy_subsequence_match("system_user", "sysu"));
|
||||
|
||||
Reference in new issue
Block a user