0.8.0: MariaDB + PlanetScale, TimescaleDB, multi-result sets, streaming export, schema diff (#27)
Release / windows (push) Successful in 17m5s
Release / linux (push) Successful in 24m10s

This commit was merged in pull request #27.
This commit is contained in:
2026-09-11 17:20:41 +00:00
parent 60d472dde2
commit 5607773299
74 changed files with 5182 additions and 239 deletions
+2 -1
View File
@@ -1783,12 +1783,13 @@ dependencies = [
[[package]]
name = "gridline"
version = "0.7.15"
version = "0.8.0"
dependencies = [
"bytes",
"chrono",
"core-foundation 0.10.1",
"deadpool-postgres",
"futures-util",
"indexmap 2.14.0",
"redis",
"rusqlite",
+2 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "gridline"
version = "0.7.15"
version = "0.8.0"
description = "An open-source, high-performance database GUI client for PostgreSQL and beyond"
authors = ["you"]
edition = "2021"
@@ -32,6 +32,7 @@ tauri-plugin-fs = "2"
tokio = { version = "1", features = ["full"] }
tokio-postgres = { version = "0.7", features = ["with-chrono-0_4", "with-uuid-1", "with-serde_json-1"] }
bytes = "1"
futures-util = "0.3"
sqlx = { version = "0.8", features = ["runtime-tokio", "mysql", "tls-rustls", "json"] }
redis = { version = "0.27", features = ["tokio-comp"] }
# async-ssh2 is not available at v0.4; using synchronous ssh2 via tokio::task::spawn_blocking per plan's fallback
@@ -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());
}
+59 -3
View File
@@ -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 {
+2
View File
@@ -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;
+506 -1
View File
@@ -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:?}"
);
}
+120
View File
@@ -887,3 +887,123 @@ mod tests {
assert!(sql.contains("$1") && sql.contains("$2"));
}
}
// ---------------------------------------------------------------------------
// TimescaleDB (hypertables)
// ---------------------------------------------------------------------------
/// Detect whether the timescaledb extension is installed on this database.
pub fn pg_timescale_detect_query() -> String {
"SELECT EXISTS (SELECT 1 FROM pg_extension WHERE extname = 'timescaledb')".to_string()
}
/// Query hypertables in a schema (TimescaleDB 2.x information views).
/// Columns: name, schema, num_dimensions, compression_enabled, num_chunks, total_size_bytes.
pub fn pg_hypertables_query(_schema: &str) -> String {
"SELECT h.hypertable_name, h.hypertable_schema, \
h.num_dimensions, \
COALESCE(h.compression_enabled, false), \
(SELECT COUNT(*) FROM timescaledb_information.chunks c \
WHERE c.hypertable_schema = h.hypertable_schema \
AND c.hypertable_name = h.hypertable_name), \
pg_total_relation_size(format('%I.%I', h.hypertable_schema, h.hypertable_name)::regclass)::bigint \
FROM timescaledb_information.hypertables h \
WHERE h.hypertable_schema = $1 \
ORDER BY h.hypertable_name"
.to_string()
}
// ---------------------------------------------------------------------------
// Schema-diff snapshot queries (0.8.0) — one set-based query per object class
// ---------------------------------------------------------------------------
/// Table columns with resolved types, nullability, and defaults.
/// Columns: table_name, column_name, data_type, is_nullable, default_expr
pub fn pg_diff_columns_query() -> String {
"SELECT c.relname, a.attname, \
pg_catalog.format_type(a.atttypid, a.atttypmod), \
NOT a.attnotnull, \
pg_catalog.pg_get_expr(d.adbin, d.adrelid) \
FROM pg_catalog.pg_class c \
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace \
JOIN pg_catalog.pg_attribute a ON a.attrelid = c.oid AND a.attnum > 0 AND NOT a.attisdropped \
LEFT JOIN pg_catalog.pg_attrdef d ON d.adrelid = c.oid AND d.adnum = a.attnum \
WHERE n.nspname = $1 AND c.relkind IN ('r', 'p') \
ORDER BY c.relname, a.attnum"
.to_string()
}
/// Constraints (PK/UNIQUE/CHECK/FK) with exact column lists and FK metadata.
/// Columns: table_name, constraint_name, contype, cols, ref_cols, ref_table, confdeltype, confupdtype, definition
pub fn pg_diff_constraints_query() -> String {
"SELECT cl.relname, con.conname, con.contype::text, \
(SELECT string_agg(a.attname, ', ' ORDER BY k.ord) \
FROM unnest(con.conkey) WITH ORDINALITY AS k(attnum, ord) \
JOIN pg_attribute a ON a.attrelid = con.conrelid AND a.attnum = k.attnum), \
(SELECT string_agg(a.attname, ', ' ORDER BY k.ord) \
FROM unnest(con.confkey) WITH ORDINALITY AS k(attnum, ord) \
JOIN pg_attribute a ON a.attrelid = con.confrelid AND a.attnum = k.attnum), \
(SELECT n2.nspname || '.' || cl2.relname \
FROM pg_class cl2 JOIN pg_namespace n2 ON n2.oid = cl2.relnamespace \
WHERE cl2.oid = con.confrelid), \
con.confdeltype::text, con.confupdtype::text, \
pg_get_constraintdef(con.oid, true) \
FROM pg_catalog.pg_constraint con \
JOIN pg_catalog.pg_class cl ON cl.oid = con.conrelid \
JOIN pg_catalog.pg_namespace n ON n.oid = cl.relnamespace \
WHERE n.nspname = $1 AND con.contype IN ('p', 'u', 'c', 'f') \
ORDER BY cl.relname, con.contype, con.conname"
.to_string()
}
/// Secondary indexes (constraint-backed indexes excluded), with column lists.
/// Columns: table_name, index_name, is_unique, cols
pub fn pg_diff_indexes_query() -> String {
"SELECT t.relname, i.relname, ix.indisunique, \
(SELECT string_agg(a.attname, ', ' ORDER BY k.ord) \
FROM unnest(ix.indkey) WITH ORDINALITY AS k(attnum, ord) \
JOIN pg_attribute a ON a.attrelid = ix.indrelid AND a.attnum = k.attnum \
WHERE k.attnum > 0) \
FROM pg_index ix \
JOIN pg_class i ON i.oid = ix.indexrelid \
JOIN pg_class t ON t.oid = ix.indrelid \
JOIN pg_namespace n ON n.oid = t.relnamespace \
WHERE n.nspname = $1 AND NOT ix.indisprimary \
AND NOT EXISTS (SELECT 1 FROM pg_constraint con WHERE con.conindid = i.oid) \
ORDER BY t.relname, i.relname"
.to_string()
}
/// Views + materialized views. Columns: schema, name, definition, materialized
pub fn pg_diff_views_query() -> String {
"SELECT v.schemaname, v.viewname, v.definition, false \
FROM pg_views v WHERE v.schemaname = $1 \
UNION ALL \
SELECT v.schemaname, v.matviewname, v.definition, true \
FROM pg_matviews v WHERE v.schemaname = $1 \
ORDER BY 2"
.to_string()
}
/// Sequences. Columns: schema, name, start, increment, min, max, cycle
pub fn pg_diff_sequences_query() -> String {
"SELECT sequence_schema, sequence_name, \
COALESCE(start_value::text, '1'), COALESCE(increment::text, '1'), \
COALESCE(minimum_value::text, '1'), COALESCE(maximum_value::text, '9223372036854775807'), \
cycle_option = 'YES' \
FROM information_schema.sequences \
WHERE sequence_schema = $1 \
ORDER BY sequence_name"
.to_string()
}
/// Enums with ordered labels. Columns: schema, name, label (row per label)
pub fn pg_diff_enums_query() -> String {
"SELECT n.nspname, t.typname, e.enumlabel \
FROM pg_type t \
JOIN pg_namespace n ON n.oid = t.typnamespace \
JOIN pg_enum e ON e.enumtypid = t.oid \
WHERE t.typtype = 'e' AND n.nspname = $1 \
ORDER BY t.typname, e.enumsortorder"
.to_string()
}
+2
View File
@@ -1,9 +1,11 @@
pub mod introspection;
pub mod mysql;
pub mod sql_split;
pub mod object_crud;
pub mod object_ddl;
pub mod pool;
pub mod tls;
pub mod schema_diff;
#[allow(unused_imports)]
pub use pool::{ConnectionPoolManager, DbConfig, DbHandle};
+61 -1
View File
@@ -323,4 +323,64 @@ mod tests {
assert_eq!(mysql_default_sort(&["name".into()]), "name");
assert_eq!(mysql_default_sort(&[]), "");
}
}
}
// ── Schema-diff snapshot queries (0.8.0) ──────────────────────────────
/// Columns: table_name, column_name, column_type, is_nullable, column_default
pub fn mysql_snapshot_columns_query(schema: &str) -> String {
format!(
"SELECT table_name, column_name, column_type, is_nullable, \
COALESCE(column_default, '') \
FROM information_schema.columns \
WHERE table_schema = '{}' \
ORDER BY table_name, ordinal_position",
schema.replace('\'', "''")
)
}
/// Constraints (one row per constraint-column). Columns: table_name,
/// constraint_name, constraint_type, column_name, referenced_table_name,
/// delete_rule, update_rule, check_clause
pub fn mysql_snapshot_constraints_query(schema: &str) -> String {
format!(
"SELECT kcu.table_name, kcu.constraint_name, tc.constraint_type, kcu.column_name, \
COALESCE(kcu.referenced_table_name, ''), \
COALESCE(rc.delete_rule, ''), COALESCE(rc.update_rule, ''), \
COALESCE((SELECT cc.check_clause FROM information_schema.check_constraints cc \
WHERE cc.constraint_schema = kcu.constraint_schema \
AND cc.constraint_name = kcu.constraint_name), '') \
FROM information_schema.key_column_usage kcu \
LEFT JOIN information_schema.table_constraints tc \
ON tc.constraint_schema = kcu.constraint_schema \
AND tc.constraint_name = kcu.constraint_name \
AND tc.table_name = kcu.table_name \
LEFT JOIN information_schema.referential_constraints rc \
ON rc.constraint_schema = kcu.constraint_schema \
AND rc.constraint_name = kcu.constraint_name \
WHERE kcu.constraint_schema = '{}' AND tc.constraint_type IS NOT NULL \
ORDER BY kcu.table_name, kcu.constraint_name, kcu.ordinal_position",
schema.replace('\'', "''")
)
}
/// Secondary indexes (one row per index-column). Columns: table_name,
/// index_name, non_unique, column_name
pub fn mysql_snapshot_indexes_query(schema: &str) -> String {
format!(
"SELECT table_name, index_name, non_unique, column_name \
FROM information_schema.statistics \
WHERE table_schema = '{}' AND index_name != 'PRIMARY' \
ORDER BY table_name, index_name, seq_in_index",
schema.replace('\'', "''")
)
}
/// Views. Columns: table_schema, table_name, view_definition
pub fn mysql_snapshot_views_query(schema: &str) -> String {
format!(
"SELECT table_schema, table_name, view_definition \
FROM information_schema.views \
WHERE table_schema = '{}'",
schema.replace('\'', "''")
)
}
+992
View File
@@ -0,0 +1,992 @@
//! Schema snapshot capture + the pure diff engine (engine lands in Task 14).
//!
//! Snapshots are read-only, single-use, and capped (5,000 objects — spec §2).
//! Sync SQL is generated in the TARGET's dialect with identifiers quoted
//! per engine (spec §2: never raw interpolation).
use crate::db::pool::DbHandle;
use crate::models::db_viewer::*;
use std::collections::BTreeMap;
pub(crate) const SNAPSHOT_CAP: usize = 5_000;
/// Map a PG `conf*type` action code to its SQL name.
pub(crate) fn pg_fk_action(code: &str) -> &'static str {
match code {
"r" => "RESTRICT",
"c" => "CASCADE",
"n" => "SET NULL",
"d" => "SET DEFAULT",
_ => "NO ACTION",
}
}
/// Enforce the 5,000-object cap, setting `truncated` (never silent).
pub(crate) fn apply_snapshot_cap(snap: &mut SchemaSnapshot) {
let mut total = snap.tables.len() + snap.views.len() + snap.sequences.len() + snap.enums.len();
let over = total > SNAPSHOT_CAP;
if over {
let mut remaining = SNAPSHOT_CAP;
fn take<T>(v: &mut Vec<T>, budget: &mut usize) {
if v.len() > *budget {
v.truncate(*budget);
}
*budget -= v.len();
}
take(&mut snap.tables, &mut remaining);
take(&mut snap.views, &mut remaining);
take(&mut snap.sequences, &mut remaining);
take(&mut snap.enums, &mut remaining);
total = SNAPSHOT_CAP;
}
snap.object_count = total;
snap.truncated = over;
}
fn csv_names(s: &str) -> Vec<String> {
s.split(", ").map(|p| p.trim().to_string()).filter(|p| !p.is_empty()).collect()
}
/// Capture a PostgreSQL schema snapshot (six set-based queries, one per class).
pub(crate) async fn capture_pg(
client: &tokio_postgres::Client,
engine: &str,
schema: &str,
) -> Result<SchemaSnapshot, String> {
let mut tables: BTreeMap<String, SnapshotTable> = BTreeMap::new();
let mut views: Vec<SnapshotView> = Vec::new();
let mut sequences: Vec<SnapshotSequence> = Vec::new();
let mut enums: BTreeMap<String, SnapshotEnum> = BTreeMap::new();
// Columns
let rows = client
.query(&crate::db::introspection::pg_diff_columns_query(), &[&schema])
.await
.map_err(|e| format!("columns query failed: {e}"))?;
for r in rows {
let tname: String = r.get(0);
let entry = tables.entry(tname.clone()).or_insert_with(|| SnapshotTable {
schema: schema.to_string(),
name: tname,
..Default::default()
});
entry.columns.push(SnapshotColumn {
name: r.get(1),
data_type: r.get(2),
nullable: r.get(3),
default_value: r.get::<_, Option<String>>(4),
});
}
// Constraints (PK / UNIQUE / CHECK / FK)
// Column order: table_name(0), constraint_name(1), contype(2), cols(3),
// ref_cols(4), ref_table(5), confdeltype(6), confupdtype(7), definition(8)
let rows = client
.query(&crate::db::introspection::pg_diff_constraints_query(), &[&schema])
.await
.map_err(|e| format!("constraints query failed: {e}"))?;
for r in rows {
let tname: String = r.get(0);
let cname: String = r.get(1);
let contype: String = r.get(2);
let Some(entry) = tables.get_mut(&tname) else { continue };
match contype.as_str() {
"p" => entry.primary_key = csv_names(&r.get::<_, Option<String>>(3).unwrap_or_default()),
"u" => entry.uniques.push(SnapshotConstraint {
name: cname,
kind: "unique".into(),
definition: r.get::<_, Option<String>>(3).unwrap_or_default(),
}),
"c" => entry.checks.push(SnapshotConstraint {
name: cname,
kind: "check".into(),
definition: r.get::<_, Option<String>>(8).unwrap_or_default(),
}),
"f" => {
let ref_qualified: Option<String> = r.get(5);
let (ref_schema, ref_table) = ref_qualified
.map(|q| match q.split_once('.') {
Some((s, t)) => (s.to_string(), t.to_string()),
None => (String::new(), q.clone()),
})
.unwrap_or_default();
entry.foreign_keys.push(SnapshotFk {
name: cname,
columns: csv_names(&r.get::<_, Option<String>>(3).unwrap_or_default()),
ref_schema,
ref_table,
ref_columns: csv_names(&r.get::<_, Option<String>>(4).unwrap_or_default()),
on_delete: pg_fk_action(&r.get::<_, Option<String>>(6).unwrap_or_default()).to_string(),
on_update: pg_fk_action(&r.get::<_, Option<String>>(7).unwrap_or_default()).to_string(),
});
}
_ => {}
}
}
// Indexes
let rows = client
.query(&crate::db::introspection::pg_diff_indexes_query(), &[&schema])
.await
.map_err(|e| format!("indexes query failed: {e}"))?;
for r in rows {
let tname: String = r.get(0);
let Some(entry) = tables.get_mut(&tname) else { continue };
entry.indexes.push(SnapshotIndex {
name: r.get(1),
columns: csv_names(&r.get::<_, Option<String>>(3).unwrap_or_default()),
unique: r.get(2),
});
}
// Views + matviews
let rows = client
.query(&crate::db::introspection::pg_diff_views_query(), &[&schema])
.await
.map_err(|e| format!("views query failed: {e}"))?;
for r in rows {
views.push(SnapshotView {
schema: r.get(0),
name: r.get(1),
definition: r.get(2),
materialized: r.get(3),
});
}
// Sequences
let rows = client
.query(&crate::db::introspection::pg_diff_sequences_query(), &[&schema])
.await
.map_err(|e| format!("sequences query failed: {e}"))?;
for r in rows {
sequences.push(SnapshotSequence {
schema: r.get(0),
name: r.get(1),
start: r.get(2),
increment: r.get(3),
minimum: r.get(4),
maximum: r.get(5),
cycle: r.get(6),
});
}
// Enums (one row per label — aggregate by name)
let rows = client
.query(&crate::db::introspection::pg_diff_enums_query(), &[&schema])
.await
.map_err(|e| format!("enums query failed: {e}"))?;
for r in rows {
let ename: String = r.get(1);
let entry = enums.entry(ename.clone()).or_insert_with(|| SnapshotEnum {
schema: r.get(0),
name: ename,
labels: Vec::new(),
});
entry.labels.push(r.get(2));
}
let mut snap = SchemaSnapshot {
engine: engine.to_string(),
schema: schema.to_string(),
tables: tables.into_values().collect(),
views,
sequences,
enums: enums.into_values().collect(),
object_count: 0,
truncated: false,
};
apply_snapshot_cap(&mut snap);
Ok(snap)
}
/// Capture a SQLite (in-file/in-memory) snapshot.
pub(crate) fn capture_sqlite(conn: &rusqlite::Connection) -> Result<SchemaSnapshot, String> {
crate::db::schema_diff::capture::sqlite(conn)
}
/// Capture a MySQL/MariaDB snapshot.
pub(crate) async fn capture_mysql(
conn: &mut sqlx::mysql::MySqlConnection,
engine: &str,
schema: &str,
) -> Result<SchemaSnapshot, String> {
crate::db::schema_diff::capture::mysql(conn, engine, schema).await
}
/// Dispatch a snapshot capture across engines. `engine` is the CONNECTION's
/// db_type (mariadb vs mysql matters for the diff dialect).
pub(crate) async fn capture_snapshot(
handle: &mut DbHandle,
engine: &str,
schema: &str,
) -> Result<SchemaSnapshot, String> {
match handle {
DbHandle::Postgresql(client, _) => capture_pg(client, engine, schema).await,
DbHandle::MySql(pool) => {
let mut conn = pool
.acquire()
.await
.map_err(|e| format!("source/target connection failed: {e}"))?;
capture_mysql(&mut *conn, engine, schema).await
}
DbHandle::Sqlite(conn) => capture_sqlite(conn),
}
}
pub(crate) mod capture {
use super::*;
pub(crate) fn sqlite(conn: &rusqlite::Connection) -> Result<SchemaSnapshot, String> {
let mut snap = SchemaSnapshot {
engine: "sqlite".into(),
schema: "main".into(),
..Default::default()
};
let mut stmt = conn
.prepare("SELECT type, name, sql FROM sqlite_master WHERE name NOT LIKE 'sqlite_%' AND type IN ('table', 'view') ORDER BY name")
.map_err(|e| e.to_string())?;
let objects: Vec<(String, String, Option<String>)> = stmt
.query_map([], |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?)))
.map_err(|e| e.to_string())?
.filter_map(|r| r.ok())
.collect();
for (otype, name, sql) in objects {
if otype == "view" {
snap.views.push(SnapshotView {
schema: "main".into(),
name,
definition: sql.unwrap_or_default(),
materialized: false,
});
continue;
}
let mut table = SnapshotTable {
schema: "main".into(),
name: name.clone(),
..Default::default()
};
// Columns + PK + defaults
let mut cols = conn
.prepare(&format!("PRAGMA table_info({})", quote_double(&name)))
.map_err(|e| e.to_string())?;
let col_rows: Vec<(String, String, i64, Option<String>, i64)> = cols
.query_map([], |r| Ok((r.get(1)?, r.get(2)?, r.get(3)?, r.get(4)?, r.get(5)?)))
.map_err(|e| e.to_string())?
.filter_map(|r| r.ok())
.collect();
drop(cols);
for (cname, ctype, notnull, default, pk) in col_rows {
if pk > 0 {
table.primary_key.push(cname.clone());
}
table.columns.push(SnapshotColumn {
name: cname,
data_type: ctype,
nullable: notnull == 0,
default_value: default,
});
}
// Indexes (skip the PK-mandated sqlite_autoindex_*)
let mut idx = conn
.prepare(&format!("PRAGMA index_list({})", quote_double(&name)))
.map_err(|e| e.to_string())?;
let idx_rows: Vec<(String, i64)> = idx
.query_map([], |r| Ok((r.get(1)?, r.get(2)?)))
.map_err(|e| e.to_string())?
.filter_map(|r| r.ok())
.collect();
drop(idx);
for (iname, unique) in idx_rows {
if iname.starts_with("sqlite_autoindex") {
continue;
}
let mut info = conn
.prepare(&format!("PRAGMA index_info({})", quote_double(&iname)))
.map_err(|e| e.to_string())?;
let icols: Vec<String> = info
.query_map([], |r| r.get::<_, String>(2))
.map_err(|e| e.to_string())?
.filter_map(|r| r.ok())
.collect();
drop(info);
table.indexes.push(SnapshotIndex {
name: iname,
columns: icols,
unique: unique != 0,
});
}
// Foreign keys
let mut fks = conn
.prepare(&format!("PRAGMA foreign_key_list({})", quote_double(&name)))
.map_err(|e| e.to_string())?;
let fk_rows: Vec<(i64, i64, String, String, String, String, String)> = fks
.query_map([], |r| {
Ok((r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?, r.get(4)?, r.get(5)?, r.get(6)?))
})
.map_err(|e| e.to_string())?
.filter_map(|r| r.ok())
.collect();
drop(fks);
// (id, seq, table, from, to, on_update, on_delete) — group by id
let mut grouped: BTreeMap<i64, SnapshotFk> = BTreeMap::new();
for (id, _seq, reftable, from, to, on_update, on_delete) in fk_rows {
let fk = grouped.entry(id).or_insert_with(|| SnapshotFk {
name: format!("fk_{name}_{id}"),
columns: Vec::new(),
ref_schema: "main".into(),
ref_table: reftable,
ref_columns: Vec::new(),
on_delete: String::new(),
on_update: String::new(),
});
fk.columns.push(from);
if !to.is_empty() {
fk.ref_columns.push(to);
}
if !on_delete.is_empty() {
fk.on_delete = on_delete.to_uppercase();
}
if !on_update.is_empty() {
fk.on_update = on_update.to_uppercase();
}
}
table.foreign_keys = grouped.into_values().collect();
snap.tables.push(table);
}
apply_snapshot_cap(&mut snap);
Ok(snap)
}
pub(crate) async fn mysql(
conn: &mut sqlx::mysql::MySqlConnection,
engine: &str,
schema: &str,
) -> Result<SchemaSnapshot, String> {
use crate::db::mysql::{mysql_row_string, mysql_snapshot_columns_query, mysql_snapshot_constraints_query, mysql_snapshot_indexes_query, mysql_snapshot_views_query};
let mut tables: BTreeMap<String, SnapshotTable> = BTreeMap::new();
let mut views: Vec<SnapshotView> = Vec::new();
let rows = sqlx::query(&mysql_snapshot_columns_query(schema))
.fetch_all(&mut *conn)
.await
.map_err(|e| format!("columns query failed: {e}"))?;
for r in rows {
let tname = mysql_row_string(&r, 0);
let entry = tables.entry(tname.clone()).or_insert_with(|| SnapshotTable {
schema: schema.to_string(),
name: tname,
..Default::default()
});
entry.columns.push(SnapshotColumn {
name: mysql_row_string(&r, 1),
data_type: mysql_row_string(&r, 2),
nullable: mysql_row_string(&r, 3) == "YES",
default_value: (!mysql_row_string(&r, 4).is_empty()).then(|| mysql_row_string(&r, 4)),
});
}
let rows = sqlx::query(&mysql_snapshot_constraints_query(schema))
.fetch_all(&mut *conn)
.await
.map_err(|e| format!("constraints query failed: {e}"))?;
for r in rows {
let tname = mysql_row_string(&r, 0);
let cname = mysql_row_string(&r, 1);
let ctype = mysql_row_string(&r, 2);
let col = mysql_row_string(&r, 3);
let Some(entry) = tables.get_mut(&tname) else { continue };
match ctype.as_str() {
"PRIMARY KEY" => {
if !entry.primary_key.contains(&col) {
entry.primary_key.push(col);
}
}
"UNIQUE" => {
if let Some(u) = entry.uniques.iter_mut().find(|u| u.name == cname) {
u.definition = if u.definition.is_empty() { col.clone() } else { format!("{}, {col}", u.definition) };
} else {
entry.uniques.push(SnapshotConstraint {
name: cname,
kind: "unique".into(),
definition: col,
});
}
}
"FOREIGN KEY" => {
let ref_table = mysql_row_string(&r, 4);
let on_delete = mysql_row_string(&r, 5).to_uppercase();
let on_update = mysql_row_string(&r, 6).to_uppercase();
if let Some(fk) = entry.foreign_keys.iter_mut().find(|f| f.name == cname) {
fk.columns.push(col);
} else {
entry.foreign_keys.push(SnapshotFk {
name: cname,
columns: vec![col],
ref_schema: String::new(),
ref_table,
ref_columns: Vec::new(),
on_delete,
on_update,
});
}
}
"CHECK" => {
if let Some(c) = entry.checks.iter_mut().find(|c| c.name == cname) {
c.definition = format!("{} {}", c.definition, mysql_row_string(&r, 7));
} else {
entry.checks.push(SnapshotConstraint {
name: cname,
kind: "check".into(),
definition: mysql_row_string(&r, 7),
});
}
}
_ => {}
}
}
let rows = sqlx::query(&mysql_snapshot_indexes_query(schema))
.fetch_all(&mut *conn)
.await
.map_err(|e| format!("indexes query failed: {e}"))?;
for r in rows {
let tname = mysql_row_string(&r, 0);
let iname = mysql_row_string(&r, 1);
let unique = mysql_row_string(&r, 2) == "0";
let col = mysql_row_string(&r, 3);
let Some(entry) = tables.get_mut(&tname) else { continue };
if let Some(ix) = entry.indexes.iter_mut().find(|i| i.name == iname) {
ix.columns.push(col);
} else {
entry.indexes.push(SnapshotIndex {
name: iname,
columns: vec![col],
unique,
});
}
}
let rows = sqlx::query(&mysql_snapshot_views_query(schema))
.fetch_all(&mut *conn)
.await
.map_err(|e| format!("views query failed: {e}"))?;
for r in rows {
views.push(SnapshotView {
schema: mysql_row_string(&r, 0),
name: mysql_row_string(&r, 1),
definition: mysql_row_string(&r, 2),
materialized: false,
});
}
let mut snap = SchemaSnapshot {
engine: engine.to_string(),
schema: schema.to_string(),
tables: tables.into_values().collect(),
views,
sequences: Vec::new(),
enums: Vec::new(),
object_count: 0,
truncated: false,
};
apply_snapshot_cap(&mut snap);
Ok(snap)
}
}
/// Double-quote an identifier (PG/SQLite) with embedded-quote doubling.
pub(crate) fn quote_double(name: &str) -> String {
format!("\"{}\"", name.replace('"', "\"\""))
}
// ---------------------------------------------------------------------------
// Pure diff engine (spec §4/§5A)
// ---------------------------------------------------------------------------
// Destructive classification (binding): DROP TABLE/VIEW/INDEX/SEQUENCE/TYPE/
// CONSTRAINT, DROP COLUMN, and ALTER COLUMN TYPE are destructive (copy-only);
// everything else (CREATE, ADD COLUMN, ADD CONSTRAINT, RENAME, SET/DROP
// DEFAULT, SET/DROP NOT NULL) is stageable.
pub(crate) fn engine_family(engine: &str) -> &'static str {
match engine {
"mysql" | "mariadb" => "mysql",
"sqlite" => "sqlite",
_ => "postgresql",
}
}
fn quote(family: &str, name: &str) -> String {
match family {
"mysql" => format!("`{}`", name.replace('`', "``")),
_ => format!("\"{}\"", name.replace('"', "\"\"")),
}
}
fn qualified(family: &str, schema: &str, name: &str) -> String {
match family {
"mysql" => format!("`{}`.`{}`", schema.replace('`', "``"), name.replace('`', "``")),
"sqlite" => quote(family, name),
_ => format!("\"{}\".\"{}\"", schema.replace('"', "\"\""), name.replace('"', "\"\"")),
}
}
fn detail_line(label: &str, old: &str, new: &str) -> DiffDetailLine {
DiffDetailLine { label: label.into(), old: old.into(), new: new.into() }
}
fn item(ty: &str, name: &str, kind: &str, detail: Vec<DiffDetailLine>, sql: Vec<String>, destructive: bool) -> DiffItem {
DiffItem {
object_type: ty.into(),
name: name.into(),
kind: kind.into(),
detail,
sync_sql: sql,
destructive,
}
}
/// Diff two snapshots into a report. Sync SQL is in the TARGET's dialect.
pub fn diff_snapshots(source: &SchemaSnapshot, target: &SchemaSnapshot) -> DiffReport {
let family = engine_family(&target.engine);
let schema = &target.schema;
let mut items: Vec<DiffItem> = Vec::new();
// ── Enums (PG) — must come first (tables may depend on them) ──────────
for (s_enum, t_enum) in enum_pairs(&source.enums, &target.enums) {
match (s_enum, t_enum) {
(Some(s), None) => items.push(item(
"enum", &s.name, "added", vec![],
vec![format!(
"CREATE TYPE {}.{} AS ENUM ({});",
quote(family, &s.schema),
quote(family, &s.name),
s.labels.iter().map(|l| format!("'{}'", l.replace('\'', "''"))).collect::<Vec<_>>().join(", ")
)],
false,
)),
(None, Some(t)) => items.push(item(
"enum", &t.name, "removed", vec![],
vec![format!("DROP TYPE {}.{};", quote(family, &t.schema), quote(family, &t.name))],
true,
)),
(Some(s), Some(t)) => {
let mut added = Vec::new();
let mut removed_or_reordered = false;
for l in &s.labels {
if !t.labels.contains(l) { added.push(l.clone()); }
}
if t.labels.iter().any(|l| !s.labels.contains(l)) || !common_prefix(&s.labels, &t.labels) {
removed_or_reordered = true;
}
if !added.is_empty() {
if removed_or_reordered {
items.push(item("enum", &s.name, "changed", vec![detail_line("labels", &t.labels.join(", "), &s.labels.join(", "))], vec![], true));
} else {
items.push(item(
"enum", &s.name, "changed",
vec![detail_line("added labels", "", &added.join(", "))],
added.iter().map(|l| format!("ALTER TYPE {}.{} ADD VALUE '{}';", quote(family, &s.schema), quote(family, &s.name), l.replace('\'', "''"))).collect(),
false,
));
}
}
}
(None, None) => {}
}
}
// ── Sequences (PG) ─────────────────────────────────────────────────────
for (s_seq, t_seq) in pair_by(&source.sequences, &target.sequences, |s| s.name.clone()) {
match (s_seq, t_seq) {
(Some(s), None) => items.push(item(
"sequence", &s.name, "added", vec![],
vec![format!(
"CREATE SEQUENCE {q} START WITH {start} INCREMENT BY {inc} MINVALUE {min} MAXVALUE {max} {cycle};",
q = qualified(family, schema, &s.name),
start = s.start, inc = s.increment, min = s.minimum, max = s.maximum,
cycle = if s.cycle { "CYCLE" } else { "NO CYCLE" }
)],
false,
)),
(None, Some(t)) => items.push(item(
"sequence", &t.name, "removed", vec![],
vec![format!("DROP SEQUENCE {};", qualified(family, schema, &t.name))],
true,
)),
(Some(s), Some(t)) => {
let mut sql = String::new();
let mut detail = Vec::new();
for (label, sv, tv) in [
("start", &s.start, &t.start),
("increment", &s.increment, &t.increment),
("minimum", &s.minimum, &t.minimum),
("maximum", &s.maximum, &t.maximum),
] {
if sv != tv {
detail.push(detail_line(label, tv, sv));
if !sql.is_empty() { sql.push(' '); }
match label {
"start" => sql.push_str(&format!("START WITH {sv}")),
"increment" => sql.push_str(&format!("INCREMENT BY {sv}")),
"minimum" => sql.push_str(&format!("MINVALUE {sv}")),
"maximum" => sql.push_str(&format!("MAXVALUE {sv}")),
_ => {}
}
}
}
if s.cycle != t.cycle {
detail.push(detail_line("cycle", &t.cycle.to_string(), &s.cycle.to_string()));
if !sql.is_empty() { sql.push(' '); }
sql.push_str(if s.cycle { "CYCLE" } else { "NO CYCLE" });
}
if !sql.is_empty() {
items.push(item("sequence", &s.name, "changed", detail, vec![format!("ALTER SEQUENCE {} {};", qualified(family, schema, &s.name), sql)], false));
}
}
(None, None) => {}
}
}
// ── Tables ─────────────────────────────────────────────────────────────
for (s_t, t_t) in pair_by(&source.tables, &target.tables, |t| t.name.clone()) {
match (s_t, t_t) {
(Some(s), None) => {
let mut defs: Vec<String> = s.columns.iter().map(|c| {
let mut d = format!("{} {}", quote(family, &c.name), c.data_type);
if !c.nullable { d.push_str(" NOT NULL"); }
if let Some(def) = &c.default_value { d.push_str(&format!(" DEFAULT {def}")); }
d
}).collect();
if !s.primary_key.is_empty() {
defs.push(format!("PRIMARY KEY ({})", s.primary_key.iter().map(|p| quote(family, p)).collect::<Vec<_>>().join(", ")));
}
items.push(item(
"table", &s.name, "added", vec![],
vec![format!("CREATE TABLE {} ({});", qualified(family, schema, &s.name), defs.join(", "))],
false,
));
push_constraint_items(&mut items, family, schema, s, "added");
}
(None, Some(t)) => items.push(item(
"table", &t.name, "removed", vec![],
vec![format!("DROP TABLE {};", qualified(family, schema, &t.name))],
true,
)),
(Some(s), Some(t)) => diff_table(&mut items, family, schema, s, t),
(None, None) => {}
}
}
// ── Views ──────────────────────────────────────────────────────────────
for (s_v, t_v) in pair_by(&source.views, &target.views, |v| v.name.clone()) {
match (s_v, t_v) {
(Some(s), None) => items.push(item(
"view", &s.name, "added", vec![],
vec![format!("CREATE {}VIEW {} AS {};", if s.materialized { "MATERIALIZED " } else { "" }, qualified(family, schema, &s.name), s.definition.trim_end_matches(';'))],
false,
)),
(None, Some(t)) => items.push(item(
"view", &t.name, "removed", vec![],
vec![format!("DROP {}VIEW {};", if t.materialized { "MATERIALIZED " } else { "" }, qualified(family, schema, &t.name))],
true,
)),
(Some(s), Some(t)) if s.definition.trim() != t.definition.trim() => {
if family == "postgresql" || family == "mysql" {
if s.materialized || t.materialized {
items.push(item("view", &s.name, "changed", vec![detail_line("definition", &t.definition, &s.definition)],
vec![format!("DROP MATERIALIZED VIEW {q}; CREATE MATERIALIZED VIEW {q} AS {d};", q = qualified(family, schema, &s.name), d = s.definition.trim_end_matches(';'))], true));
} else {
items.push(item("view", &s.name, "changed", vec![detail_line("definition", &t.definition, &s.definition)],
vec![format!("CREATE OR REPLACE VIEW {} AS {};", qualified(family, schema, &s.name), s.definition.trim_end_matches(';'))], false));
}
} else {
items.push(item("view", &s.name, "changed", vec![detail_line("definition", &t.definition, &s.definition)], vec![], true));
}
}
_ => {}
}
}
DiffReport {
items,
source_label: format!("{} ({})", source.schema, source.engine),
target_label: format!("{} ({})", target.schema, target.engine),
truncated: source.truncated || target.truncated,
}
}
/// Items safe to stage: non-destructive AND carrying SQL.
pub fn stageable_items(report: &DiffReport) -> Vec<&DiffItem> {
report.items.iter().filter(|i| !i.destructive && !i.sync_sql.is_empty()).collect()
}
fn common_prefix(source: &[String], target: &[String]) -> bool {
let mut ti = 0usize;
for l in target {
if source.contains(l) {
if ti >= source.len() || &source[ti] != l {
return false;
}
ti += 1;
}
}
true
}
/// Pair two keyed lists into (source, target) options by key.
fn pair_by<'a, T, K: Ord + Clone + Eq>(src: &'a [T], tgt: &'a [T], key: impl Fn(&T) -> K) -> Vec<(Option<&'a T>, Option<&'a T>)> {
use std::collections::BTreeMap;
let mut map: BTreeMap<K, (Option<&T>, Option<&T>)> = BTreeMap::new();
for s in src { map.entry(key(s)).or_insert((None, None)).0 = Some(s); }
for t in tgt { map.entry(key(t)).or_insert((None, None)).1 = Some(t); }
map.into_values().collect()
}
fn enum_pairs<'a>(src: &'a [SnapshotEnum], tgt: &'a [SnapshotEnum]) -> Vec<(Option<&'a SnapshotEnum>, Option<&'a SnapshotEnum>)> {
pair_by(src, tgt, |e| e.name.clone())
}
fn push_constraint_items(items: &mut Vec<DiffItem>, family: &str, schema: &str, table: &SnapshotTable, kind: &str) {
let qtn = qualified(family, schema, &table.name);
for fk in &table.foreign_keys {
items.push(item(
"constraint", &format!("{}.{}", table.name, fk.name), kind, vec![],
vec![constraint_fk_sql(family, schema, &qtn, fk)],
false,
));
}
for u in &table.uniques {
let cols = u.definition.split(", ").map(|c| quote(family, c.trim())).collect::<Vec<_>>().join(", ");
items.push(item(
"constraint", &format!("{}.{}", table.name, u.name), kind, vec![],
vec![format!("ALTER TABLE {qtn} ADD CONSTRAINT {n} UNIQUE ({cols});", n = quote(family, &u.name), cols = cols)],
false,
));
}
for c in &table.checks {
items.push(item(
"constraint", &format!("{}.{}", table.name, c.name), kind, vec![],
vec![format!("ALTER TABLE {qtn} ADD CONSTRAINT {n} CHECK ({d});", n = quote(family, &c.name), d = c.definition.trim())],
false,
));
}
for ix in &table.indexes {
let cols = ix.columns.iter().map(|c| quote(family, c)).collect::<Vec<_>>().join(", ");
items.push(item(
"index", &format!("{}.{}", table.name, ix.name), kind, vec![],
vec![format!("CREATE {u}INDEX {n} ON {qtn} ({cols});", u = if ix.unique { "UNIQUE " } else { "" }, n = quote(family, &ix.name), cols = cols)],
false,
));
}
}
fn constraint_fk_sql(family: &str, _schema: &str, qtn: &str, fk: &SnapshotFk) -> String {
let cols = fk.columns.iter().map(|c| quote(family, c)).collect::<Vec<_>>().join(", ");
let ref_cols = fk.ref_columns.iter().map(|c| quote(family, c)).collect::<Vec<_>>().join(", ");
let ref_qualified = match family {
"mysql" => format!("`{}`", fk.ref_table.replace('`', "``")),
"sqlite" => quote(family, &fk.ref_table),
_ => qualified(family, &fk.ref_schema, &fk.ref_table),
};
format!(
"ALTER TABLE {qtn} ADD CONSTRAINT {n} FOREIGN KEY ({cols}) REFERENCES {r} ({rc}) ON DELETE {od} ON UPDATE {ou};",
n = quote(family, &fk.name), cols = cols, r = ref_qualified, rc = ref_cols,
od = fk.on_delete, ou = fk.on_update
)
}
fn diff_table(items: &mut Vec<DiffItem>, family: &str, schema: &str, s: &SnapshotTable, t: &SnapshotTable) {
let qtn = qualified(family, schema, &s.name);
for (s_c, t_c) in pair_by(&s.columns, &t.columns, |c| c.name.clone()) {
match (s_c, t_c) {
(Some(c), None) => {
let mut sql = format!("ALTER TABLE {qtn} ADD COLUMN {n} {ty}", n = quote(family, &c.name), ty = c.data_type);
if !c.nullable {
if family == "sqlite" && c.default_value.is_none() {
items.push(item("column", &format!("{}.{}", s.name, c.name), "added",
vec![detail_line("nullable", "false", "true (SQLite cannot ADD COLUMN NOT NULL without DEFAULT)")],
vec![format!("{sql};")], false));
continue;
}
sql.push_str(" NOT NULL");
}
if let Some(d) = &c.default_value { sql.push_str(&format!(" DEFAULT {d}")); }
items.push(item("column", &format!("{}.{}", s.name, c.name), "added", vec![], vec![format!("{sql};")], false));
}
(None, Some(c)) => items.push(item(
"column", &format!("{}.{}", s.name, c.name), "removed", vec![],
vec![format!("ALTER TABLE {qtn} DROP COLUMN {n};", n = quote(family, &c.name))],
true,
)),
(Some(sc), Some(tc)) => {
if sc.data_type != tc.data_type {
let sql = match family {
"postgresql" => format!("ALTER TABLE {qtn} ALTER COLUMN {n} TYPE {ty} USING {n}::{ty};", n = quote(family, &sc.name), ty = sc.data_type),
"mysql" => format!("ALTER TABLE {qtn} MODIFY COLUMN {n} {ty}{nn};", n = quote(family, &sc.name), ty = sc.data_type, nn = if sc.nullable { String::new() } else { " NOT NULL".into() }),
_ => String::new(),
};
items.push(item("column", &format!("{}.{}", s.name, sc.name), "changed",
vec![detail_line("type", &tc.data_type, &sc.data_type)],
if sql.is_empty() { vec![] } else { vec![sql] }, true));
}
if sc.nullable != tc.nullable {
let sql = match family {
"postgresql" => format!("ALTER TABLE {qtn} ALTER COLUMN {n} {op};", n = quote(family, &sc.name), op = if sc.nullable { "DROP NOT NULL" } else { "SET NOT NULL" }),
"mysql" => format!("ALTER TABLE {qtn} MODIFY COLUMN {n} {ty}{nn};", n = quote(family, &sc.name), ty = sc.data_type, nn = if sc.nullable { String::new() } else { " NOT NULL".into() }),
_ => String::new(),
};
if !sql.is_empty() {
items.push(item("column", &format!("{}.{}", s.name, sc.name), "changed",
vec![detail_line("nullable", &tc.nullable.to_string(), &sc.nullable.to_string())],
vec![sql], false));
}
}
if sc.default_value != tc.default_value {
let sql = match family {
"postgresql" => match &sc.default_value {
Some(d) => format!("ALTER TABLE {qtn} ALTER COLUMN {n} SET DEFAULT {d};", n = quote(family, &sc.name)),
None => format!("ALTER TABLE {qtn} ALTER COLUMN {n} DROP DEFAULT;", n = quote(family, &sc.name)),
},
"mysql" => match &sc.default_value {
Some(d) => format!("ALTER TABLE {qtn} ALTER COLUMN {n} SET DEFAULT {d};", n = quote(family, &sc.name)),
None => format!("ALTER TABLE {qtn} ALTER COLUMN {n} DROP DEFAULT;", n = quote(family, &sc.name)),
},
_ => String::new(),
};
if !sql.is_empty() {
items.push(item("column", &format!("{}.{}", s.name, sc.name), "changed",
vec![detail_line("default", &tc.default_value.as_deref().unwrap_or_default(), &sc.default_value.clone().unwrap_or_default())],
vec![sql], false));
}
}
}
(None, None) => {}
}
}
if s.primary_key != t.primary_key {
items.push(item("table", &s.name, "changed",
vec![detail_line("primary key", &t.primary_key.join(", "), &s.primary_key.join(", "))],
vec![], true));
}
for (s_c, t_c) in pair_by(&s.uniques, &t.uniques, |c| c.name.clone()) {
push_constraint_delta(items, family, s, "UNIQUE", s_c, t_c);
}
for (s_c, t_c) in pair_by(&s.checks, &t.checks, |c| c.name.clone()) {
push_constraint_delta(items, family, s, "CHECK", s_c, t_c);
}
for (s_f, t_f) in pair_by(&s.foreign_keys, &t.foreign_keys, |f| f.name.clone()) {
let qtn2 = qualified(family, schema, &s.name);
match (s_f, t_f) {
(Some(f), None) => {
if family == "sqlite" {
items.push(item("constraint", &format!("{}.{}", s.name, f.name), "added",
vec![detail_line("note", "", "SQLite cannot add foreign keys to existing tables — table rebuild required")],
vec![], false));
} else {
items.push(item("constraint", &format!("{}.{}", s.name, f.name), "added", vec![],
vec![constraint_fk_sql(family, schema, &qtn2, f)], false));
}
}
(None, Some(f)) => items.push(item(
"constraint", &format!("{}.{}", s.name, f.name), "removed", vec![],
vec![format!("ALTER TABLE {qtn2} DROP CONSTRAINT {n};", n = quote(family, &f.name))],
true,
)),
(Some(sf), Some(tf)) if sf.definition_differs(tf) => {
items.push(item("constraint", &format!("{}.{}", s.name, sf.name), "changed",
vec![detail_line("definition", &tf.on_delete, &sf.on_delete)],
vec![format!("ALTER TABLE {qtn2} DROP CONSTRAINT {n}; ALTER TABLE {qtn2} ADD CONSTRAINT {n2} FOREIGN KEY ({cols}) REFERENCES {r} ({rc}) ON DELETE {od} ON UPDATE {ou};",
n = quote(family, &sf.name), n2 = quote(family, &sf.name),
cols = sf.columns.iter().map(|c| quote(family, c)).collect::<Vec<_>>().join(", "),
r = qualified(family, &sf.ref_schema, &sf.ref_table),
rc = sf.ref_columns.iter().map(|c| quote(family, c)).collect::<Vec<_>>().join(", "),
od = sf.on_delete, ou = sf.on_update)],
true));
}
_ => {}
}
}
for (s_i, t_i) in pair_by(&s.indexes, &t.indexes, |i| i.name.clone()) {
match (s_i, t_i) {
(Some(ix), None) => items.push(item(
"index", &format!("{}.{}", s.name, ix.name), "added", vec![],
vec![format!("CREATE {u}INDEX {n} ON {qtn} ({cols});", u = if ix.unique { "UNIQUE " } else { "" }, n = qualified(family, schema, &ix.name), cols = ix.columns.iter().map(|c| quote(family, c)).collect::<Vec<_>>().join(", "))],
false,
)),
(None, Some(ix)) => items.push(item(
"index", &format!("{}.{}", s.name, ix.name), "removed", vec![],
vec![format!("DROP INDEX {n};", n = quote(family, &ix.name))],
true,
)),
(Some(si), Some(ti)) if si.columns != ti.columns || si.unique != ti.unique => {
items.push(item("index", &format!("{}.{}", s.name, si.name), "changed",
vec![detail_line("columns", &ti.columns.join(", "), &si.columns.join(", "))],
vec![format!("DROP INDEX {n}; CREATE {u}INDEX {n} ON {qtn} ({cols});", n = qualified(family, schema, &si.name), u = if si.unique { "UNIQUE " } else { "" }, cols = si.columns.iter().map(|c| quote(family, c)).collect::<Vec<_>>().join(", "))],
true));
}
_ => {}
}
}
}
fn push_constraint_delta(items: &mut Vec<DiffItem>, family: &str, table: &SnapshotTable, kind: &str,
s_c: Option<&SnapshotConstraint>, t_c: Option<&SnapshotConstraint>) {
let qtn = qualified(family, &table.schema, &table.name);
match (s_c, t_c) {
(Some(c), None) if kind == "UNIQUE" => items.push(item(
"constraint", &format!("{}.{}", table.name, c.name), "added", vec![],
vec![format!("ALTER TABLE {qtn} ADD CONSTRAINT {n} UNIQUE ({cols});", n = quote(family, &c.name), cols = c.definition.split(", ").map(|x| quote(family, x.trim())).collect::<Vec<_>>().join(", "))],
false,
)),
(Some(c), None) if kind == "CHECK" => items.push(item(
"constraint", &format!("{}.{}", table.name, c.name), "added", vec![],
vec![format!("ALTER TABLE {qtn} ADD CONSTRAINT {n} CHECK ({d});", n = quote(family, &c.name), d = c.definition.trim())],
false,
)),
(Some(c), None) if family == "sqlite" => items.push(item(
"constraint", &format!("{}.{}", table.name, c.name), "added",
vec![detail_line("note", "", "SQLite cannot add constraints to existing tables — table rebuild required")],
vec![], false)),
(None, Some(c)) => items.push(item(
"constraint", &format!("{}.{}", table.name, c.name), "removed", vec![],
vec![format!("ALTER TABLE {qtn} DROP CONSTRAINT {n};", n = quote(family, &c.name))],
true,
)),
(Some(sc), Some(tc)) if sc.definition.trim() != tc.definition.trim() => {
let sql = if kind == "UNIQUE" {
format!("ALTER TABLE {qtn} DROP CONSTRAINT {n}; ALTER TABLE {qtn} ADD CONSTRAINT {n} UNIQUE ({cols});",
n = quote(family, &sc.name), cols = sc.definition.split(", ").map(|x| quote(family, x.trim())).collect::<Vec<_>>().join(", "))
} else {
format!("ALTER TABLE {qtn} DROP CONSTRAINT {n}; ALTER TABLE {qtn} ADD CONSTRAINT {n} CHECK ({d});",
n = quote(family, &sc.name), d = sc.definition.trim())
};
items.push(item("constraint", &format!("{}.{}", table.name, sc.name), "changed",
vec![detail_line("definition", &tc.definition, &sc.definition)],
vec![sql], true));
}
_ => {}
}
}
impl SnapshotFk {
fn definition_differs(&self, other: &SnapshotFk) -> bool {
self.columns != other.columns
|| self.ref_table != other.ref_table
|| self.ref_columns != other.ref_columns
|| self.on_delete != other.on_delete
|| self.on_update != other.on_update
}
}
#[cfg(test)]
#[path = "schema_diff.test.rs"]
mod schema_diff_tests;
@@ -0,0 +1,362 @@
use super::*;
// ── Pure helpers (PG constraint row mapping) ────────────────────────
#[test]
fn pg_fk_action_maps_codes() {
assert_eq!(pg_fk_action("a"), "NO ACTION");
assert_eq!(pg_fk_action("r"), "RESTRICT");
assert_eq!(pg_fk_action("c"), "CASCADE");
assert_eq!(pg_fk_action("n"), "SET NULL");
assert_eq!(pg_fk_action("d"), "SET DEFAULT");
assert_eq!(pg_fk_action("x"), "NO ACTION");
}
#[test]
fn snapshot_cap_sets_truncated() {
// 5,001 objects → truncated report, never silent (spec §2)
let mut snap = SchemaSnapshot::default();
snap.engine = "postgresql".into();
snap.schema = "s".into();
for i in 0..5_001 {
snap.tables.push(SnapshotTable {
schema: "s".into(),
name: format!("t{i}"),
columns: vec![],
primary_key: vec![],
uniques: vec![],
checks: vec![],
indexes: vec![],
foreign_keys: vec![],
});
}
apply_snapshot_cap(&mut snap);
assert!(snap.truncated);
assert!(snap.object_count <= 5_000);
assert_eq!(snap.tables.len(), snap.object_count);
}
#[test]
fn query_builders_target_the_right_catalogs() {
let cols = crate::db::introspection::pg_diff_columns_query();
assert!(cols.contains("pg_attribute") && cols.contains("format_type"));
let cons = crate::db::introspection::pg_diff_constraints_query();
assert!(cons.contains("pg_constraint") && cons.contains("contype"));
let idx = crate::db::introspection::pg_diff_indexes_query();
assert!(idx.contains("pg_index") && idx.contains("indisunique"));
let views = crate::db::introspection::pg_diff_views_query();
assert!(views.contains("pg_views") && views.contains("pg_matviews"));
let seqs = crate::db::introspection::pg_diff_sequences_query();
assert!(seqs.contains("information_schema.sequences"));
let enums = crate::db::introspection::pg_diff_enums_query();
assert!(enums.contains("pg_type") && enums.contains("pg_enum"));
}
// ── SQLite capture (in-memory, real, NOT ignored) ──────────────────
#[test]
fn sqlite_capture_reads_pragmas() {
let conn = rusqlite::Connection::open_in_memory().unwrap();
conn.execute_batch(
"CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL DEFAULT 'x', email TEXT);
CREATE UNIQUE INDEX idx_users_email ON users(email);
CREATE TABLE orders (id INTEGER PRIMARY KEY, user_id INTEGER REFERENCES users(id) ON DELETE CASCADE);
CREATE VIEW v_users AS SELECT id, name FROM users;",
)
.unwrap();
let snap = capture_sqlite(&conn).expect("capture");
assert_eq!(snap.engine, "sqlite");
assert_eq!(snap.tables.len(), 2);
let users = snap.tables.iter().find(|t| t.name == "users").unwrap();
assert_eq!(users.primary_key, vec!["id"]);
assert_eq!(users.columns.len(), 3);
assert!(users.columns.iter().any(|c| c.name == "name" && !c.nullable));
assert!(users.columns.iter().any(|c| c.name == "email" && c.nullable));
assert!(users.indexes.iter().any(|i| i.name == "idx_users_email" && i.unique));
let orders = snap.tables.iter().find(|t| t.name == "orders").unwrap();
assert_eq!(orders.foreign_keys.len(), 1);
let fk = &orders.foreign_keys[0];
assert!(fk.ref_table.contains("users"));
assert_eq!(fk.on_delete, "CASCADE");
assert_eq!(snap.views.len(), 1);
assert!(snap.views[0].definition.contains("SELECT"));
}
/// Live PG capture — mirror the env/client conventions of the live PG tests in
/// `commands/objects.test.rs`. Create two diverged tables in a temp schema,
/// assert capture shape (columns with defaults, PK, FK with actions, indexes,
/// views, a sequence, an enum).
#[tokio::test]
#[ignore]
async fn pg_capture_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; });
let schema = "gridline_diff_test";
client
.batch_execute(&format!(
"DROP SCHEMA IF EXISTS {schema} CASCADE; CREATE SCHEMA {schema};"
))
.await
.expect("create temp schema");
// Two diverged tables: one with PK/FK/index/default, one plain.
client
.batch_execute(&format!(
"CREATE TABLE {schema}.parent (id serial PRIMARY KEY, code text UNIQUE);
CREATE TABLE {schema}.child (
id serial PRIMARY KEY,
parent_id integer REFERENCES {schema}.parent(id) ON DELETE CASCADE ON UPDATE RESTRICT,
name text NOT NULL DEFAULT 'x',
email text
);
CREATE INDEX idx_child_email ON {schema}.child(email);
CREATE VIEW {schema}.v_child AS SELECT id, name FROM {schema}.child;
CREATE SEQUENCE {schema}.seq_test START 5 INCREMENT 2;
CREATE TYPE {schema}.mood AS ENUM ('happy', 'sad');"
))
.await
.expect("create objects");
let snap = capture_pg(&client, "postgresql", schema).await.expect("capture");
assert_eq!(snap.engine, "postgresql");
assert_eq!(snap.schema, schema);
assert_eq!(snap.tables.len(), 2);
let child = snap.tables.iter().find(|t| t.name == "child").expect("child table");
assert_eq!(child.primary_key, vec!["id"]);
assert!(child.columns.iter().any(|c| c.name == "name" && !c.nullable && c.default_value.is_some()));
assert!(child.columns.iter().any(|c| c.name == "email" && c.nullable));
assert!(child.indexes.iter().any(|i| i.name == "idx_child_email"));
assert_eq!(child.foreign_keys.len(), 1);
let fk = &child.foreign_keys[0];
assert!(fk.ref_table.contains("parent"));
assert_eq!(fk.on_delete, "CASCADE");
assert_eq!(fk.on_update, "RESTRICT");
let parent = snap.tables.iter().find(|t| t.name == "parent").expect("parent table");
assert!(parent.uniques.iter().any(|u| u.kind == "unique"));
assert!(snap.views.iter().any(|v| v.name == "v_child"));
assert!(snap.sequences.iter().any(|s| s.name == "seq_test" && s.start == "5"));
assert!(snap.enums.iter().any(|e| e.name == "mood" && e.labels == vec!["happy", "sad"]));
client
.batch_execute(&format!("DROP SCHEMA {schema} CASCADE;"))
.await
.expect("drop temp schema");
let _ = handle;
}
// ── Task 14: pure diff engine + sync-SQL generation ─────────────────
fn col(name: &str, ty: &str, nullable: bool, default: Option<&str>) -> SnapshotColumn {
SnapshotColumn {
name: name.into(),
data_type: ty.into(),
nullable,
default_value: default.map(|d| d.into()),
}
}
fn table(name: &str, columns: Vec<SnapshotColumn>) -> SnapshotTable {
SnapshotTable {
schema: "s".into(),
name: name.into(),
columns,
primary_key: vec![],
uniques: vec![],
checks: vec![],
indexes: vec![],
foreign_keys: vec![],
}
}
fn pg_snap(tables: Vec<SnapshotTable>) -> SchemaSnapshot {
let object_count = tables.len();
SchemaSnapshot {
engine: "postgresql".into(),
schema: "s".into(),
tables,
views: vec![],
sequences: vec![],
enums: vec![],
object_count,
truncated: false,
}
}
#[test]
fn added_table_generates_create_with_pk_and_is_stageable() {
let mut src = pg_snap(vec![]);
let mut t = table("users", vec![col("id", "integer", false, None), col("email", "text", true, Some("''"))]);
t.primary_key = vec!["id".into()];
src.tables.push(t);
let tgt = pg_snap(vec![]);
let r = diff_snapshots(&src, &tgt);
let item = r.items.iter().find(|i| i.object_type == "table" && i.name == "users").unwrap();
assert_eq!(item.kind, "added");
assert!(!item.destructive);
assert_eq!(item.sync_sql.len(), 1);
let sql = &item.sync_sql[0];
assert!(sql.starts_with("CREATE TABLE \"s\".\"users\""), "got: {sql}");
assert!(sql.contains("\"id\" integer NOT NULL"));
assert!(sql.contains("\"email\" text DEFAULT ''"));
assert!(sql.contains("PRIMARY KEY (\"id\")"));
}
#[test]
fn removed_table_is_destructive_copy_only() {
let tgt = pg_snap(vec![table("old", vec![col("id", "integer", false, None)])]);
let src = pg_snap(vec![]);
let r = diff_snapshots(&src, &tgt);
let item = r.items.iter().find(|i| i.name == "old").unwrap();
assert_eq!(item.kind, "removed");
assert!(item.destructive);
assert_eq!(item.sync_sql, vec!["DROP TABLE \"s\".\"old\";"]);
}
#[test]
fn column_diffs_classify_exactly_per_spec() {
let mut src = pg_snap(vec![table(
"t",
vec![
col("kept", "int", true, None),
col("added", "int", true, None),
col("retyped", "bigint", true, None),
col("tightened", "int", false, None),
col("defchanged", "int", true, Some("2")),
],
)]);
src.tables[0].name = "t".into();
let tgt = pg_snap(vec![table(
"t",
vec![
col("kept", "int", true, None),
col("gone", "int", true, None),
col("retyped", "int", true, None),
col("tightened", "int", true, None),
col("defchanged", "int", true, Some("1")),
],
)]);
let r = diff_snapshots(&src, &tgt);
let add = r.items.iter().find(|i| i.name == "t.added").unwrap();
assert_eq!((add.kind.as_str(), add.destructive), ("added", false));
assert_eq!(add.sync_sql[0], "ALTER TABLE \"s\".\"t\" ADD COLUMN \"added\" int;");
let gone = r.items.iter().find(|i| i.name == "t.gone").unwrap();
assert_eq!((gone.kind.as_str(), gone.destructive), ("removed", true));
assert_eq!(gone.sync_sql[0], "ALTER TABLE \"s\".\"t\" DROP COLUMN \"gone\";");
let retyped = r.items.iter().find(|i| i.name == "t.retyped").unwrap();
assert!(retyped.destructive, "ALTER COLUMN TYPE is destructive per spec");
assert!(retyped.sync_sql[0].contains("ALTER COLUMN \"retyped\" TYPE bigint"));
let tight = r.items.iter().find(|i| i.name == "t.tightened").unwrap();
assert!(!tight.destructive);
assert!(tight.sync_sql[0].contains("SET NOT NULL"));
let def = r.items.iter().find(|i| i.name == "t.defchanged").unwrap();
assert!(!def.destructive);
assert!(def.sync_sql[0].contains("SET DEFAULT 2"));
}
#[test]
fn constraint_index_fk_view_sequence_enum_diffs() {
let mut src = pg_snap(vec![table("t", vec![col("id", "int", false, None)])]);
let mut tgt = pg_snap(vec![table("t", vec![col("id", "int", false, None)])]);
src.tables[0].foreign_keys.push(SnapshotFk {
name: "fk_t_ref".into(),
columns: vec!["id".into()],
ref_schema: "s".into(),
ref_table: "other".into(),
ref_columns: vec!["id".into()],
on_delete: "CASCADE".into(),
on_update: "NO ACTION".into(),
});
src.tables[0].indexes.push(SnapshotIndex { name: "ix_t".into(), columns: vec!["id".into()], unique: false });
src.views.push(SnapshotView { schema: "s".into(), name: "v".into(), definition: "SELECT 1".into(), materialized: false });
src.sequences.push(SnapshotSequence { schema: "s".into(), name: "seq".into(), start: "1".into(), increment: "1".into(), minimum: "1".into(), maximum: "100".into(), cycle: false });
src.enums.push(SnapshotEnum { schema: "s".into(), name: "color".into(), labels: vec!["red".into(), "green".into()] });
tgt.enums.push(SnapshotEnum { schema: "s".into(), name: "color".into(), labels: vec!["red".into()] });
let r = diff_snapshots(&src, &tgt);
let fk = r.items.iter().find(|i| i.object_type == "constraint" && i.name == "t.fk_t_ref").unwrap();
assert!(!fk.destructive);
assert!(fk.sync_sql[0].contains("ADD CONSTRAINT \"fk_t_ref\" FOREIGN KEY (\"id\")"));
assert!(fk.sync_sql[0].contains("REFERENCES \"s\".\"other\" (\"id\")"));
assert!(fk.sync_sql[0].contains("ON DELETE CASCADE"));
let ix = r.items.iter().find(|i| i.object_type == "index" && i.name == "t.ix_t").unwrap();
assert_eq!(ix.sync_sql[0], "CREATE INDEX \"s\".\"ix_t\" ON \"s\".\"t\" (\"id\");");
let view = r.items.iter().find(|i| i.object_type == "view" && i.name == "v").unwrap();
assert!(view.sync_sql[0].starts_with("CREATE VIEW \"s\".\"v\" AS"));
let seq = r.items.iter().find(|i| i.object_type == "sequence" && i.name == "seq").unwrap();
assert!(seq.sync_sql[0].starts_with("CREATE SEQUENCE \"s\".\"seq\""));
let en = r.items.iter().find(|i| i.object_type == "enum" && i.name == "color").unwrap();
assert!(!en.destructive);
assert!(en.sync_sql[0].contains("ALTER TYPE \"s\".\"color\" ADD VALUE 'green'"));
}
#[test]
fn mysql_family_uses_backticks() {
let mut src = SchemaSnapshot { engine: "mariadb".into(), schema: "app".into(), ..Default::default() };
src.tables.push(SnapshotTable {
schema: "app".into(),
name: "users".into(),
columns: vec![SnapshotColumn { name: "id".into(), data_type: "int".into(), nullable: false, default_value: None }],
primary_key: vec!["id".into()],
uniques: vec![],
checks: vec![],
indexes: vec![],
foreign_keys: vec![],
});
let tgt = SchemaSnapshot { engine: "mysql".into(), schema: "app".into(), ..Default::default() };
let r = diff_snapshots(&src, &tgt);
let item = r.items.iter().find(|i| i.name == "users").unwrap();
assert!(item.sync_sql[0].starts_with("CREATE TABLE `app`.`users`"), "got: {}", item.sync_sql[0]);
}
#[test]
fn sqlite_changed_view_is_destructive_note_only() {
let mut src = SchemaSnapshot { engine: "sqlite".into(), schema: "main".into(), ..Default::default() };
src.views.push(SnapshotView { schema: "main".into(), name: "v".into(), definition: "SELECT 2".into(), materialized: false });
let mut tgt = SchemaSnapshot { engine: "sqlite".into(), schema: "main".into(), ..Default::default() };
tgt.views.push(SnapshotView { schema: "main".into(), name: "v".into(), definition: "SELECT 1".into(), materialized: false });
let r = diff_snapshots(&src, &tgt);
let item = r.items.iter().find(|i| i.object_type == "view").unwrap();
assert_eq!(item.kind, "changed");
assert!(item.destructive);
assert!(item.sync_sql.is_empty(), "SQLite cannot CREATE OR REPLACE a view");
}
#[test]
fn identical_snapshots_diff_empty_and_stageable_filter() {
let snap = pg_snap(vec![table("t", vec![col("id", "int", false, None)])]);
let r = diff_snapshots(&snap, &snap);
assert!(r.items.is_empty());
assert!(stageable_items(&r).is_empty());
let mut src2 = pg_snap(vec![]);
src2.tables.push(table("new", vec![col("id", "int", false, None)]));
src2.tables.push(table("dropme", vec![col("id", "int", false, None)]));
let r2 = diff_snapshots(&src2, &snap);
assert!(stageable_items(&r2).iter().all(|i| !i.destructive && !i.sync_sql.is_empty()));
}
+212
View File
@@ -0,0 +1,212 @@
//! Pure SQL statement splitter shared by multi-result execution and the
//! streaming export (single-statement validation).
//!
//! A hand-rolled state machine (no new dependencies). It understands:
//! single/double/backtick-quoted strings with doubled-quote escapes,
//! `--` line comments, `/* */` block comments, and PostgreSQL dollar-quoted
//! bodies (`$$…$$`, `$tag$…$tag$`). Conservative by design: anything it
//! cannot confidently close is kept inside the current statement rather
//! than split — a naive semicolon split that could execute fragmentary SQL
//! is unacceptable (spec §2).
//!
//! Statement classification (SELECT vs DML vs DDL) happens at execution
//! time, not here.
/// Split `sql` into ordered statement texts. Comment-only and empty
/// fragments are dropped. Statement text keeps its comments verbatim.
pub fn split_statements(sql: &str) -> Vec<String> {
let chars: Vec<char> = sql.chars().collect();
let mut stmts: Vec<String> = Vec::new();
let mut current = String::new();
let mut i = 0usize;
while i < chars.len() {
let c = chars[i];
match c {
'\'' | '"' | '`' => {
copy_quoted(&chars, &mut i, &mut current, c);
}
'-' if peek(&chars, i + 1) == Some('-') => {
copy_until(&chars, &mut i, &mut current, |ch| ch == '\n');
}
'/' if peek(&chars, i + 1) == Some('*') => {
copy_block_comment(&chars, &mut i, &mut current);
}
'$' => {
if let Some(end) = dollar_quote_end(&chars, i) {
for _ in i..end {
if i < chars.len() {
current.push(chars[i]);
i += 1;
}
}
} else {
current.push(c);
i += 1;
}
}
';' => {
if has_executable_text(&current) {
stmts.push(current.trim().to_string());
}
current.clear();
i += 1;
}
_ => {
current.push(c);
i += 1;
}
}
}
if has_executable_text(&current) {
stmts.push(current.trim().to_string());
}
stmts
}
fn peek(chars: &[char], idx: usize) -> Option<char> {
chars.get(idx).copied()
}
/// Copy a quoted region starting at the opening `quote` char. Doubled quotes
/// are escapes and stay inside the region. Unterminated quotes consume the
/// rest of the input (conservative).
fn copy_quoted(chars: &[char], i: &mut usize, out: &mut String, quote: char) {
out.push(chars[*i]);
*i += 1;
while *i < chars.len() {
let c = chars[*i];
out.push(c);
*i += 1;
if c == quote {
if peek(chars, *i) == Some(quote) {
// doubled quote — escaped, keep it and continue
out.push(quote);
*i += 1;
} else {
return;
}
}
}
}
/// Copy characters until `stop` matches (inclusive for `\n` handled by the
/// caller's predicate) or input ends.
fn copy_until(chars: &[char], i: &mut usize, out: &mut String, stop: impl Fn(char) -> bool) {
while *i < chars.len() {
let c = chars[*i];
out.push(c);
*i += 1;
if stop(c) {
return;
}
}
}
/// Copy a `/* … */` block comment (no nesting per the SQL standard).
fn copy_block_comment(chars: &[char], i: &mut usize, out: &mut String) {
out.push(chars[*i]); // '/'
*i += 1;
if *i < chars.len() {
out.push(chars[*i]); // '*'
*i += 1;
}
while *i < chars.len() {
let c = chars[*i];
out.push(c);
*i += 1;
if c == '*' && peek(chars, *i) == Some('/') {
out.push('/');
*i += 1;
return;
}
}
}
/// At `chars[start] == '$'`, try to match `$[A-Za-z_][A-Za-z0-9_]*$` and find
/// the matching closing delimiter. Returns the index one past the closing
/// delimiter, or None if this is not a well-formed (and closable) dollar
/// quote — in which case the `$` is ordinary text.
fn dollar_quote_end(chars: &[char], start: usize) -> Option<usize> {
let mut j = start + 1;
if j < chars.len() && chars[j] == '$' {
// bare $$ body
let tag = String::new();
return find_dollar_close(chars, j + 1, &tag);
}
let mut tag = String::new();
while j < chars.len() && (chars[j].is_ascii_alphanumeric() || chars[j] == '_') {
tag.push(chars[j]);
j += 1;
}
if tag.is_empty() || j >= chars.len() || chars[j] != '$' {
return None; // not $tag$
}
find_dollar_close(chars, j + 1, &tag)
}
fn find_dollar_close(chars: &[char], from: usize, tag: &str) -> Option<usize> {
let closing: Vec<char> = format!("${tag}$").chars().collect();
let mut j = from;
while j + closing.len() <= chars.len() {
if chars[j..j + closing.len()] == closing[..] {
return Some(j + closing.len());
}
j += 1;
}
None
}
/// True if the fragment contains anything other than whitespace and comments
/// (comments are preserved in statement text, but a fragment that is ONLY
/// comments/whitespace is not a statement).
pub fn has_executable_text(stmt: &str) -> bool {
let chars: Vec<char> = stmt.chars().collect();
let mut i = 0usize;
while i < chars.len() {
let c = chars[i];
match c {
'\'' | '"' | '`' => {
let mut probe = String::new();
let mut j = i;
copy_quoted(&chars, &mut j, &mut probe, c);
return true; // a quoted region IS executable text
}
'-' if peek(&chars, i + 1) == Some('-') => {
skip_until(&chars, &mut i, |ch| ch == '\n');
}
'/' if peek(&chars, i + 1) == Some('*') => {
skip_block_comment(&chars, &mut i);
}
c if c.is_whitespace() => i += 1,
_ => return true,
}
}
false
}
fn skip_until(chars: &[char], i: &mut usize, stop: impl Fn(char) -> bool) {
while *i < chars.len() {
let c = chars[*i];
*i += 1;
if stop(c) {
return;
}
}
}
fn skip_block_comment(chars: &[char], i: &mut usize) {
*i += 2; // "/*"
while *i < chars.len() {
if chars[*i] == '*' && peek(chars, *i + 1) == Some('/') {
*i += 2;
return;
}
*i += 1;
}
}
#[cfg(test)]
#[path = "sql_split.test.rs"]
mod sql_split_tests;
@@ -0,0 +1,86 @@
use super::*;
#[test]
fn splits_two_statements() {
assert_eq!(
split_statements("SELECT 1; SELECT 2"),
vec!["SELECT 1", "SELECT 2"]
);
}
#[test]
fn no_trailing_semicolon_is_fine() {
assert_eq!(split_statements("SELECT 1"), vec!["SELECT 1"]);
}
#[test]
fn semicolon_inside_string_is_not_a_boundary() {
assert_eq!(split_statements("SELECT 'a;b'"), vec!["SELECT 'a;b'"]);
assert_eq!(
split_statements("SELECT 'a;b'; SELECT 2"),
vec!["SELECT 'a;b'", "SELECT 2"]
);
}
#[test]
fn doubled_quote_escapes() {
assert_eq!(
split_statements("SELECT 'it''s'; SELECT 2"),
vec!["SELECT 'it''s'", "SELECT 2"]
);
}
#[test]
fn double_quoted_and_backtick_identifiers() {
assert_eq!(
split_statements("SELECT \"a;b\"; SELECT `x;y`"),
vec!["SELECT \"a;b\"", "SELECT `x;y`"]
);
assert_eq!(
split_statements("SELECT `a``b`; SELECT 2"),
vec!["SELECT `a``b`", "SELECT 2"]
);
}
#[test]
fn line_comments_do_not_break_statements() {
assert_eq!(
split_statements("SELECT 1 -- ; not a boundary\n; SELECT 2"),
vec!["SELECT 1 -- ; not a boundary", "SELECT 2"]
);
}
#[test]
fn block_comments_do_not_break_statements() {
assert_eq!(split_statements("SELECT /* ; */ 1"), vec!["SELECT /* ; */ 1"]);
}
#[test]
fn dollar_quoted_bodies() {
assert_eq!(
split_statements("SELECT $$a;b$$; SELECT 2"),
vec!["SELECT $$a;b$$", "SELECT 2"]
);
assert_eq!(
split_statements("SELECT $fn$x;y$fn$; SELECT 2"),
vec!["SELECT $fn$x;y$fn$", "SELECT 2"]
);
}
#[test]
fn unclosed_dollar_is_literal() {
assert_eq!(split_statements("SELECT a$b"), vec!["SELECT a$b"]);
assert_eq!(split_statements("SELECT 1$"), vec!["SELECT 1$"]);
}
#[test]
fn comment_only_statements_are_skipped() {
assert_eq!(split_statements("-- nothing\n; SELECT 1"), vec!["SELECT 1"]);
assert_eq!(split_statements("/* c */"), Vec::<String>::new());
}
#[test]
fn empty_and_whitespace_input() {
assert_eq!(split_statements(""), Vec::<String>::new());
assert_eq!(split_statements(" ; ; "), Vec::<String>::new());
}
+5 -1
View File
@@ -24,7 +24,7 @@ pub struct AppState {
use commands::{
backup, connections, db_viewer, demo, folders, import_export, keychain, maintenance, objects, query,
schema_graph, settings, tags,
query_export, schema_diff, schema_graph, settings, tags,
};
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
@@ -136,6 +136,7 @@ pub fn run() {
db_viewer::get_extensions,
db_viewer::get_indexes,
db_viewer::get_constraints,
db_viewer::get_hypertables,
objects::create_schema,
objects::rename_schema,
objects::drop_schema,
@@ -172,7 +173,10 @@ pub fn run() {
settings::export_settings,
settings::import_settings,
schema_graph::get_schema_graph,
schema_diff::compare_schemas,
query::execute_query,
query::execute_query_multi,
query_export::export_query_to_file,
query::cancel_query,
query::get_query_history,
query::clear_query_history,
+152
View File
@@ -72,6 +72,22 @@ pub struct QueryResult {
pub execution_time_ms: Option<i64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StatementNotice {
pub statement_index: usize,
/// "dml" | "ddl" | "error"
pub kind: String,
pub text: String,
pub affected: Option<i64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MultiQueryResult {
pub result_sets: Vec<QueryResult>,
pub notices: Vec<StatementNotice>,
pub execution_time_ms: i64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Pagination {
pub page: i64,
@@ -132,6 +148,23 @@ pub struct ExtensionInfo {
pub comment: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HypertableInfo {
pub name: String,
pub schema: String,
pub num_dimensions: i64,
pub compression_enabled: bool,
pub num_chunks: i64,
pub total_size_bytes: Option<i64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HypertableListResponse {
pub available: bool,
pub reason: Option<String>,
pub items: Vec<HypertableInfo>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ObjectSearchHit {
pub name: String,
@@ -312,6 +345,125 @@ pub struct Relationship {
pub cardinality: String,
}
// ---------------------------------------------------------------------------
// Schema diff (0.8.0)
// ---------------------------------------------------------------------------
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct SnapshotColumn {
pub name: String,
pub data_type: String,
pub nullable: bool,
pub default_value: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct SnapshotIndex {
pub name: String,
pub columns: Vec<String>,
pub unique: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct SnapshotFk {
pub name: String,
pub columns: Vec<String>,
pub ref_schema: String,
pub ref_table: String,
pub ref_columns: Vec<String>,
pub on_delete: String,
pub on_update: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct SnapshotConstraint {
pub name: String,
/// "unique" | "check"
pub kind: String,
/// check: expression; unique: comma-joined columns
pub definition: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct SnapshotTable {
pub schema: String,
pub name: String,
pub columns: Vec<SnapshotColumn>,
pub primary_key: Vec<String>,
pub uniques: Vec<SnapshotConstraint>,
pub checks: Vec<SnapshotConstraint>,
pub indexes: Vec<SnapshotIndex>,
pub foreign_keys: Vec<SnapshotFk>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct SnapshotView {
pub schema: String,
pub name: String,
pub definition: String,
pub materialized: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct SnapshotSequence {
pub schema: String,
pub name: String,
pub start: String,
pub increment: String,
pub minimum: String,
pub maximum: String,
pub cycle: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct SnapshotEnum {
pub schema: String,
pub name: String,
pub labels: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq)]
pub struct SchemaSnapshot {
/// "postgresql" | "mysql" | "mariadb" | "sqlite" — the CONNECTION's db_type
pub engine: String,
pub schema: String,
pub tables: Vec<SnapshotTable>,
pub views: Vec<SnapshotView>,
pub sequences: Vec<SnapshotSequence>,
pub enums: Vec<SnapshotEnum>,
pub object_count: usize,
pub truncated: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct DiffDetailLine {
pub label: String,
pub old: String,
pub new: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct DiffItem {
/// "table" | "column" | "constraint" | "index" | "view" | "sequence" | "enum"
pub object_type: String,
/// For column/constraint/index items: "<table>.<name>"; else the object name
pub name: String,
/// "added" | "removed" | "changed"
pub kind: String,
pub detail: Vec<DiffDetailLine>,
/// Target-side statements (usually exactly one per item)
pub sync_sql: Vec<String>,
pub destructive: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiffReport {
pub items: Vec<DiffItem>,
pub source_label: String,
pub target_label: String,
pub truncated: bool,
}
#[cfg(test)]
mod tests {
use super::*;
+1 -1
View File
@@ -1,7 +1,7 @@
{
"$schema": "https://schema.tauri.app/config/2",
"productName": "Gridline",
"version": "0.7.15",
"version": "0.8.0",
"identifier": "com.adrianbonpin.gridline",
"build": {
"beforeDevCommand": "bun run dev",