Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions src-tauri/src/db/backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,7 @@ async fn import_sqlite(rep: &Reporter, pool: &sqlx::SqlitePool, sql: &str) -> Re
let mut run = Runner::new(rep, sql.len());
let mut sp = Splitter::new(sql, Escapes::Standard);
while let Some(stmt) = sp.next() {
let Stmt::Sql(s) = stmt else { run.total += 1; run.errors.push("COPY blocks are Postgres-only".into()); continue };
let Stmt::Sql(s) = stmt else { run.total += 1; run.fail("COPY blocks are Postgres-only".into()); continue };
run.total += 1;
let low = head_lower(&s, 24);
if low.starts_with("pragma foreign_keys") || is_tx_control(&s) { run.ok += 1; continue; }
Expand Down Expand Up @@ -1227,6 +1227,22 @@ struct Runner<'a> {
batch_bytes: usize,
}

/// Failures streamed to the live log before it only counts them, so a script
/// where every statement fails can't flood the page.
const LOGGED_FAILURES: usize = 200;

impl Runner<'_> {
/// Record a failed statement and stream it to the restore log.
fn fail(&mut self, message: String) {
if self.errors.len() < LOGGED_FAILURES {
self.meter.rep.log("error", message.clone());
} else if self.errors.len() == LOGGED_FAILURES {
self.meter.rep.log("warn", "More statements failed; the rest are counted, not listed");
}
self.errors.push(message);
}
}

