fix: support mysql install scripts without selected database (#2356)
This commit is contained in:
1 parent
730f839e74
commit
71e618dd46
12 files changed
+975
-90
No files matched your search
@@ -1,3 +1,43 @@
|
||||
use dbx_core::connection::AppState;
|
||||
use dbx_core::models::connection::{ConnectionConfig, DatabaseType};
|
||||
use dbx_core::query::execute_sql_statement;
|
||||
use dbx_core::sql::SqlFileRequest;
|
||||
use dbx_core::sql_file_import::execute_sql_file_content;
|
||||
use dbx_core::storage::Storage;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
fn live_mysql_sql_file_config(id: &str) -> ConnectionConfig {
|
||||
let host = std::env::var("DBX_LIVE_SQL_FILE_MYSQL_HOST").expect("DBX_LIVE_SQL_FILE_MYSQL_HOST");
|
||||
let port =
|
||||
std::env::var("DBX_LIVE_SQL_FILE_MYSQL_PORT").ok().and_then(|value| value.parse::<u16>().ok()).unwrap_or(3306);
|
||||
let username = std::env::var("DBX_LIVE_SQL_FILE_MYSQL_USER").expect("DBX_LIVE_SQL_FILE_MYSQL_USER");
|
||||
let password = std::env::var("DBX_LIVE_SQL_FILE_MYSQL_PASSWORD").expect("DBX_LIVE_SQL_FILE_MYSQL_PASSWORD");
|
||||
|
||||
serde_json::from_value(serde_json::json!({
|
||||
"id": id,
|
||||
"name": id,
|
||||
"db_type": DatabaseType::Mysql,
|
||||
"host": host,
|
||||
"port": port,
|
||||
"username": username,
|
||||
"password": password,
|
||||
"database": null,
|
||||
"connect_timeout_secs": 5,
|
||||
"query_timeout_secs": 30,
|
||||
"idle_timeout_secs": 60,
|
||||
"keepalive_interval_secs": 0
|
||||
}))
|
||||
.expect("live MySQL SQL file config should deserialize")
|
||||
}
|
||||
|
||||
async fn app_state_with_config(config: ConnectionConfig) -> (AppState, std::path::PathBuf) {
|
||||
let db_path = std::env::temp_dir().join(format!("dbx-live-sql-file-{}.db", uuid::Uuid::new_v4().simple()));
|
||||
let storage = Storage::open(&db_path).await.expect("open temp storage");
|
||||
let state = AppState::new(storage);
|
||||
state.configs.write().await.insert(config.id.clone(), config);
|
||||
(state, db_path)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "requires the remote DBX MySQL 5.7 smoke-test container"]
|
||||
async fn live_mysql57_text_protocol_select_succeeds() {
|
||||
@@ -190,3 +230,73 @@ async fn live_mysql_recovers_after_server_idle_disconnect() {
|
||||
assert_eq!(result.columns, vec!["recovered"]);
|
||||
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 without a required default database"]
|
||||
async fn live_sql_file_import_creates_database_and_switches_context_without_default_database() {
|
||||
let config = live_mysql_sql_file_config("sql-file-import");
|
||||
let (state, db_path) = app_state_with_config(config.clone()).await;
|
||||
let suffix = uuid::Uuid::new_v4().simple().to_string();
|
||||
let database_name = format!("dbx_issue_2356_{suffix}");
|
||||
let script = format!(
|
||||
r#"
|
||||
CREATE DATABASE `{database_name}`;
|
||||
USE `{database_name}`;
|
||||
CREATE TABLE install_check (
|
||||
id INT PRIMARY KEY
|
||||
);
|
||||
INSERT INTO install_check (id) VALUES (1), (2);
|
||||
"#
|
||||
);
|
||||
let request = SqlFileRequest {
|
||||
execution_id: format!("exec-{suffix}"),
|
||||
connection_id: config.id.clone(),
|
||||
database: String::new(),
|
||||
file_path: "issue-2356-mysql-install.sql".to_string(),
|
||||
continue_on_error: false,
|
||||
};
|
||||
|
||||
let _ = execute_sql_statement(
|
||||
&state,
|
||||
&config.id,
|
||||
"",
|
||||
&format!("DROP DATABASE IF EXISTS `{database_name}`"),
|
||||
None,
|
||||
None,
|
||||
)
|
||||
.await;
|
||||
|
||||
let result = execute_sql_file_content(
|
||||
&state,
|
||||
&request,
|
||||
&script,
|
||||
CancellationToken::new(),
|
||||
std::time::Instant::now(),
|
||||
|_| {},
|
||||
)
|
||||
.await;
|
||||
let verify = execute_sql_statement(
|
||||
&state,
|
||||
&config.id,
|
||||
&database_name,
|
||||
"SELECT COUNT(*) AS count FROM install_check",
|
||||
None,
|
||||
None,
|
||||
)
|
||||
.await;
|
||||
let _ = execute_sql_statement(
|
||||
&state,
|
||||
&config.id,
|
||||
"",
|
||||
&format!("DROP DATABASE IF EXISTS `{database_name}`"),
|
||||
None,
|
||||
None,
|
||||
)
|
||||
.await;
|
||||
let _ = std::fs::remove_file(db_path);
|
||||
|
||||
result.expect("SQL file import should succeed");
|
||||
let verify = verify.expect("verify imported rows");
|
||||
assert_eq!(verify.columns, vec!["count"]);
|
||||
assert_eq!(verify.rows, vec![vec![serde_json::json!("2")]]);
|
||||
}
|
||||
Reference in new issue
Block a user