0.8.0: MariaDB + PlanetScale, TimescaleDB, multi-result sets, streaming export, schema diff (#27)
This commit was merged in pull request #27.
This commit is contained in:
@@ -2,7 +2,7 @@ use crate::models::{Connection, ConnectionInput};
|
||||
use crate::store::Store;
|
||||
use std::sync::Mutex;
|
||||
|
||||
const VALID_DB_TYPES: [&str; 4] = ["postgresql", "mysql", "sqlite", "redis"];
|
||||
const VALID_DB_TYPES: [&str; 5] = ["postgresql", "mysql", "mariadb", "sqlite", "redis"];
|
||||
|
||||
fn validate(input: &ConnectionInput) -> Result<(), String> {
|
||||
if input.name.is_empty() || input.name.chars().count() > 100 {
|
||||
@@ -360,3 +360,7 @@ mod tests {
|
||||
assert_eq!(get_recent_connections_inner(&st, 10).unwrap().len(), 0);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "connections.test.rs"]
|
||||
mod connections_tests;
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
use super::*;
|
||||
|
||||
fn mariadb_input() -> ConnectionInput {
|
||||
ConnectionInput {
|
||||
name: "Maria local".into(),
|
||||
db_type: "mariadb".into(),
|
||||
host: "127.0.0.1".into(),
|
||||
port: Some(3306),
|
||||
username: Some("root".into()),
|
||||
folder_id: None,
|
||||
tag_ids: vec![],
|
||||
password: None,
|
||||
database: Some("test".into()),
|
||||
environment: None,
|
||||
ssh_host: None,
|
||||
ssh_port: None,
|
||||
ssh_user: None,
|
||||
ssh_auth_method: None,
|
||||
ssh_private_key_path: None,
|
||||
ssh_password: None,
|
||||
ssh_passphrase: None,
|
||||
ssl_mode: None,
|
||||
ssl_ca_path: None,
|
||||
ssl_cert_path: None,
|
||||
ssl_key_path: None,
|
||||
use_keychain: true,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate_accepts_mariadb_with_port() {
|
||||
assert!(validate(&mariadb_input()).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate_rejects_mariadb_without_port() {
|
||||
let mut input = mariadb_input();
|
||||
input.port = None;
|
||||
assert!(validate(&input).is_err(), "mariadb requires a port like mysql");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate_rejects_unknown_db_type() {
|
||||
let mut input = mariadb_input();
|
||||
input.db_type = "oracle".into();
|
||||
assert!(validate(&input).is_err());
|
||||
}
|
||||
@@ -5,8 +5,8 @@
|
||||
|
||||
use crate::db::pool::{ConnectionPoolManager, DbConfig, DbHandle};
|
||||
use crate::models::db_viewer::{
|
||||
Change, ColumnInfo, ConstraintInfo, EnumInfo, ExtensionInfo, FunctionInfo, IndexInfo,
|
||||
QueryResult, SequenceInfo, TableInfo, TriggerInfo,
|
||||
Change, ColumnInfo, ConstraintInfo, EnumInfo, ExtensionInfo, FunctionInfo, HypertableInfo,
|
||||
HypertableListResponse, IndexInfo, QueryResult, SequenceInfo, TableInfo, TriggerInfo,
|
||||
};
|
||||
use bytes::BytesMut;
|
||||
use std::collections::HashMap;
|
||||
@@ -1198,7 +1198,7 @@ pub async fn db_connect(
|
||||
}
|
||||
Err(e) => Err(format!("Connection failed: {}", e)),
|
||||
}
|
||||
} else if config.db_type == "mysql" {
|
||||
} else if config.db_type == "mysql" || config.db_type == "mariadb" {
|
||||
run_mysql_connect(
|
||||
&connection_id,
|
||||
&config,
|
||||
@@ -2640,6 +2640,58 @@ pub async fn get_functions(
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn get_hypertables(
|
||||
connection_id: String,
|
||||
schema: Option<String>,
|
||||
state: State<'_, crate::AppState>,
|
||||
) -> Result<HypertableListResponse, String> {
|
||||
let mut pm = state.pool_manager.lock().await;
|
||||
match pm.get(&connection_id) {
|
||||
Some(DbHandle::Postgresql(client, _)) => {
|
||||
let detected: bool = client
|
||||
.query_one(&crate::db::introspection::pg_timescale_detect_query(), &[])
|
||||
.await
|
||||
.map(|r| r.get::<_, bool>(0))
|
||||
.unwrap_or(false);
|
||||
if !detected {
|
||||
return Ok(HypertableListResponse {
|
||||
available: false,
|
||||
reason: Some("TimescaleDB extension is not installed".into()),
|
||||
items: vec![],
|
||||
});
|
||||
}
|
||||
let schema = schema.unwrap_or_else(|| "public".to_string());
|
||||
let query = crate::db::introspection::pg_hypertables_query(&schema);
|
||||
let rows = client
|
||||
.query(&query, &[&schema])
|
||||
.await
|
||||
.map_err(|e| format!("TimescaleDB introspection failed: {e}"))?;
|
||||
Ok(HypertableListResponse {
|
||||
available: true,
|
||||
reason: None,
|
||||
items: rows
|
||||
.iter()
|
||||
.map(|r| HypertableInfo {
|
||||
name: r.get(0),
|
||||
schema: r.get(1),
|
||||
num_dimensions: r.get::<_, Option<i64>>(2).unwrap_or(0),
|
||||
compression_enabled: r.get::<_, Option<bool>>(3).unwrap_or(false),
|
||||
num_chunks: r.get::<_, Option<i64>>(4).unwrap_or(0),
|
||||
total_size_bytes: r.get::<_, Option<i64>>(5),
|
||||
})
|
||||
.collect(),
|
||||
})
|
||||
}
|
||||
Some(DbHandle::Sqlite(_)) | Some(DbHandle::MySql(_)) => Ok(HypertableListResponse {
|
||||
available: false,
|
||||
reason: Some("Hypertables are a TimescaleDB (PostgreSQL) feature".into()),
|
||||
items: vec![],
|
||||
}),
|
||||
None => Err("Connection not found".into()),
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn get_indexes(
|
||||
connection_id: String,
|
||||
@@ -3637,3 +3689,7 @@ mod tests {
|
||||
assert!(!pg_dump_available_at("/nonexistent/pg_dump_999999"));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "db_viewer.test.rs"]
|
||||
mod db_viewer_extra_tests;
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
use crate::db::pool::{ConnectionPoolManager, DbHandle};
|
||||
|
||||
#[test]
|
||||
fn hypertables_query_targets_timescale_information_schema() {
|
||||
let q = crate::db::introspection::pg_hypertables_query("public");
|
||||
assert!(q.contains("timescaledb_information.hypertables"));
|
||||
assert!(q.contains("hypertable_schema = $1"));
|
||||
assert!(q.contains("timescaledb_information.chunks"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn timescale_detect_query_checks_pg_extension() {
|
||||
let q = crate::db::introspection::pg_timescale_detect_query();
|
||||
assert!(q.contains("pg_extension"));
|
||||
assert!(q.contains("'timescaledb'"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hypertable_response_degrades_gracefully() {
|
||||
let resp = crate::models::db_viewer::HypertableListResponse {
|
||||
available: false,
|
||||
reason: Some("TimescaleDB extension is not installed".into()),
|
||||
items: vec![],
|
||||
};
|
||||
let json = serde_json::to_string(&resp).expect("serializes");
|
||||
assert!(json.contains("\"available\":false"));
|
||||
}
|
||||
|
||||
/// Live test. On a plain PostgreSQL connection (no TimescaleDB) the detection
|
||||
/// helper must report `available: false`, never an error. Follows the exact
|
||||
/// env-var + client-construction convention used by the live PG tests in
|
||||
/// `commands/objects.test.rs`.
|
||||
#[tokio::test]
|
||||
#[ignore]
|
||||
async fn hypertables_unavailable_on_plain_pg() {
|
||||
let h = std::env::var("GRIDLINE_TEST_PG_HOST").expect("set GRIDLINE_TEST_PG_HOST");
|
||||
let p: u16 = std::env::var("GRIDLINE_TEST_PG_PORT")
|
||||
.unwrap_or_else(|_| "5432".into())
|
||||
.parse()
|
||||
.unwrap();
|
||||
let u = std::env::var("GRIDLINE_TEST_PG_USER").expect("set GRIDLINE_TEST_PG_USER");
|
||||
let d = std::env::var("GRIDLINE_TEST_PG_DB").expect("set GRIDLINE_TEST_PG_DB");
|
||||
let pw = std::env::var("GRIDLINE_TEST_PG_PASSWORD").unwrap_or_default();
|
||||
let (client, conn) = tokio_postgres::connect(
|
||||
&format!("host={h} port={p} user={u} dbname={d} password={pw}"),
|
||||
tokio_postgres::NoTls,
|
||||
)
|
||||
.await
|
||||
.expect("connect to test PG");
|
||||
let handle = tokio::spawn(async move {
|
||||
let _ = conn.await;
|
||||
});
|
||||
let mut pm = ConnectionPoolManager::new();
|
||||
let id = "test-pg".to_string();
|
||||
pm.register(&id, DbHandle::Postgresql(client, handle));
|
||||
let pm = tokio::sync::Mutex::new(pm);
|
||||
|
||||
// Exercise the detection helper directly (the command needs `State`, which
|
||||
// is not constructible in tests).
|
||||
let detected = {
|
||||
let mut g = pm.lock().await;
|
||||
match g.get(&id) {
|
||||
Some(DbHandle::Postgresql(c, _)) => {
|
||||
c.query_one(&crate::db::introspection::pg_timescale_detect_query(), &[])
|
||||
.await
|
||||
}
|
||||
_ => panic!("expected a postgresql handle"),
|
||||
}
|
||||
};
|
||||
assert!(detected.is_ok(), "detection query must run on plain PG");
|
||||
let available: bool = detected.unwrap().get(0);
|
||||
assert!(!available, "plain PG must report available=false");
|
||||
}
|
||||
@@ -3,7 +3,7 @@ use crate::store::Store;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::sync::Mutex;
|
||||
|
||||
const VALID_DB_TYPES: [&str; 4] = ["postgresql", "mysql", "sqlite", "redis"];
|
||||
const VALID_DB_TYPES: [&str; 5] = ["postgresql", "mysql", "mariadb", "sqlite", "redis"];
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub(crate) struct ImportRecord {
|
||||
|
||||
@@ -8,6 +8,8 @@ pub mod keychain;
|
||||
pub mod maintenance;
|
||||
pub mod objects;
|
||||
pub mod query;
|
||||
pub mod query_export;
|
||||
pub mod schema_diff;
|
||||
pub mod schema_graph;
|
||||
pub mod settings;
|
||||
pub mod ssh;
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
//! 3. Every query is recorded in the local `query_history` table.
|
||||
|
||||
use crate::db::pool::DbHandle;
|
||||
use crate::models::db_viewer::{ColumnInfo, QueryResult};
|
||||
use crate::models::db_viewer::{ColumnInfo, MultiQueryResult, QueryResult, StatementNotice};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::{Column, Row};
|
||||
use std::time::Instant;
|
||||
@@ -708,6 +708,507 @@ async fn execute_mysql_raw(
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Multiple result sets (execute_query_multi)
|
||||
// ---------------------------------------------------------------------------
|
||||
// Per the spec: statements execute sequentially; SELECT-producing statements
|
||||
// get wrapped pagination (first page per set + total count — per-set paging is
|
||||
// deliberately omitted because re-running a script would re-execute DML);
|
||||
// DML/DDL produce notices; the first error stops the run, prior sets survive.
|
||||
|
||||
const MAX_STATEMENTS: usize = 50;
|
||||
|
||||
pub(crate) enum StatementOutcome {
|
||||
Set(QueryResult),
|
||||
Notice {
|
||||
kind: &'static str,
|
||||
text: String,
|
||||
affected: Option<i64>,
|
||||
},
|
||||
}
|
||||
|
||||
/// Classify a statement by its first significant keyword (execution-time
|
||||
/// classification per spec §4 — NOT in the tokenizer).
|
||||
pub(crate) fn statement_kind(stmt: &str) -> &'static str {
|
||||
let k: String = stmt
|
||||
.trim_start()
|
||||
.chars()
|
||||
.take_while(|c| c.is_ascii_alphabetic())
|
||||
.collect::<String>()
|
||||
.to_lowercase();
|
||||
match k.as_str() {
|
||||
"select" | "with" | "values" | "table" => "select",
|
||||
"insert" | "update" | "delete" => "dml",
|
||||
_ => "ddl",
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a PG CommandComplete tag ("INSERT 0 5" → 5; "CREATE TABLE" → None).
|
||||
pub(crate) fn affected_from_tag(tag: &str) -> Option<i64> {
|
||||
tag.split_whitespace().last()?.parse::<i64>().ok()
|
||||
}
|
||||
|
||||
fn notice_for(
|
||||
idx: usize,
|
||||
kind: &str,
|
||||
text: String,
|
||||
affected: Option<i64>,
|
||||
) -> StatementNotice {
|
||||
StatementNotice {
|
||||
statement_index: idx,
|
||||
kind: kind.to_string(),
|
||||
text,
|
||||
affected,
|
||||
}
|
||||
}
|
||||
|
||||
// ── SQLite walker ─────────────────────────────────────────────────
|
||||
|
||||
pub(crate) fn execute_sqlite_statement(
|
||||
conn: &rusqlite::Connection,
|
||||
stmt: &str,
|
||||
page: i64,
|
||||
page_size: i64,
|
||||
) -> Result<StatementOutcome, String> {
|
||||
match conn.execute(stmt, []) {
|
||||
Ok(n) => Ok(StatementOutcome::Notice {
|
||||
kind: if statement_kind(stmt) == "ddl" { "ddl" } else { "dml" },
|
||||
text: format!("OK — {n} rows affected"),
|
||||
affected: Some(n as i64),
|
||||
}),
|
||||
Err(rusqlite::Error::ExecuteReturnedResults) => {
|
||||
let (columns, all_rows) = execute_sqlite_with_query(conn, stmt)?;
|
||||
let total_rows = all_rows.len() as i64;
|
||||
let rows: Vec<Vec<serde_json::Value>> = all_rows
|
||||
.into_iter()
|
||||
.take(page_size.max(0) as usize)
|
||||
.collect();
|
||||
Ok(StatementOutcome::Set(QueryResult {
|
||||
columns,
|
||||
rows,
|
||||
total_rows,
|
||||
page,
|
||||
page_size,
|
||||
execution_time_ms: None,
|
||||
}))
|
||||
}
|
||||
Err(rusqlite::Error::SqliteFailure(e, _))
|
||||
if e.code == rusqlite::ErrorCode::OperationInterrupted =>
|
||||
{
|
||||
Err("Query cancelled".to_string())
|
||||
}
|
||||
Err(e) => Err(e.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn run_sqlite_statements(
|
||||
conn: &rusqlite::Connection,
|
||||
statements: &[String],
|
||||
page: i64,
|
||||
page_size: i64,
|
||||
result_sets: &mut Vec<QueryResult>,
|
||||
notices: &mut Vec<StatementNotice>,
|
||||
) -> Option<String> {
|
||||
for (idx, stmt) in statements.iter().enumerate() {
|
||||
match execute_sqlite_statement(conn, stmt, page, page_size) {
|
||||
Ok(StatementOutcome::Set(qr)) => result_sets.push(qr),
|
||||
Ok(StatementOutcome::Notice { kind, text, affected }) => {
|
||||
notices.push(notice_for(idx, kind, text, affected))
|
||||
}
|
||||
Err(e) if e == "Query cancelled" => return Some(e),
|
||||
Err(e) => {
|
||||
notices.push(StatementNotice {
|
||||
statement_index: idx,
|
||||
kind: "error".into(),
|
||||
text: e.clone(),
|
||||
affected: None,
|
||||
});
|
||||
return Some(format!("Statement {} failed: {e}", idx + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
// ── PostgreSQL walker ─────────────────────────────────────────────
|
||||
|
||||
pub(crate) async fn execute_pg_statement(
|
||||
client: &tokio_postgres::Client,
|
||||
stmt: &str,
|
||||
page: i64,
|
||||
page_size: i64,
|
||||
) -> Result<StatementOutcome, String> {
|
||||
let trimmed = stmt.trim();
|
||||
let off = (page.saturating_sub(1).max(0)) * page_size;
|
||||
|
||||
// 1. Try wrapped pagination — a parse error here executes nothing.
|
||||
let wrapped_count = format!("SELECT COUNT(*) FROM ({}) AS _gridline_cnt", trimmed);
|
||||
if let Ok(row) = client.query_one(&wrapped_count, &[]).await {
|
||||
let total_rows: i64 = row.get(0);
|
||||
let wrapped_data = format!(
|
||||
"SELECT * FROM ({}) AS _gridline_data LIMIT $1 OFFSET $2",
|
||||
trimmed
|
||||
);
|
||||
let data_rows = client
|
||||
.query(&wrapped_data, &[&page_size, &off])
|
||||
.await
|
||||
.map_err(|e| {
|
||||
if is_pg_cancel_error(&e) {
|
||||
"Query cancelled".to_string()
|
||||
} else {
|
||||
format!("{e}")
|
||||
}
|
||||
})?;
|
||||
let columns: Vec<ColumnInfo> = match data_rows.first() {
|
||||
Some(first) => first
|
||||
.columns()
|
||||
.iter()
|
||||
.map(|c| ColumnInfo {
|
||||
name: c.name().to_string(),
|
||||
data_type: format!("{:?}", c.type_()),
|
||||
is_nullable: true,
|
||||
is_pk: false,
|
||||
is_fk: false,
|
||||
fk_ref: None,
|
||||
default_value: None,
|
||||
editable: true,
|
||||
is_generated: false,
|
||||
})
|
||||
.collect(),
|
||||
None => Vec::new(),
|
||||
};
|
||||
let rows: Vec<Vec<serde_json::Value>> = data_rows
|
||||
.iter()
|
||||
.map(|row| {
|
||||
(0..row.len())
|
||||
.map(|i| crate::commands::db_viewer::pg_value_to_json(row, i))
|
||||
.collect()
|
||||
})
|
||||
.collect();
|
||||
return Ok(StatementOutcome::Set(QueryResult {
|
||||
columns,
|
||||
rows,
|
||||
total_rows,
|
||||
page,
|
||||
page_size,
|
||||
execution_time_ms: None,
|
||||
}));
|
||||
}
|
||||
|
||||
// 2. Raw single-statement execution via simple_query (captures the
|
||||
// CommandComplete tag so DML notices carry affected counts).
|
||||
let messages = client
|
||||
.simple_query(trimmed)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
if is_pg_cancel_error(&e) {
|
||||
"Query cancelled".to_string()
|
||||
} else {
|
||||
crate::commands::db_viewer::pg_error_message(&e)
|
||||
}
|
||||
})?;
|
||||
|
||||
let mut columns: Vec<ColumnInfo> = Vec::new();
|
||||
let mut all_rows: Vec<Vec<serde_json::Value>> = Vec::new();
|
||||
let mut last_tag: Option<String> = None;
|
||||
for msg in messages {
|
||||
match msg {
|
||||
tokio_postgres::SimpleQueryMessage::Row(row) => {
|
||||
if columns.is_empty() {
|
||||
columns = row
|
||||
.columns()
|
||||
.iter()
|
||||
.map(|c| ColumnInfo {
|
||||
name: c.name().to_string(),
|
||||
data_type: "text".to_string(),
|
||||
is_nullable: true,
|
||||
is_pk: false,
|
||||
is_fk: false,
|
||||
fk_ref: None,
|
||||
default_value: None,
|
||||
editable: true,
|
||||
is_generated: false,
|
||||
})
|
||||
.collect();
|
||||
}
|
||||
let values: Vec<serde_json::Value> = (0..row.len())
|
||||
.map(|i| match row.try_get::<usize>(i) {
|
||||
Ok(Some(s)) => serde_json::Value::String(s.to_string()),
|
||||
_ => serde_json::Value::Null,
|
||||
})
|
||||
.collect();
|
||||
all_rows.push(values);
|
||||
}
|
||||
tokio_postgres::SimpleQueryMessage::CommandComplete(tag) => {
|
||||
last_tag = Some(tag.to_string());
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
if all_rows.is_empty() {
|
||||
let tag = last_tag.clone().unwrap_or_else(|| "OK".to_string());
|
||||
let affected = last_tag.as_deref().and_then(affected_from_tag);
|
||||
return Ok(StatementOutcome::Notice {
|
||||
kind: if statement_kind(stmt) == "ddl" { "ddl" } else { "dml" },
|
||||
text: tag,
|
||||
affected,
|
||||
});
|
||||
}
|
||||
|
||||
let total_rows = all_rows.len() as i64;
|
||||
let rows: Vec<Vec<serde_json::Value>> = all_rows
|
||||
.into_iter()
|
||||
.take(page_size.max(0) as usize)
|
||||
.collect();
|
||||
Ok(StatementOutcome::Set(QueryResult {
|
||||
columns,
|
||||
rows,
|
||||
total_rows,
|
||||
page,
|
||||
page_size,
|
||||
execution_time_ms: None,
|
||||
}))
|
||||
}
|
||||
|
||||
pub(crate) async fn run_pg_statements(
|
||||
client: &tokio_postgres::Client,
|
||||
statements: &[String],
|
||||
page: i64,
|
||||
page_size: i64,
|
||||
result_sets: &mut Vec<QueryResult>,
|
||||
notices: &mut Vec<StatementNotice>,
|
||||
) -> Option<String> {
|
||||
for (idx, stmt) in statements.iter().enumerate() {
|
||||
match execute_pg_statement(client, stmt, page, page_size).await {
|
||||
Ok(StatementOutcome::Set(qr)) => result_sets.push(qr),
|
||||
Ok(StatementOutcome::Notice { kind, text, affected }) => {
|
||||
notices.push(notice_for(idx, kind, text, affected))
|
||||
}
|
||||
Err(e) if e == "Query cancelled" => return Some(e),
|
||||
Err(e) => {
|
||||
notices.push(StatementNotice {
|
||||
statement_index: idx,
|
||||
kind: "error".into(),
|
||||
text: e.clone(),
|
||||
affected: None,
|
||||
});
|
||||
return Some(format!("Statement {} failed: {e}", idx + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
// ── MySQL walker ──────────────────────────────────────────────────
|
||||
|
||||
async fn execute_mysql_statement_on(
|
||||
conn: &mut sqlx::mysql::MySqlConnection,
|
||||
stmt: &str,
|
||||
page: i64,
|
||||
page_size: i64,
|
||||
) -> Result<StatementOutcome, String> {
|
||||
let trimmed = stmt.trim();
|
||||
let off = (page.saturating_sub(1).max(0)) * page_size;
|
||||
|
||||
let total_rows: i64 = match sqlx::query_scalar::<_, i64>(&mysql_wrap_count(trimmed))
|
||||
.fetch_one(&mut *conn)
|
||||
.await
|
||||
{
|
||||
Ok(n) => n,
|
||||
Err(e) if is_mysql_cancel_error(&e) => return Err("Query cancelled".to_string()),
|
||||
Err(_) => {
|
||||
return execute_mysql_raw(conn, trimmed, page, page_size, off)
|
||||
.await
|
||||
.map(|qr| {
|
||||
if qr.columns.is_empty() {
|
||||
StatementOutcome::Notice {
|
||||
kind: if statement_kind(stmt) == "ddl" { "ddl" } else { "dml" },
|
||||
text: "OK".to_string(),
|
||||
affected: None,
|
||||
}
|
||||
} else {
|
||||
StatementOutcome::Set(qr)
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
let data_rows = match sqlx::query(&mysql_wrap_data(trimmed))
|
||||
.bind(page_size)
|
||||
.bind(off)
|
||||
.fetch_all(&mut *conn)
|
||||
.await
|
||||
{
|
||||
Ok(rows) => rows,
|
||||
Err(e) if is_mysql_cancel_error(&e) => return Err("Query cancelled".to_string()),
|
||||
Err(e) => return Err(crate::commands::db_viewer::sanitize_error(&format!("{e}"))),
|
||||
};
|
||||
let columns: Vec<ColumnInfo> = match data_rows.first() {
|
||||
Some(first) => first
|
||||
.columns()
|
||||
.iter()
|
||||
.map(|c| ColumnInfo {
|
||||
name: c.name().to_string(),
|
||||
data_type: c.type_info().to_string(),
|
||||
is_nullable: true,
|
||||
is_pk: false,
|
||||
is_fk: false,
|
||||
fk_ref: None,
|
||||
default_value: None,
|
||||
editable: true,
|
||||
is_generated: false,
|
||||
})
|
||||
.collect(),
|
||||
None => Vec::new(),
|
||||
};
|
||||
let rows: Vec<Vec<serde_json::Value>> = data_rows
|
||||
.iter()
|
||||
.map(|r| (0..r.len()).map(|i| mysql_cell_to_json(r, i)).collect())
|
||||
.collect();
|
||||
Ok(StatementOutcome::Set(QueryResult {
|
||||
columns,
|
||||
rows,
|
||||
total_rows,
|
||||
page,
|
||||
page_size,
|
||||
execution_time_ms: None,
|
||||
}))
|
||||
}
|
||||
|
||||
async fn run_mysql_statements(
|
||||
conn: &mut sqlx::mysql::MySqlConnection,
|
||||
statements: &[String],
|
||||
page: i64,
|
||||
page_size: i64,
|
||||
result_sets: &mut Vec<QueryResult>,
|
||||
notices: &mut Vec<StatementNotice>,
|
||||
cancel_registry: &crate::cancel::CancelRegistry,
|
||||
connection_id: &str,
|
||||
) -> Option<String> {
|
||||
let conn_id: i64 = sqlx::query_scalar("SELECT CAST(CONNECTION_ID() AS SIGNED)")
|
||||
.fetch_one(&mut *conn)
|
||||
.await
|
||||
.unwrap_or(-1);
|
||||
cancel_registry.set_mysql_conn_id(connection_id, Some(conn_id));
|
||||
for (idx, stmt) in statements.iter().enumerate() {
|
||||
match execute_mysql_statement_on(conn, stmt, page, page_size).await {
|
||||
Ok(StatementOutcome::Set(qr)) => result_sets.push(qr),
|
||||
Ok(StatementOutcome::Notice { kind, text, affected }) => {
|
||||
notices.push(notice_for(idx, kind, text, affected))
|
||||
}
|
||||
Err(e) if e == "Query cancelled" => {
|
||||
cancel_registry.set_mysql_conn_id(connection_id, None);
|
||||
return Some(e);
|
||||
}
|
||||
Err(e) => {
|
||||
notices.push(StatementNotice {
|
||||
statement_index: idx,
|
||||
kind: "error".into(),
|
||||
text: e.clone(),
|
||||
affected: None,
|
||||
});
|
||||
cancel_registry.set_mysql_conn_id(connection_id, None);
|
||||
return Some(format!("Statement {} failed: {e}", idx + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
cancel_registry.set_mysql_conn_id(connection_id, None);
|
||||
None
|
||||
}
|
||||
|
||||
/// Execute a multi-statement script, returning every result set and notices.
|
||||
/// `execute_query` is untouched; the query workspace calls this instead.
|
||||
#[tauri::command]
|
||||
pub async fn execute_query_multi(
|
||||
connection_id: String,
|
||||
query: String,
|
||||
page: Option<i64>,
|
||||
page_size: Option<i64>,
|
||||
state: State<'_, crate::AppState>,
|
||||
) -> Result<MultiQueryResult, String> {
|
||||
let start = Instant::now();
|
||||
let p = page.unwrap_or(1);
|
||||
let ps = page_size.unwrap_or(50);
|
||||
|
||||
let statements = crate::db::sql_split::split_statements(&query);
|
||||
if statements.is_empty() {
|
||||
return Err("Query cannot be empty".to_string());
|
||||
}
|
||||
if statements.len() > MAX_STATEMENTS {
|
||||
return Err(format!(
|
||||
"Too many statements: {} (max {MAX_STATEMENTS})",
|
||||
statements.len()
|
||||
));
|
||||
}
|
||||
|
||||
let mut result_sets: Vec<QueryResult> = Vec::new();
|
||||
let mut notices: Vec<StatementNotice> = Vec::new();
|
||||
|
||||
let err_text: Option<String>;
|
||||
let mut pm = state.pool_manager.lock().await;
|
||||
match pm.get(&connection_id) {
|
||||
Some(DbHandle::Postgresql(client, _)) => {
|
||||
err_text =
|
||||
run_pg_statements(client, &statements, p, ps, &mut result_sets, &mut notices).await;
|
||||
}
|
||||
Some(DbHandle::Sqlite(conn)) => {
|
||||
err_text = run_sqlite_statements(conn, &statements, p, ps, &mut result_sets, &mut notices);
|
||||
}
|
||||
Some(DbHandle::MySql(pool)) => {
|
||||
let mut conn = pool
|
||||
.acquire()
|
||||
.await
|
||||
.map_err(|e| crate::commands::db_viewer::sanitize_error(&format!("{e}")))?;
|
||||
err_text = run_mysql_statements(
|
||||
&mut *conn,
|
||||
&statements,
|
||||
p,
|
||||
ps,
|
||||
&mut result_sets,
|
||||
&mut notices,
|
||||
&state.cancel_registry,
|
||||
&connection_id,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
None => return Err("Connection not found".to_string()),
|
||||
}
|
||||
|
||||
let elapsed = start.elapsed().as_millis() as i64;
|
||||
let row_count = result_sets.first().map(|q| q.rows.len() as i64).unwrap_or(0);
|
||||
let history_id = Uuid::new_v4().to_string();
|
||||
if let Some(e) = &err_text {
|
||||
insert_history(
|
||||
&state.db_store,
|
||||
&history_id,
|
||||
&connection_id,
|
||||
&query,
|
||||
Some(elapsed),
|
||||
Some(row_count),
|
||||
"error",
|
||||
Some(e.as_str()),
|
||||
);
|
||||
} else {
|
||||
insert_history(
|
||||
&state.db_store,
|
||||
&history_id,
|
||||
&connection_id,
|
||||
&query,
|
||||
Some(elapsed),
|
||||
Some(row_count),
|
||||
"success",
|
||||
None,
|
||||
);
|
||||
}
|
||||
|
||||
Ok(MultiQueryResult {
|
||||
result_sets,
|
||||
notices,
|
||||
execution_time_ms: elapsed,
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Query history commands
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -1378,3 +1879,7 @@ mod tests {
|
||||
assert!(res.unwrap_err().contains("Query cancelled"));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "query.test.rs"]
|
||||
mod query_tests;
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
use super::*;
|
||||
|
||||
// ── statement_kind / affected_from_tag (pure) ──────────────────────
|
||||
|
||||
#[test]
|
||||
fn statement_kind_classifies_by_first_keyword() {
|
||||
assert_eq!(statement_kind("SELECT 1"), "select");
|
||||
assert_eq!(statement_kind(" with c as (select 1) select * from c"), "select");
|
||||
assert_eq!(statement_kind("insert into t values (1)"), "dml");
|
||||
assert_eq!(statement_kind("UPDATE t SET a=1"), "dml");
|
||||
assert_eq!(statement_kind("DELETE FROM t"), "dml");
|
||||
assert_eq!(statement_kind("CREATE TABLE t (a int)"), "ddl");
|
||||
assert_eq!(statement_kind("-- c\nDROP TABLE t"), "ddl");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn affected_from_tag_parses_pg_command_tags() {
|
||||
assert_eq!(affected_from_tag("INSERT 0 5"), Some(5));
|
||||
assert_eq!(affected_from_tag("UPDATE 3"), Some(3));
|
||||
assert_eq!(affected_from_tag("CREATE TABLE"), None);
|
||||
}
|
||||
|
||||
// ── SQLite walker — real in-memory database, no env vars, not ignored ──
|
||||
|
||||
fn mem_conn() -> rusqlite::Connection {
|
||||
let conn = rusqlite::Connection::open_in_memory().expect("open in-memory");
|
||||
conn.execute_batch(
|
||||
"CREATE TABLE t (a INTEGER, b TEXT);
|
||||
INSERT INTO t VALUES (1, 'x'), (2, 'y'), (3, 'z');",
|
||||
)
|
||||
.expect("seed");
|
||||
conn
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sqlite_walker_yields_notice_for_dml_with_affected() {
|
||||
let conn = mem_conn();
|
||||
let out = execute_sqlite_statement(&conn, "INSERT INTO t VALUES (4, 'w')", 1, 50)
|
||||
.expect("statement runs");
|
||||
match out {
|
||||
StatementOutcome::Notice { kind, affected, .. } => {
|
||||
assert_eq!(kind, "dml");
|
||||
assert_eq!(affected, Some(1));
|
||||
}
|
||||
_ => panic!("expected Notice, got Set"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sqlite_walker_yields_set_for_select() {
|
||||
let conn = mem_conn();
|
||||
let out = execute_sqlite_statement(&conn, "SELECT a FROM t ORDER BY a", 1, 2)
|
||||
.expect("statement runs");
|
||||
match out {
|
||||
StatementOutcome::Set(qr) => {
|
||||
assert_eq!(qr.columns.len(), 1);
|
||||
assert_eq!(qr.rows.len(), 2, "page size caps rows");
|
||||
assert_eq!(qr.total_rows, 3, "total counts all rows");
|
||||
}
|
||||
_ => panic!("expected Set, got Notice"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sqlite_walker_notices_ddl() {
|
||||
let conn = mem_conn();
|
||||
let out = execute_sqlite_statement(&conn, "CREATE TABLE u (x INT)", 1, 50).unwrap();
|
||||
match out {
|
||||
StatementOutcome::Notice { kind, .. } => assert_eq!(kind, "ddl"),
|
||||
_ => panic!("expected Notice"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_sqlite_statements_stops_at_first_error_with_prior_sets() {
|
||||
let conn = mem_conn();
|
||||
let statements = crate::db::sql_split::split_statements(
|
||||
"SELECT a FROM t; SELECT * FROM missing; SELECT 1",
|
||||
);
|
||||
let mut sets = Vec::new();
|
||||
let mut notices = Vec::new();
|
||||
let err = run_sqlite_statements(&conn, &statements, 1, 50, &mut sets, &mut notices);
|
||||
assert_eq!(sets.len(), 1, "first set is returned");
|
||||
assert!(err.is_some(), "the failed statement stops the run");
|
||||
}
|
||||
|
||||
/// Live PG walker test — mirror the env/client conventions of the live PG
|
||||
/// tests in `commands/objects.test.rs`. Assert: `SELECT 1; SELECT 2` (split
|
||||
/// via sql_split) produces two Sets via `run_pg_statements`, and
|
||||
/// `UPDATE`-on-missing-table produces a first-error stop with the error
|
||||
/// notice attached.
|
||||
#[tokio::test]
|
||||
#[ignore]
|
||||
async fn pg_multi_statement_live() {
|
||||
let h = std::env::var("GRIDLINE_TEST_PG_HOST").expect("set GRIDLINE_TEST_PG_HOST");
|
||||
let p: u16 = std::env::var("GRIDLINE_TEST_PG_PORT")
|
||||
.unwrap_or_else(|_| "5432".into())
|
||||
.parse()
|
||||
.unwrap();
|
||||
let u = std::env::var("GRIDLINE_TEST_PG_USER").expect("set GRIDLINE_TEST_PG_USER");
|
||||
let d = std::env::var("GRIDLINE_TEST_PG_DB").expect("set GRIDLINE_TEST_PG_DB");
|
||||
let pw = std::env::var("GRIDLINE_TEST_PG_PASSWORD").unwrap_or_default();
|
||||
let (client, conn) = tokio_postgres::connect(
|
||||
&format!("host={h} port={p} user={u} dbname={d} password={pw}"),
|
||||
tokio_postgres::NoTls,
|
||||
)
|
||||
.await
|
||||
.expect("connect to test PG");
|
||||
let handle = tokio::spawn(async move {
|
||||
let _ = conn.await;
|
||||
});
|
||||
|
||||
// Two SELECTs → two Sets, no notices.
|
||||
let stmts = crate::db::sql_split::split_statements("SELECT 1; SELECT 2");
|
||||
let mut sets = vec![];
|
||||
let mut notices = vec![];
|
||||
let err = run_pg_statements(&client, &stmts, 1, 50, &mut sets, &mut notices).await;
|
||||
assert!(err.is_none());
|
||||
assert_eq!(sets.len(), 2);
|
||||
assert!(notices.is_empty());
|
||||
|
||||
// UPDATE on a missing table → first-error stop with an error notice.
|
||||
let bad = crate::db::sql_split::split_statements("UPDATE gridline_missing SET a=1");
|
||||
let mut sets2 = vec![];
|
||||
let mut notices2 = vec![];
|
||||
let err2 = run_pg_statements(&client, &bad, 1, 50, &mut sets2, &mut notices2).await;
|
||||
assert!(err2.is_some(), "missing table must stop the run");
|
||||
assert!(
|
||||
notices2.iter().any(|n| n.kind == "error"),
|
||||
"an error notice must be attached"
|
||||
);
|
||||
|
||||
let _ = handle;
|
||||
}
|
||||
@@ -0,0 +1,539 @@
|
||||
//! Streaming export of a full query result to a local file (spec §4/§5C).
|
||||
//!
|
||||
//! Headless `run_*_export` functions (testable without Tauri State) + one
|
||||
//! thin command that owns the pool lock, the cancel registration, and the
|
||||
//! `export-progress` events. Fail-closed: any error deletes the partial file.
|
||||
|
||||
use crate::db::pool::DbHandle;
|
||||
use crate::models::db_viewer::ColumnInfo;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::{Column, Row};
|
||||
use std::io::Write;
|
||||
use std::path::{Path, PathBuf};
|
||||
use tauri::State;
|
||||
|
||||
const PROGRESS_ROWS: u64 = 5_000;
|
||||
const CURSOR_NAME: &str = "_gridline_export";
|
||||
const CURSOR_FETCH: usize = 1_000;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub enum ExportFormat {
|
||||
Csv,
|
||||
Jsonl,
|
||||
Json,
|
||||
}
|
||||
|
||||
impl ExportFormat {
|
||||
pub fn from_str(s: &str) -> Result<Self, String> {
|
||||
match s {
|
||||
"csv" => Ok(ExportFormat::Csv),
|
||||
"jsonl" => Ok(ExportFormat::Jsonl),
|
||||
"json" => Ok(ExportFormat::Json),
|
||||
other => Err(format!("unsupported export format: {other} (csv | jsonl | json)")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct ExportSummary {
|
||||
pub rows_written: u64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub struct ExportProgressEvent {
|
||||
pub job_id: String,
|
||||
/// "running" | "done" | "error"
|
||||
pub status: String,
|
||||
pub rows: u64,
|
||||
pub error: Option<String>,
|
||||
}
|
||||
|
||||
/// Read-only heuristic mirroring the frontend `isDestructiveQuery` set (spec §2:
|
||||
/// never trust the frontend guard alone).
|
||||
pub fn is_read_only_sql(sql: &str) -> bool {
|
||||
let k: String = sql
|
||||
.trim_start()
|
||||
.chars()
|
||||
.take_while(|c| c.is_ascii_alphabetic())
|
||||
.collect::<String>()
|
||||
.to_uppercase();
|
||||
!matches!(
|
||||
k.as_str(),
|
||||
"INSERT" | "UPDATE" | "DELETE" | "DROP" | "ALTER" | "TRUNCATE" | "CREATE" | "REPLACE"
|
||||
)
|
||||
}
|
||||
|
||||
/// Validate export inputs: single, read-only statement + absolute path.
|
||||
fn validate_export_inputs(sql: &str, path: &Path) -> Result<(), String> {
|
||||
if !path.is_absolute() {
|
||||
return Err("Export path must be absolute".to_string());
|
||||
}
|
||||
if !is_read_only_sql(sql) {
|
||||
return Err("Export accepts read-only queries only (INSERT/UPDATE/DELETE/DROP/ALTER/TRUNCATE/CREATE/REPLACE are rejected)".to_string());
|
||||
}
|
||||
let statements = crate::db::sql_split::split_statements(sql);
|
||||
if statements.len() != 1 {
|
||||
return Err("Export accepts a single query".to_string());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ── Format writers (pure) ─────────────────────────────────────────
|
||||
|
||||
/// Quote one CSV cell with formula-injection protection (spec §2) — mirrors
|
||||
/// the client-side `csvCell` brought to parity in Task 11.
|
||||
pub fn csv_cell(value: &serde_json::Value) -> String {
|
||||
let raw = match value {
|
||||
serde_json::Value::Null => String::new(),
|
||||
serde_json::Value::String(s) => s.clone(),
|
||||
other => other.to_string(),
|
||||
};
|
||||
let guarded = match raw.chars().next() {
|
||||
Some('=') | Some('+') | Some('-') | Some('@') | Some('\t') | Some('\r') => {
|
||||
format!("'{raw}")
|
||||
}
|
||||
_ => raw.clone(),
|
||||
};
|
||||
if guarded.contains(',') || guarded.contains('"') || guarded.contains('\n') || guarded.contains('\r') {
|
||||
format!("\"{}\"", guarded.replace('"', "\"\""))
|
||||
} else {
|
||||
guarded
|
||||
}
|
||||
}
|
||||
|
||||
pub fn csv_line(columns: &[ColumnInfo], row: &[serde_json::Value]) -> String {
|
||||
let mut line = String::new();
|
||||
for (i, _) in columns.iter().enumerate() {
|
||||
if i > 0 {
|
||||
line.push(',');
|
||||
}
|
||||
line.push_str(&csv_cell(row.get(i).unwrap_or(&serde_json::Value::Null)));
|
||||
}
|
||||
line
|
||||
}
|
||||
|
||||
pub fn csv_header(columns: &[ColumnInfo]) -> String {
|
||||
columns
|
||||
.iter()
|
||||
.map(|c| csv_cell(&serde_json::Value::String(c.name.clone())))
|
||||
.collect::<Vec<_>>()
|
||||
.join(",")
|
||||
}
|
||||
|
||||
pub fn jsonl_line(columns: &[ColumnInfo], row: &[serde_json::Value]) -> String {
|
||||
let mut obj = serde_json::Map::new();
|
||||
for (i, c) in columns.iter().enumerate() {
|
||||
obj.insert(c.name.clone(), row.get(i).cloned().unwrap_or(serde_json::Value::Null));
|
||||
}
|
||||
serde_json::to_string(&serde_json::Value::Object(obj)).unwrap_or_default()
|
||||
}
|
||||
|
||||
fn write_row<W: Write>(
|
||||
w: &mut W,
|
||||
format: ExportFormat,
|
||||
columns: &[ColumnInfo],
|
||||
row: &[serde_json::Value],
|
||||
first: bool,
|
||||
) -> std::io::Result<()> {
|
||||
match format {
|
||||
ExportFormat::Csv => writeln!(w, "{}", csv_line(columns, row)),
|
||||
ExportFormat::Jsonl => writeln!(w, "{}", jsonl_line(columns, row)),
|
||||
ExportFormat::Json => {
|
||||
if !first {
|
||||
writeln!(w, ",")?;
|
||||
}
|
||||
write!(w, "{}", jsonl_line(columns, row))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Run the export, deleting the partial file on any failure (fail-closed).
|
||||
fn with_partial_cleanup<W, F>(path: &PathBuf, w: W, f: F) -> Result<ExportSummary, String>
|
||||
where
|
||||
W: Write,
|
||||
F: FnOnce(&mut std::io::BufWriter<W>) -> Result<ExportSummary, String>,
|
||||
{
|
||||
let mut writer = std::io::BufWriter::with_capacity(8 * 1024 * 1024, w);
|
||||
match f(&mut writer) {
|
||||
Ok(summary) => {
|
||||
writer.flush().map_err(|e| e.to_string())?;
|
||||
Ok(summary)
|
||||
}
|
||||
Err(e) => {
|
||||
let _ = std::fs::remove_file(path);
|
||||
Err(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── SQLite (native streaming) ─────────────────────────────────────
|
||||
|
||||
pub fn run_sqlite_export(
|
||||
conn: &rusqlite::Connection,
|
||||
sql: &str,
|
||||
format: ExportFormat,
|
||||
path: &PathBuf,
|
||||
progress: &mut (dyn FnMut(u64) + Send),
|
||||
) -> Result<ExportSummary, String> {
|
||||
validate_export_inputs(sql, path)?;
|
||||
let file = std::fs::File::create(path).map_err(|e| format!("cannot create file: {e}"))?;
|
||||
|
||||
with_partial_cleanup(path, file, |writer| {
|
||||
let mut stmt = conn.prepare(sql).map_err(|e| e.to_string())?;
|
||||
let columns: Vec<ColumnInfo> = (0..stmt.column_count())
|
||||
.map(|i| ColumnInfo {
|
||||
name: stmt.column_name(i).unwrap_or("?").to_string(),
|
||||
data_type: "TEXT".to_string(),
|
||||
is_nullable: true,
|
||||
is_pk: false,
|
||||
is_fk: false,
|
||||
fk_ref: None,
|
||||
default_value: None,
|
||||
editable: true,
|
||||
is_generated: false,
|
||||
})
|
||||
.collect();
|
||||
if format == ExportFormat::Csv {
|
||||
write!(writer, "\u{FEFF}").map_err(|e| e.to_string())?; // UTF-8 BOM
|
||||
writeln!(writer, "{}", csv_header(&columns)).map_err(|e| e.to_string())?;
|
||||
}
|
||||
if format == ExportFormat::Json {
|
||||
write!(writer, "[").map_err(|e| e.to_string())?;
|
||||
}
|
||||
let col_count = columns.len();
|
||||
let mut rows_written: u64 = 0;
|
||||
let mut first = true;
|
||||
let mut rows = stmt.query([]).map_err(|e| e.to_string())?;
|
||||
while let Some(row) = rows.next().map_err(|e| e.to_string())? {
|
||||
let values: Vec<serde_json::Value> = (0..col_count)
|
||||
.map(|i| sqlite_ref_to_json(row, i))
|
||||
.collect();
|
||||
write_row(writer, format, &columns, &values, first).map_err(|e| e.to_string())?;
|
||||
first = false;
|
||||
rows_written += 1;
|
||||
if rows_written % PROGRESS_ROWS == 0 {
|
||||
progress(rows_written);
|
||||
}
|
||||
}
|
||||
if format == ExportFormat::Json {
|
||||
writeln!(writer, "]").map_err(|e| e.to_string())?;
|
||||
}
|
||||
progress(rows_written);
|
||||
Ok(ExportSummary { rows_written })
|
||||
})
|
||||
}
|
||||
|
||||
fn sqlite_ref_to_json(row: &rusqlite::Row, i: usize) -> serde_json::Value {
|
||||
use rusqlite::types::ValueRef;
|
||||
match row.get_ref(i) {
|
||||
Ok(ValueRef::Null) => serde_json::Value::Null,
|
||||
Ok(ValueRef::Integer(v)) => serde_json::json!(v),
|
||||
Ok(ValueRef::Real(v)) => serde_json::json!(v),
|
||||
Ok(ValueRef::Text(v)) => serde_json::Value::String(String::from_utf8_lossy(v).to_string()),
|
||||
Ok(ValueRef::Blob(v)) => serde_json::Value::String(format!("[{}B blob]", v.len())),
|
||||
Err(_) => serde_json::Value::Null,
|
||||
}
|
||||
}
|
||||
|
||||
// ── PostgreSQL (cursor FETCH loop — constant memory, stable order) ──
|
||||
|
||||
pub async fn run_pg_export(
|
||||
client: &tokio_postgres::Client,
|
||||
sql: &str,
|
||||
format: ExportFormat,
|
||||
path: &PathBuf,
|
||||
progress: &mut (dyn FnMut(u64) + Send),
|
||||
) -> Result<ExportSummary, String> {
|
||||
validate_export_inputs(sql, path)?;
|
||||
let file = std::fs::File::create(path).map_err(|e| format!("cannot create file: {e}"))?;
|
||||
let trimmed = sql.trim();
|
||||
let mut writer = std::io::BufWriter::with_capacity(8 * 1024 * 1024, file);
|
||||
|
||||
// LINEAR FORM: run the cursor loop inline; on any error drop the writer
|
||||
// and remove the partial file.
|
||||
let run = async {
|
||||
client.simple_query("BEGIN").await.map_err(|e| format!("export begin failed: {e}"))?;
|
||||
if let Err(e) = client
|
||||
.simple_query(&format!("DECLARE {CURSOR_NAME} CURSOR FOR ({trimmed})"))
|
||||
.await
|
||||
{
|
||||
let _ = client.simple_query("ROLLBACK").await;
|
||||
return Err(format!("export declare failed: {e}"));
|
||||
}
|
||||
let mut columns: Vec<ColumnInfo> = Vec::new();
|
||||
let mut rows_written: u64 = 0;
|
||||
let mut first = true;
|
||||
if format == ExportFormat::Json {
|
||||
write!(writer, "[").map_err(|e| e.to_string())?;
|
||||
}
|
||||
loop {
|
||||
let msgs = client
|
||||
.simple_query(&format!("FETCH FORWARD {CURSOR_FETCH} FROM {CURSOR_NAME}"))
|
||||
.await;
|
||||
let msgs = match msgs {
|
||||
Ok(m) => m,
|
||||
Err(e) => {
|
||||
let _ = client.simple_query("ROLLBACK").await;
|
||||
return Err(format!("export fetch failed: {e}"));
|
||||
}
|
||||
};
|
||||
let mut batch_rows: Vec<Vec<serde_json::Value>> = Vec::new();
|
||||
for msg in msgs {
|
||||
if let tokio_postgres::SimpleQueryMessage::Row(row) = msg {
|
||||
if columns.is_empty() {
|
||||
columns = row
|
||||
.columns()
|
||||
.iter()
|
||||
.map(|c| ColumnInfo {
|
||||
name: c.name().to_string(),
|
||||
data_type: "text".to_string(),
|
||||
is_nullable: true,
|
||||
is_pk: false,
|
||||
is_fk: false,
|
||||
fk_ref: None,
|
||||
default_value: None,
|
||||
editable: true,
|
||||
is_generated: false,
|
||||
})
|
||||
.collect();
|
||||
}
|
||||
// Simple-query protocol returns every value as text; decode
|
||||
// as &str (usize has no FromSql impl) and NULL as None.
|
||||
let values: Vec<serde_json::Value> = (0..row.len())
|
||||
.map(|i| match row.try_get(i) {
|
||||
Ok(Some(s)) => serde_json::Value::String(s.to_string()),
|
||||
_ => serde_json::Value::Null,
|
||||
})
|
||||
.collect();
|
||||
batch_rows.push(values);
|
||||
}
|
||||
}
|
||||
if batch_rows.is_empty() {
|
||||
break;
|
||||
}
|
||||
if format == ExportFormat::Csv && rows_written == 0 {
|
||||
write!(writer, "\u{FEFF}").map_err(|e| e.to_string())?; // UTF-8 BOM
|
||||
writeln!(writer, "{}", csv_header(&columns)).map_err(|e| e.to_string())?;
|
||||
}
|
||||
for row in batch_rows {
|
||||
write_row(&mut writer, format, &columns, &row, first).map_err(|e| e.to_string())?;
|
||||
first = false;
|
||||
rows_written += 1;
|
||||
if rows_written % PROGRESS_ROWS == 0 {
|
||||
progress(rows_written);
|
||||
}
|
||||
}
|
||||
}
|
||||
if format == ExportFormat::Json {
|
||||
writeln!(writer, "]").map_err(|e| e.to_string())?;
|
||||
}
|
||||
let _ = client.simple_query(&format!("CLOSE {CURSOR_NAME}")).await;
|
||||
let _ = client.simple_query("ROLLBACK").await;
|
||||
progress(rows_written);
|
||||
Ok(ExportSummary { rows_written })
|
||||
}
|
||||
.await;
|
||||
|
||||
match run {
|
||||
Ok(summary) => {
|
||||
writer.flush().map_err(|e| e.to_string())?;
|
||||
Ok(summary)
|
||||
}
|
||||
Err(e) => {
|
||||
drop(writer);
|
||||
let _ = std::fs::remove_file(path);
|
||||
Err(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── MySQL (sqlx stream — native streaming) ─────────────────────────
|
||||
|
||||
pub async fn run_mysql_export(
|
||||
conn: &mut sqlx::mysql::MySqlConnection,
|
||||
sql: &str,
|
||||
format: ExportFormat,
|
||||
path: &PathBuf,
|
||||
progress: &mut (dyn FnMut(u64) + Send),
|
||||
) -> Result<ExportSummary, String> {
|
||||
validate_export_inputs(sql, path)?;
|
||||
let file = std::fs::File::create(path).map_err(|e| format!("cannot create file: {e}"))?;
|
||||
use futures_util::TryStreamExt;
|
||||
let mut writer = std::io::BufWriter::with_capacity(8 * 1024 * 1024, file);
|
||||
|
||||
let run = async {
|
||||
let mut stream = sqlx::query(sql).fetch(&mut *conn);
|
||||
let mut columns: Vec<ColumnInfo> = Vec::new();
|
||||
let mut rows_written: u64 = 0;
|
||||
let mut first = true;
|
||||
if format == ExportFormat::Json {
|
||||
write!(writer, "[").map_err(|e| e.to_string())?;
|
||||
}
|
||||
while let Some(row) = stream.try_next().await.map_err(|e| {
|
||||
crate::commands::db_viewer::sanitize_error(&format!("{e}"))
|
||||
})? {
|
||||
if columns.is_empty() {
|
||||
columns = row
|
||||
.columns()
|
||||
.iter()
|
||||
.map(|c| ColumnInfo {
|
||||
name: c.name().to_string(),
|
||||
data_type: c.type_info().to_string(),
|
||||
is_nullable: true,
|
||||
is_pk: false,
|
||||
is_fk: false,
|
||||
fk_ref: None,
|
||||
default_value: None,
|
||||
editable: true,
|
||||
is_generated: false,
|
||||
})
|
||||
.collect();
|
||||
if format == ExportFormat::Csv {
|
||||
write!(writer, "\u{FEFF}").map_err(|e| e.to_string())?; // UTF-8 BOM
|
||||
writeln!(writer, "{}", csv_header(&columns)).map_err(|e| e.to_string())?;
|
||||
}
|
||||
}
|
||||
let values: Vec<serde_json::Value> =
|
||||
(0..row.len()).map(|i| crate::commands::query::mysql_cell_to_json(&row, i)).collect();
|
||||
write_row(&mut writer, format, &columns, &values, first).map_err(|e| e.to_string())?;
|
||||
first = false;
|
||||
rows_written += 1;
|
||||
if rows_written % PROGRESS_ROWS == 0 {
|
||||
progress(rows_written);
|
||||
}
|
||||
}
|
||||
if format == ExportFormat::Json {
|
||||
writeln!(writer, "]").map_err(|e| e.to_string())?;
|
||||
}
|
||||
progress(rows_written);
|
||||
Ok(ExportSummary { rows_written })
|
||||
}
|
||||
.await;
|
||||
|
||||
match run {
|
||||
Ok(summary) => {
|
||||
writer.flush().map_err(|e| e.to_string())?;
|
||||
Ok(summary)
|
||||
}
|
||||
Err(e) => {
|
||||
drop(writer);
|
||||
let _ = std::fs::remove_file(path);
|
||||
Err(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── Dispatcher + thin command ──────────────────────────────────────
|
||||
|
||||
/// Dispatch across engines. The caller (command) owns the pool lock and the
|
||||
/// cancel registration; MySQL callers must hold a dedicated connection.
|
||||
pub(crate) async fn run_export_query(
|
||||
handle: &mut DbHandle,
|
||||
sql: &str,
|
||||
format: ExportFormat,
|
||||
path: &PathBuf,
|
||||
progress: &mut (dyn FnMut(u64) + Send),
|
||||
) -> Result<ExportSummary, String> {
|
||||
match handle {
|
||||
DbHandle::Sqlite(conn) => run_sqlite_export(conn, sql, format, path, progress),
|
||||
DbHandle::Postgresql(client, _) => run_pg_export(client, sql, format, path, progress).await,
|
||||
DbHandle::MySql(pool) => {
|
||||
let mut conn = pool
|
||||
.acquire()
|
||||
.await
|
||||
.map_err(|e| crate::commands::db_viewer::sanitize_error(&format!("{e}")))?;
|
||||
run_mysql_export(&mut *conn, sql, format, path, progress).await
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn export_query_to_file(
|
||||
connection_id: String,
|
||||
sql: String,
|
||||
format: String,
|
||||
path: String,
|
||||
state: State<'_, crate::AppState>,
|
||||
app: tauri::AppHandle,
|
||||
) -> Result<serde_json::Value, String> {
|
||||
use tauri::Emitter;
|
||||
|
||||
let fmt = ExportFormat::from_str(&format)?;
|
||||
let path_buf = PathBuf::from(&path);
|
||||
let job_id = uuid::Uuid::new_v4().to_string();
|
||||
|
||||
let mut last_emit = std::time::Instant::now();
|
||||
let job = job_id.clone();
|
||||
let mut progress_cb = |rows: u64| {
|
||||
if last_emit.elapsed() >= std::time::Duration::from_millis(250) {
|
||||
last_emit = std::time::Instant::now();
|
||||
let _ = app.emit(
|
||||
"export-progress",
|
||||
ExportProgressEvent {
|
||||
job_id: job.clone(),
|
||||
status: "running".into(),
|
||||
rows,
|
||||
error: None,
|
||||
},
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
let mut pm = state.pool_manager.lock().await;
|
||||
let handle = pm
|
||||
.get(&connection_id)
|
||||
.ok_or_else(|| "Connection not found".to_string())?;
|
||||
|
||||
// MySQL: the STREAMING connection must be the one registered for
|
||||
// KILL QUERY cancellation — acquire it once here, register its id,
|
||||
// stream through it, then clear the registration.
|
||||
let result = match handle {
|
||||
DbHandle::MySql(pool) => {
|
||||
let mut conn = pool
|
||||
.acquire()
|
||||
.await
|
||||
.map_err(|e| crate::commands::db_viewer::sanitize_error(&format!("{e}")))?;
|
||||
let conn_id: i64 = sqlx::query_scalar("SELECT CAST(CONNECTION_ID() AS SIGNED)")
|
||||
.fetch_one(&mut *conn)
|
||||
.await
|
||||
.unwrap_or(-1);
|
||||
state.cancel_registry.set_mysql_conn_id(&connection_id, Some(conn_id));
|
||||
let r = run_mysql_export(&mut *conn, &sql, fmt, &path_buf, &mut progress_cb).await;
|
||||
state.cancel_registry.set_mysql_conn_id(&connection_id, None);
|
||||
r
|
||||
}
|
||||
other => run_export_query(other, &sql, fmt, &path_buf, &mut progress_cb).await,
|
||||
};
|
||||
|
||||
match result {
|
||||
Ok(summary) => {
|
||||
let _ = app.emit(
|
||||
"export-progress",
|
||||
ExportProgressEvent {
|
||||
job_id: job_id.clone(),
|
||||
status: "done".into(),
|
||||
rows: summary.rows_written,
|
||||
error: None,
|
||||
},
|
||||
);
|
||||
Ok(serde_json::json!({ "rows_written": summary.rows_written, "path": path }))
|
||||
}
|
||||
Err(e) => {
|
||||
let _ = app.emit(
|
||||
"export-progress",
|
||||
ExportProgressEvent {
|
||||
job_id: job_id.clone(),
|
||||
status: "error".into(),
|
||||
rows: 0,
|
||||
error: Some(e.clone()),
|
||||
},
|
||||
);
|
||||
Err(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "query_export.test.rs"]
|
||||
mod query_export_tests;
|
||||
@@ -0,0 +1,235 @@
|
||||
use super::*;
|
||||
use sqlx::Connection;
|
||||
|
||||
#[test]
|
||||
fn read_only_heuristic_matches_frontend_guard_set() {
|
||||
for sql in ["SELECT 1", " with x as (select 1) select * from x", "-- note\nSELECT 1"] {
|
||||
assert!(is_read_only_sql(sql), "should allow: {sql}");
|
||||
}
|
||||
for sql in [
|
||||
"INSERT INTO t VALUES (1)",
|
||||
"update t set a = 1",
|
||||
"DELETE FROM t",
|
||||
"DROP TABLE t",
|
||||
"ALTER TABLE t ADD c int",
|
||||
"TRUNCATE t",
|
||||
"CREATE TABLE t (a int)",
|
||||
"REPLACE INTO t VALUES (1)",
|
||||
] {
|
||||
assert!(!is_read_only_sql(sql), "should reject: {sql}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn csv_cell_quotes_and_guards_formula_injection() {
|
||||
assert_eq!(csv_cell(&serde_json::Value::String("plain".into())), "plain");
|
||||
assert_eq!(csv_cell(&serde_json::Value::String("a,b".into())), "\"a,b\"");
|
||||
assert_eq!(csv_cell(&serde_json::Value::String("a\"b".into())), "\"a\"\"b\"");
|
||||
assert_eq!(csv_cell(&serde_json::Value::String("a\nb".into())), "\"a\nb\"");
|
||||
assert_eq!(csv_cell(&serde_json::Value::String("=SUM(A1)".into())), "'=SUM(A1)");
|
||||
assert_eq!(csv_cell(&serde_json::Value::String("+x".into())), "'+x");
|
||||
assert_eq!(csv_cell(&serde_json::Value::String("-1".into())), "'-1");
|
||||
assert_eq!(csv_cell(&serde_json::Value::String("@cmd".into())), "'@cmd");
|
||||
assert_eq!(csv_cell(&serde_json::Value::Null), "");
|
||||
assert_eq!(csv_cell(&serde_json::json!(42)), "42");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn csv_line_bom_is_written_by_header_only() {
|
||||
let cols = vec![
|
||||
crate::models::db_viewer::ColumnInfo { name: "a".into(), data_type: "int".into(), is_nullable: true, is_pk: false, is_fk: false, fk_ref: None, default_value: None, editable: true, is_generated: false },
|
||||
crate::models::db_viewer::ColumnInfo { name: "b".into(), data_type: "text".into(), is_nullable: true, is_pk: false, is_fk: false, fk_ref: None, default_value: None, editable: true, is_generated: false },
|
||||
];
|
||||
let line = csv_line(&cols, &[serde_json::json!("1"), serde_json::Value::Null]);
|
||||
assert_eq!(line, "1,");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn jsonl_line_encodes_row_object() {
|
||||
let cols = vec![crate::models::db_viewer::ColumnInfo { name: "a".into(), data_type: "int".into(), is_nullable: true, is_pk: false, is_fk: false, fk_ref: None, default_value: None, editable: true, is_generated: false }];
|
||||
let line = jsonl_line(&cols, &[serde_json::json!("x\"y")]);
|
||||
assert_eq!(line, "{\"a\":\"x\\\"y\"}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sqlite_export_streams_all_rows_with_bom_and_guard() {
|
||||
let conn = rusqlite::Connection::open_in_memory().unwrap();
|
||||
conn.execute_batch(
|
||||
"CREATE TABLE t (a INTEGER, b TEXT);
|
||||
INSERT INTO t VALUES (1, 'plain');
|
||||
INSERT INTO t VALUES (2, '=SUM(A1)');
|
||||
INSERT INTO t VALUES (3, NULL);",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let path = std::env::temp_dir().join(format!("gridline-export-test-{}.csv", std::process::id()));
|
||||
let mut rows_seen = 0u64;
|
||||
let summary = run_sqlite_export(&conn, "SELECT a, b FROM t ORDER BY a", ExportFormat::Csv, &path, &mut |n| rows_seen = n)
|
||||
.expect("export runs");
|
||||
assert_eq!(summary.rows_written, 3);
|
||||
assert_eq!(rows_seen, 3);
|
||||
|
||||
let bytes = std::fs::read(&path).unwrap();
|
||||
assert!(bytes.starts_with(&[0xEF, 0xBB, 0xBF]), "UTF-8 BOM first");
|
||||
let text = String::from_utf8_lossy(&bytes[3..]);
|
||||
let lines: Vec<&str> = text.lines().collect();
|
||||
assert_eq!(lines[0], "a,b");
|
||||
assert_eq!(lines[1], "1,plain");
|
||||
assert_eq!(lines[2], "2,'=SUM(A1)");
|
||||
assert_eq!(lines[3], "3,");
|
||||
let _ = std::fs::remove_file(&path);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sqlite_export_rejects_write_statements_and_multi_statement() {
|
||||
let conn = rusqlite::Connection::open_in_memory().unwrap();
|
||||
let path = std::env::temp_dir().join("gridline-export-should-not-exist.csv");
|
||||
let err = run_sqlite_export(&conn, "INSERT INTO t VALUES (1)", ExportFormat::Csv, &path, &mut |_| {})
|
||||
.expect_err("write SQL must be rejected");
|
||||
assert!(err.contains("read-only"), "got: {err}");
|
||||
let err = run_sqlite_export(&conn, "SELECT 1; SELECT 2", ExportFormat::Csv, &path, &mut |_| {})
|
||||
.expect_err("multi-statement must be rejected");
|
||||
assert!(err.contains("single"), "got: {err}");
|
||||
assert!(!path.exists(), "no partial file may be left behind");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sqlite_export_error_deletes_partial_file() {
|
||||
let conn = rusqlite::Connection::open_in_memory().unwrap();
|
||||
conn.execute_batch("CREATE TABLE t (a INTEGER); INSERT INTO t VALUES (1),(2),(3);").unwrap();
|
||||
let path = std::env::temp_dir().join(format!("gridline-export-partial-{}.csv", std::process::id()));
|
||||
let err = run_sqlite_export(&conn, "SELECT a, b FROM t", ExportFormat::Csv, &path, &mut |_| {})
|
||||
.expect_err("bad column must fail");
|
||||
assert!(!err.is_empty());
|
||||
assert!(!path.exists(), "partial file must be deleted on error");
|
||||
}
|
||||
|
||||
/// Live PG export — mirror the env/client conventions of the live PG tests in
|
||||
/// `commands/objects.test.rs`. Assert a cursor export of a 3-row table
|
||||
/// produces the same 4 CSV lines as the SQLite test (BOM + header + rows).
|
||||
#[tokio::test]
|
||||
#[ignore]
|
||||
async fn pg_export_live() {
|
||||
let h = match std::env::var("GRIDLINE_TEST_PG_HOST") {
|
||||
Ok(v) => v,
|
||||
Err(_) => return,
|
||||
};
|
||||
let p: u16 = std::env::var("GRIDLINE_TEST_PG_PORT")
|
||||
.unwrap_or_else(|_| "5432".into())
|
||||
.parse()
|
||||
.unwrap();
|
||||
let u = std::env::var("GRIDLINE_TEST_PG_USER").expect("set GRIDLINE_TEST_PG_USER");
|
||||
let d = std::env::var("GRIDLINE_TEST_PG_DB").expect("set GRIDLINE_TEST_PG_DB");
|
||||
let pw = std::env::var("GRIDLINE_TEST_PG_PASSWORD").unwrap_or_default();
|
||||
let (client, conn) = tokio_postgres::connect(
|
||||
&format!("host={h} port={p} user={u} dbname={d} password={pw}"),
|
||||
tokio_postgres::NoTls,
|
||||
)
|
||||
.await
|
||||
.expect("connect to test PG");
|
||||
let _conn_task = tokio::spawn(async move {
|
||||
let _ = conn.await;
|
||||
});
|
||||
client
|
||||
.batch_execute(
|
||||
"DROP TABLE IF EXISTS gridline_export_test;
|
||||
CREATE TABLE gridline_export_test (a INTEGER, b TEXT);
|
||||
INSERT INTO gridline_export_test VALUES (1,'plain'),(2,'=SUM(A1)'),(3,NULL);",
|
||||
)
|
||||
.await
|
||||
.expect("setup");
|
||||
|
||||
let path = std::env::temp_dir().join(format!("gridline-export-pg-{}.csv", std::process::id()));
|
||||
let mut rows_seen = 0u64;
|
||||
let summary = run_pg_export(
|
||||
&client,
|
||||
"SELECT a, b FROM gridline_export_test ORDER BY a",
|
||||
ExportFormat::Csv,
|
||||
&path,
|
||||
&mut |n| rows_seen = n,
|
||||
)
|
||||
.await
|
||||
.expect("export runs");
|
||||
assert_eq!(summary.rows_written, 3);
|
||||
assert_eq!(rows_seen, 3);
|
||||
|
||||
let bytes = std::fs::read(&path).unwrap();
|
||||
assert!(bytes.starts_with(&[0xEF, 0xBB, 0xBF]), "UTF-8 BOM first");
|
||||
let text = String::from_utf8_lossy(&bytes[3..]);
|
||||
let lines: Vec<&str> = text.lines().collect();
|
||||
assert_eq!(lines[0], "a,b");
|
||||
assert_eq!(lines[1], "1,plain");
|
||||
assert_eq!(lines[2], "2,'=SUM(A1)");
|
||||
assert_eq!(lines[3], "3,");
|
||||
let _ = std::fs::remove_file(&path);
|
||||
let _ = client.batch_execute("DROP TABLE IF EXISTS gridline_export_test").await;
|
||||
}
|
||||
|
||||
/// Live MySQL export — mirror the env conventions of the MySQL live tests in
|
||||
/// `commands/db_viewer.rs` (GRIDLINE_TEST_MYSQL_*). Same assertions.
|
||||
#[tokio::test]
|
||||
#[ignore]
|
||||
async fn mysql_export_live() {
|
||||
let host = match std::env::var("GRIDLINE_TEST_MYSQL_HOST") {
|
||||
Ok(v) => v,
|
||||
Err(_) => return,
|
||||
};
|
||||
let port: u16 = std::env::var("GRIDLINE_TEST_MYSQL_PORT")
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.unwrap_or(3306);
|
||||
let user = std::env::var("GRIDLINE_TEST_MYSQL_USER").unwrap_or_else(|_| "root".into());
|
||||
let pass = std::env::var("GRIDLINE_TEST_MYSQL_PASS").unwrap_or_default();
|
||||
let db = match std::env::var("GRIDLINE_TEST_MYSQL_DB") {
|
||||
Ok(v) => v,
|
||||
Err(_) => return,
|
||||
};
|
||||
let opts = sqlx::mysql::MySqlConnectOptions::new()
|
||||
.host(&host)
|
||||
.port(port)
|
||||
.username(&user)
|
||||
.password(&pass)
|
||||
.database(&db);
|
||||
let mut conn = sqlx::mysql::MySqlConnection::connect_with(&opts)
|
||||
.await
|
||||
.expect("connect to test MySQL");
|
||||
sqlx::query("DROP TABLE IF EXISTS gridline_export_test")
|
||||
.execute(&mut conn)
|
||||
.await
|
||||
.ok();
|
||||
sqlx::query("CREATE TABLE gridline_export_test (a INT, b TEXT)")
|
||||
.execute(&mut conn)
|
||||
.await
|
||||
.expect("create");
|
||||
sqlx::query("INSERT INTO gridline_export_test VALUES (1,'plain'),(2,'=SUM(A1)'),(3,NULL)")
|
||||
.execute(&mut conn)
|
||||
.await
|
||||
.expect("insert");
|
||||
|
||||
let path = std::env::temp_dir().join(format!("gridline-export-mysql-{}.csv", std::process::id()));
|
||||
let mut rows_seen = 0u64;
|
||||
let summary = run_mysql_export(
|
||||
&mut conn,
|
||||
"SELECT a, b FROM gridline_export_test ORDER BY a",
|
||||
ExportFormat::Csv,
|
||||
&path,
|
||||
&mut |n| rows_seen = n,
|
||||
)
|
||||
.await
|
||||
.expect("export runs");
|
||||
assert_eq!(summary.rows_written, 3);
|
||||
assert_eq!(rows_seen, 3);
|
||||
|
||||
let bytes = std::fs::read(&path).unwrap();
|
||||
assert!(bytes.starts_with(&[0xEF, 0xBB, 0xBF]), "UTF-8 BOM first");
|
||||
let text = String::from_utf8_lossy(&bytes[3..]);
|
||||
let lines: Vec<&str> = text.lines().collect();
|
||||
assert_eq!(lines[0], "a,b");
|
||||
assert_eq!(lines[1], "1,plain");
|
||||
assert_eq!(lines[2], "2,'=SUM(A1)");
|
||||
assert_eq!(lines[3], "3,");
|
||||
let _ = std::fs::remove_file(&path);
|
||||
let _ = sqlx::query("DROP TABLE IF EXISTS gridline_export_test")
|
||||
.execute(&mut conn)
|
||||
.await;
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
//! Thin `compare_schemas` command (spec §4: two pool lookups → snapshots →
|
||||
//! diff; errors name the failing side; staging never happens here).
|
||||
|
||||
use crate::models::db_viewer::DiffReport;
|
||||
use tauri::State;
|
||||
|
||||
/// Engine family for diff pairing: mysql and mariadb are one family.
|
||||
pub fn engine_family(db_type: &str) -> String {
|
||||
match db_type {
|
||||
"mysql" | "mariadb" => "mysql".to_string(),
|
||||
"sqlite" => "sqlite".to_string(),
|
||||
"postgresql" => "postgresql".to_string(),
|
||||
other => other.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_same_family(source_db_type: &str, target_db_type: &str) -> Result<(), String> {
|
||||
let (s, t) = (engine_family(source_db_type), engine_family(target_db_type));
|
||||
if s != t {
|
||||
return Err(format!(
|
||||
"Schema diff requires the same engine family — source is {s}, target is {t}"
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn connection_db_type(
|
||||
db_store: &std::sync::Mutex<crate::store::Store>,
|
||||
connection_id: &str,
|
||||
) -> Result<String, String> {
|
||||
let store = db_store.lock().map_err(|e| e.to_string())?;
|
||||
let conns = store.get_connections().map_err(|e| e.to_string())?;
|
||||
conns
|
||||
.iter()
|
||||
.find(|c| c.id == connection_id)
|
||||
.map(|c| c.db_type.clone())
|
||||
.ok_or_else(|| format!("connection {connection_id} not found"))
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn compare_schemas(
|
||||
source_connection_id: String,
|
||||
source_schema: String,
|
||||
target_connection_id: String,
|
||||
target_schema: String,
|
||||
state: State<'_, crate::AppState>,
|
||||
) -> Result<DiffReport, String> {
|
||||
let source_db_type = connection_db_type(&state.db_store, &source_connection_id)
|
||||
.map_err(|e| format!("Source connection failed: {e}"))?;
|
||||
let target_db_type = connection_db_type(&state.db_store, &target_connection_id)
|
||||
.map_err(|e| format!("Target connection failed: {e}"))?;
|
||||
validate_same_family(&source_db_type, &target_db_type)?;
|
||||
|
||||
let mut pm = state.pool_manager.lock().await;
|
||||
|
||||
let source = {
|
||||
let handle = pm
|
||||
.get(&source_connection_id)
|
||||
.ok_or_else(|| "Source connection failed: not connected (open it in the DB viewer first)".to_string())?;
|
||||
crate::db::schema_diff::capture_snapshot(handle, &source_db_type, &source_schema)
|
||||
.await
|
||||
.map_err(|e| format!("Source connection failed: {e}"))?
|
||||
};
|
||||
|
||||
let target = {
|
||||
let handle = pm
|
||||
.get(&target_connection_id)
|
||||
.ok_or_else(|| "Target connection failed: not connected".to_string())?;
|
||||
crate::db::schema_diff::capture_snapshot(handle, &target_db_type, &target_schema)
|
||||
.await
|
||||
.map_err(|e| format!("Target connection failed: {e}"))?
|
||||
};
|
||||
|
||||
let mut report = crate::db::schema_diff::diff_snapshots(&source, &target);
|
||||
report.source_label = format!("{} · {}", report.source_label, source_connection_id);
|
||||
report.target_label = format!("{} · {}", report.target_label, target_connection_id);
|
||||
Ok(report)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "schema_diff.test.rs"]
|
||||
mod schema_diff_tests;
|
||||
@@ -0,0 +1,25 @@
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn engine_family_pairs_mysql_with_mariadb() {
|
||||
assert_eq!(engine_family("mysql"), engine_family("mariadb"));
|
||||
assert_eq!(engine_family("mysql"), "mysql");
|
||||
assert_eq!(engine_family("postgresql"), "postgresql");
|
||||
assert_eq!(engine_family("sqlite"), "sqlite");
|
||||
assert_ne!(engine_family("postgresql"), engine_family("mysql"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn family_check_rejects_cross_engine_pairs() {
|
||||
let err = validate_same_family("postgresql", "mysql").expect_err("must reject");
|
||||
assert!(err.contains("same engine"), "got: {err}");
|
||||
assert!(validate_same_family("mariadb", "mysql").is_ok());
|
||||
assert!(validate_same_family("mysql", "mariadb").is_ok());
|
||||
assert!(validate_same_family("postgresql", "postgresql").is_ok());
|
||||
}
|
||||
|
||||
/// Live end-to-end: register two connections and diff their schemas through
|
||||
/// the real pool path.
|
||||
#[test]
|
||||
#[ignore = "needs the pool manager wiring from the command; run via the UI or the live harness"]
|
||||
fn compare_command_live_pg() {}
|
||||
@@ -107,7 +107,7 @@ fn url_scheme_end(msg: &str, i: usize) -> Option<usize> {
|
||||
pub fn validate_test_input(config: &DbConfig) -> Option<String> {
|
||||
let db_type = config.db_type.to_lowercase();
|
||||
|
||||
let valid_types = ["postgresql", "mysql", "sqlite", "redis"];
|
||||
let valid_types = ["postgresql", "mysql", "mariadb", "sqlite", "redis"];
|
||||
if !valid_types.contains(&db_type.as_str()) {
|
||||
return Some(format!(
|
||||
"unsupported database type: {}. Supported types: {}",
|
||||
@@ -220,6 +220,7 @@ pub async fn test_database_connection(config: &DbConfig, ssh: &SshManager) -> Te
|
||||
let result = match config.db_type.to_lowercase().as_str() {
|
||||
"postgresql" => test_pg_connection(config, ssh).await,
|
||||
"mysql" => test_mysql_connection(config, ssh).await,
|
||||
"mariadb" => test_mysql_connection(config, ssh).await,
|
||||
"sqlite" => test_sqlite_connection(config),
|
||||
"redis" => test_redis_connection(config, ssh).await,
|
||||
other => TestConnectionResult {
|
||||
@@ -724,3 +725,7 @@ mod tests {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "test_connection.test.rs"]
|
||||
mod test_connection_tests;
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
use super::*;
|
||||
use crate::commands::ssh::{Ssh2Backend, SshTunnelManager};
|
||||
use crate::db::pool::DbConfig;
|
||||
use std::sync::Arc;
|
||||
|
||||
fn mariadb_config(port: Option<i64>) -> DbConfig {
|
||||
DbConfig {
|
||||
db_type: "mariadb".into(),
|
||||
host: "127.0.0.1".into(),
|
||||
port,
|
||||
username: Some("root".into()),
|
||||
password: None,
|
||||
database: Some("test".into()),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate_test_input_accepts_mariadb_with_port() {
|
||||
assert_eq!(validate_test_input(&mariadb_config(Some(3306))), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validate_test_input_rejects_mariadb_without_port() {
|
||||
assert!(validate_test_input(&mariadb_config(None)).is_some());
|
||||
}
|
||||
|
||||
/// Live test against a real MariaDB server. Run with:
|
||||
/// `GRIDLINE_TEST_MARIADB_HOST=… GRIDLINE_TEST_MARIADB_PORT=3306 \
|
||||
/// GRIDLINE_TEST_MARIADB_USER=… GRIDLINE_TEST_MARIADB_PASSWORD=… \
|
||||
/// GRIDLINE_TEST_MARIADB_DB=… cargo test -- --ignored mariadb_live`
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn mariadb_live_test_connection_reports_mariadb_version() {
|
||||
let config = DbConfig {
|
||||
host: std::env::var("GRIDLINE_TEST_MARIADB_HOST").expect("GRIDLINE_TEST_MARIADB_HOST"),
|
||||
port: std::env::var("GRIDLINE_TEST_MARIADB_PORT")
|
||||
.ok()
|
||||
.and_then(|p| p.parse::<i64>().ok()),
|
||||
username: std::env::var("GRIDLINE_TEST_MARIADB_USER").ok(),
|
||||
password: std::env::var("GRIDLINE_TEST_MARIADB_PASSWORD").ok(),
|
||||
database: std::env::var("GRIDLINE_TEST_MARIADB_DB").ok(),
|
||||
..mariadb_config(None)
|
||||
};
|
||||
let rt = tokio::runtime::Runtime::new().unwrap();
|
||||
let ssh = std::sync::Mutex::new(SshTunnelManager::new(Arc::new(Ssh2Backend)));
|
||||
let result = rt.block_on(async { test_mysql_connection(&config, &ssh).await });
|
||||
assert!(result.ok, "connection failed: {:?}", result.error);
|
||||
let version = result.server_version.unwrap_or_default().to_lowercase();
|
||||
assert!(
|
||||
version.contains("mariadb"),
|
||||
"expected a MariaDB server, got version: {version:?}"
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user