impl<'a> Runner<'a> {
fn new(rep: &'a Reporter, len: usize) -> Self {
Self { meter: Meter::new(rep, len), total: 0, ok: 0, errors: Vec::new(), batch: Vec::new(), batch_bytes: 0 }
Expand Down Expand Up @@ -1261,7 +1277,7 @@ macro_rules! runner_exec {
Ok(_) => self.ok += 1,
Err(e) => {
(&mut *conn).execute("ROLLBACK TO SAVEPOINT stroke_sp; RELEASE SAVEPOINT stroke_sp").await.ok();
self.errors.push(stmt_error(e, stmt));
self.fail(stmt_error(e, stmt));
}
}
}
Expand Down Expand Up @@ -1940,7 +1956,7 @@ async fn import_postgres(rep: &Reporter, pool: &sqlx::PgPool, sql: &str) -> Resu
run.flush_pg(&mut *conn).await;
match pg_copy_in(&mut conn, &copy_sql, data).await {
Ok(()) => run.ok += 1,
Err(e) => run.errors.push(stmt_error(e, &copy_sql)),
Err(e) => run.fail(stmt_error(e, &copy_sql)),
}
}
}
Expand Down Expand Up @@ -2349,7 +2365,7 @@ async fn import_mysql(rep: &Reporter, pool: &sqlx::MySqlPool, sql: &str) -> Resu
// it; a failing statement is atomic on its own.
match (&mut *conn).execute(s.as_str()).await {
Ok(_) => run.ok += 1,
Err(e) => run.errors.push(stmt_error(e, &s)),
Err(e) => run.fail(stmt_error(e, &s)),
}
}
run.meter.tick(sp.position(), run.ok, run.errors.len(), false);
Expand Down Expand Up @@ -2572,7 +2588,7 @@ async fn import_d1(rep: &Reporter, cfg: Remote<'_>, sql: &str) -> Result<ImportR
match cfg.query(&s).await {
Ok(_) => run.ok += 1,
Err(e) if is_fk_error(&e) => waiting.push((s, e)),
Err(e) => run.errors.push(stmt_error(e, &s)),
Err(e) => run.fail(stmt_error(e, &s)),
}
run.meter.tick(sp.position(), run.ok, run.errors.len(), false);
if is_cancelled() { run.total += sp.count(); break; }
Expand All @@ -2590,13 +2606,13 @@ async fn import_d1(rep: &Reporter, cfg: Remote<'_>, sql: &str) -> Result<ImportR
match cfg.query(&s).await {
Ok(_) => run.ok += 1,
Err(e) if is_fk_error(&e) => still.push((s, e)),
Err(e) => run.errors.push(stmt_error(e, &s)),
Err(e) => run.fail(stmt_error(e, &s)),
}
}
waiting = still;
if waiting.len() == before { break; }
}
for (s, e) in waiting { run.errors.push(stmt_error(e, &s)); }
for (s, e) in waiting { run.fail(stmt_error(e, &s)); }
run.meter.tick(sql.len(), run.ok, run.errors.len(), true);
Ok(finish_import(rep, run.total, run.ok, run.errors, started))
}
Expand Down Expand Up @@ -2705,7 +2721,7 @@ async fn import_duckdb(
if head_lower(&s, 24).starts_with("pragma foreign_keys") { run.ok += 1; continue; }
match super::duckdb::execute_sql(handle, &s).await {
Ok(_) => run.ok += 1,
Err(e) => run.errors.push(stmt_error(e, &s)),
Err(e) => run.fail(stmt_error(e, &s)),
}
run.meter.tick(sp.position(), run.ok, run.errors.len(), false);
if is_cancelled() { run.total += sp.count(); break; }
Expand Down Expand Up @@ -2789,7 +2805,7 @@ async fn import_mssql(
run.total += 1;
match super::mssql::execute_sql(handle, &s).await {
Ok(_) => run.ok += 1,
Err(e) => run.errors.push(stmt_error(e, &s)),
Err(e) => run.fail(stmt_error(e, &s)),
}
run.meter.tick(sp.position(), run.ok, run.errors.len(), false);
if is_cancelled() { run.total += sp.count(); break; }
Expand Down
103 changes: 102 additions & 1 deletion src-tauri/src/db/dynamodb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,14 +420,115 @@ fn number(n: &str) -> serde_json::Value {
/// Rows past this many stop the NextToken loop; the message says so.
const PARTIQL_ROW_CAP: usize = 5_000;

/// Run one PartiQL statement (`SELECT`, `INSERT`, `UPDATE`, `DELETE`).
/// DynamoDB API operations the console (and the AI) can call as JSON, for
/// what PartiQL can't do: tables, TTL, batch writes. Grouped by what they need.
pub const API_READS: &[&str] = &[
"DescribeTable", "ListTables", "GetItem", "Query", "Scan", "BatchGetItem", "DescribeTimeToLive",
];
pub const API_WRITES: &[&str] = &["PutItem", "UpdateItem", "DeleteItem", "BatchWriteItem", "TransactWriteItems"];
pub const API_SCHEMA: &[&str] = &["CreateTable", "UpdateTable", "UpdateTimeToLive"];
pub const API_DANGEROUS: &[&str] = &["DeleteTable"];

/// An API call, written either as `{"operation": "CreateTable", "input": {...}}`
/// or Dev Tools style as `CreateTable {...}`. None when it's PartiQL.
pub fn parse_api_call(stmt: &str) -> Option<Result<(String, serde_json::Value), String>> {
let s = stmt.trim().trim_end_matches(';').trim();
let known = |op: &str| [API_READS, API_WRITES, API_SCHEMA, API_DANGEROUS].iter().any(|l| l.contains(&op));
if s.starts_with('{') {
let v: serde_json::Value = match serde_json::from_str(s) {
Ok(v) => v,
Err(e) => return Some(Err(format!("The API call isn't valid JSON: {e}"))),
};
let Some(op) = v.get("operation").and_then(|o| o.as_str()) else {
return Some(Err("An API call names its \"operation\", like {\"operation\": \"CreateTable\", \"input\": {...}}".into()));
};
if !known(op) {
return Some(Err(format!("{op} isn't available here")));
}
return Some(Ok((op.to_string(), v.get("input").cloned().unwrap_or_else(|| serde_json::json!({})))));
}
let word: String = s.chars().take_while(|c| c.is_ascii_alphabetic()).collect();
if word.is_empty() || !known(&word) {
return None;
}
let body = s[word.len()..].trim();
let input = if body.is_empty() {
serde_json::json!({})
} else {
match serde_json::from_str(body) {
Ok(v) => v,
Err(e) => return Some(Err(format!("The {word} input isn't valid JSON: {e}"))),
}
};
Some(Ok((word, input)))
}

/// Run an API call and show its answer: items as rows, anything else as one row.
async fn api_call(
cfg: &DynamoConfig,
op: &str,
input: &serde_json::Value,
statement: &str,
t0: std::time::Instant,
) -> Result<super::query::SqlResult, String> {
use super::query::{ColumnInfo, SqlResult};
let out = request(cfg, op, input).await?;
let items: Vec<serde_json::Map<String, serde_json::Value>> = if let Some(arr) = out.get("Items").and_then(|v| v.as_array()) {
arr.iter().filter_map(|i| i.as_object().cloned()).collect()
} else if let Some(item) = out.get("Item").and_then(|v| v.as_object()) {
vec![item.clone()]
} else {
Vec::new()
};
if items.is_empty() && !out.get("Items").is_some() {
let message = Some(format!("{op} done"));
let rows = if out.as_object().is_some_and(|o| !o.is_empty()) { vec![vec![out]] } else { Vec::new() };
return Ok(SqlResult {
columns: vec![ColumnInfo::new("result".to_string(), "json")],
row_count: Some(rows.len() as i64),
rows,
message,
query_ms: t0.elapsed().as_millis() as u64,
sql: statement.to_string(),
});
}
let mut cols: Vec<String> = Vec::new();
let mut seen = std::collections::HashSet::new();
for it in &items {
for k in it.keys() {
if seen.insert(k.clone()) {
cols.push(k.clone());
}
}
}
let rows: Vec<Vec<serde_json::Value>> = items
.iter()
.map(|it| cols.iter().map(|c| it.get(c).map(unmarshall).unwrap_or(serde_json::Value::Null)).collect())
.collect();
let message = out.get("LastEvaluatedKey").map(|_| "More items exist: pass LastEvaluatedKey as ExclusiveStartKey for the next page".to_string());
Ok(SqlResult {
columns: cols.iter().map(|c| ColumnInfo::new(c.clone(), "json")).collect(),
row_count: Some(rows.len() as i64),
rows,
message,
query_ms: t0.elapsed().as_millis() as u64,
sql: statement.to_string(),
})
}

/// Run one statement: PartiQL (`SELECT`, `INSERT`, `UPDATE`, `DELETE`), or an
/// API call for what PartiQL can't do (see [`parse_api_call`]).
pub async fn query(cfg: &DynamoConfig, statement: &str) -> Result<super::query::SqlResult, String> {
use super::query::{ColumnInfo, SqlResult};
let t0 = std::time::Instant::now();
let stmt = statement.trim().trim_end_matches(';').trim();
if stmt.is_empty() {
return Err("Empty statement".into());
}
if let Some(call) = parse_api_call(stmt) {
let (op, input) = call?;
return api_call(cfg, &op, &input, statement, t0).await;
}
let mut items: Vec<serde_json::Map<String, serde_json::Value>> = Vec::new();
let mut token: Option<String> = None;
let mut capped = false;
Expand Down
44 changes: 44 additions & 0 deletions src-tauri/src/db/mongo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,8 @@ pub enum Stmt {
Use(String),
ShowDbs,
ShowCollections,
/// `db.createCollection("name")`
CreateCollection(String),
/// `db.<coll>.<method>(...)[.sort(...)][.limit(...)][.skip(...)]`
Coll { coll: String, calls: Vec<Call> },
}
Expand Down Expand Up @@ -1369,6 +1371,7 @@ fn calls(mut s: &str) -> Result<Vec<Call>, String> {
const METHODS: &[&str] = &[
"find", "findOne", "aggregate", "countDocuments", "estimatedDocumentCount", "distinct", "insertOne", "insertMany",
"updateOne", "updateMany", "replaceOne", "deleteOne", "deleteMany", "getIndexes", "explain",
"createIndex", "dropIndex", "drop",
];

pub fn parse(stmt: &str) -> Result<Stmt, String> {
Expand All @@ -1383,6 +1386,17 @@ pub fn parse(stmt: &str) -> Result<Stmt, String> {
_ => {}
}
let rest = s.strip_prefix("db.").ok_or(usage)?;
// db.createCollection("name")
if let Some(r) = rest.strip_prefix("createCollection(") {
let r = r.trim();
let inner = r.strip_suffix(')').ok_or("createCollection() takes a quoted name")?.trim();
let name = inner
.strip_prefix('"').and_then(|v| v.strip_suffix('"'))
.or_else(|| inner.strip_prefix('\'').and_then(|v| v.strip_suffix('\'')))
.filter(|n| !n.is_empty())
.ok_or("createCollection() takes a quoted name")?;
return Ok(Stmt::CreateCollection(name.to_string()));
}
// db.getCollection("name").method(...)
if let Some(r) = rest.strip_prefix("getCollection(") {
let r = r.trim_start();
Expand Down Expand Up @@ -1430,6 +1444,10 @@ pub async fn query(m: &MongoConn, stmt: &str) -> Result<super::query::SqlResult,
let names = m.client.list_database_names().await.map_err(err)?;
(names.into_iter().map(|n| json!({ "name": n })).collect(), None)
}
Stmt::CreateCollection(name) => {
m.client.database(&db_name).create_collection(&name).await.map_err(err)?;
(vec![], Some(format!("Created collection {name}")))
}
Stmt::ShowCollections => {
let mut names = m.client.database(&db_name).list_collection_names().await.map_err(err)?;
names.sort();
Expand Down Expand Up @@ -1578,6 +1596,32 @@ pub async fn query(m: &MongoConn, stmt: &str) -> Result<super::query::SqlResult,
let r = if head.name == "deleteOne" { c.delete_one(f).await } else { c.delete_many(f).await }.map_err(err)?;
(vec![], Some(format!("Deleted {}", r.deleted_count)))
}
"createIndex" => {
let keys = arg_doc(&args, 0, "index key")?;
if keys.is_empty() {
return Err("createIndex() takes the keys, like {email: 1}".into());
}
let opts = args.get(1).filter(|v| v.is_object());
let mut options = mongodb::options::IndexOptions::default();
if let Some(o) = opts {
options.name = o.get("name").and_then(|v| v.as_str()).map(str::to_string);
options.unique = o.get("unique").and_then(|v| v.as_bool());
options.sparse = o.get("sparse").and_then(|v| v.as_bool());
options.expire_after = o.get("expireAfterSeconds").and_then(|v| v.as_u64()).map(std::time::Duration::from_secs);
}
let model = mongodb::IndexModel::builder().keys(keys).options(options).build();
let r = c.create_index(model).await.map_err(err)?;
(vec![json!({ "name": r.index_name })], Some(format!("Created index {}", r.index_name)))
}
"dropIndex" => {
let name = args.first().and_then(|v| v.as_str()).ok_or("dropIndex() takes the index name")?;
c.drop_index(name).await.map_err(err)?;
(vec![], Some(format!("Dropped index {name}")))
}
"drop" => {
c.drop().await.map_err(err)?;
(vec![], Some(format!("Dropped collection {coll}")))
}
other => return Err(format!("{other}() isn't supported here")),
}
}
Expand Down
29 changes: 27 additions & 2 deletions src-tauri/src/db/terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,14 @@ pub async fn terminal_open(
AnyConnectionConfig::Libsql(_) => crate::providers::turso_cli_token(&app).await,
_ => None,
};
open_session(&state, config, cols, rows, turso_token).await
let turso_config = match (&config, &turso_token) {
(AnyConnectionConfig::Libsql(_), Some(_)) => {
use tauri::Manager;
app.path().app_config_dir().ok().and_then(|dir| quiet_turso_config(&dir.join("turso-cli")))
}
_ => None,
};
open_session(&state, config, cols, rows, turso_token, turso_config).await
}

/// Install the connection's client with this OS's package manager, without a
Expand Down Expand Up @@ -905,12 +912,24 @@ pub async fn terminal_close(state: State<'_, TerminalState>, id: String) -> Resu
Ok(())
}

/// Make the Turso CLI config folder Stroke points its shell at: autoupdate off.
/// Returns None when it can't be written, and the CLI keeps its own.
fn quiet_turso_config(dir: &Path) -> Option<PathBuf> {
std::fs::create_dir_all(dir).ok()?;
let file = dir.join("settings.json");
if !file.exists() {
std::fs::write(&file, r#"{"config":{"autoupdate":"off"}}"#).ok()?;
}
Some(dir.to_path_buf())
}

async fn open_session(
state: &TerminalState,
config: AnyConnectionConfig,
cols: u16,
rows: u16,
turso_token: Option<String>,
turso_config: Option<PathBuf>,
) -> Result<TerminalSession, String> {
reap_unattached(state);
let t0 = Instant::now();
Expand Down Expand Up @@ -956,6 +975,12 @@ async fn open_session(
let mut env = Vec::new();
if let Some(token) = turso_token {
env.push(("TURSO_API_TOKEN", token));
// A config folder of Stroke's own with autoupdate off: the CLI
// otherwise updates itself through Homebrew when the shell exits.
// The token above signs it in, so the user's own login isn't needed.
if let (false, Some(dir)) = (via_wsl, &turso_config) {
env.push(("TURSO_CONFIG_FOLDER", dir.to_string_lossy().into_owned()));
}
// WSL only forwards the Windows variables WSLENV names.
if via_wsl {
env.push(("WSLENV", "TURSO_API_TOKEN/u".to_string()));
Expand Down Expand Up @@ -1436,7 +1461,7 @@ mod tests {
name: "t".into(),
file_path: db.display().to_string(),
});
let session = open_session(&state, config, 80, 24, None).await.unwrap();
let session = open_session(&state, config, 80, 24, None, None).await.unwrap();

let wrong = session.url.replace("token=", "token=x");
assert!(tokio_tungstenite::connect_async(&wrong).await.is_err(), "a wrong token must not attach");
Expand Down
Loading
Loading