diff --git a/src-tauri/src/db/backup.rs b/src-tauri/src/db/backup.rs index 7a27821c..ef0ce3ca 100644 --- a/src-tauri/src/db/backup.rs +++ b/src-tauri/src/db/backup.rs @@ -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; } @@ -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 } @@ -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)); } } } @@ -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, ©_sql, data).await { Ok(()) => run.ok += 1, - Err(e) => run.errors.push(stmt_error(e, ©_sql)), + Err(e) => run.fail(stmt_error(e, ©_sql)), } } } @@ -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); @@ -2572,7 +2588,7 @@ async fn import_d1(rep: &Reporter, cfg: Remote<'_>, sql: &str) -> Result 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; } @@ -2590,13 +2606,13 @@ async fn import_d1(rep: &Reporter, cfg: Remote<'_>, sql: &str) -> Result 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)) } @@ -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; } @@ -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; } diff --git a/src-tauri/src/db/dynamodb.rs b/src-tauri/src/db/dynamodb.rs index 0b6285c9..fa85dd71 100644 --- a/src-tauri/src/db/dynamodb.rs +++ b/src-tauri/src/db/dynamodb.rs @@ -420,7 +420,104 @@ 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> { + 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 { + use super::query::{ColumnInfo, SqlResult}; + let out = request(cfg, op, input).await?; + let items: Vec> = 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 = 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> = 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 { use super::query::{ColumnInfo, SqlResult}; let t0 = std::time::Instant::now(); @@ -428,6 +525,10 @@ pub async fn query(cfg: &DynamoConfig, statement: &str) -> Result> = Vec::new(); let mut token: Option = None; let mut capped = false; diff --git a/src-tauri/src/db/mongo.rs b/src-tauri/src/db/mongo.rs index eaf92d2a..c6588f0f 100644 --- a/src-tauri/src/db/mongo.rs +++ b/src-tauri/src/db/mongo.rs @@ -1329,6 +1329,8 @@ pub enum Stmt { Use(String), ShowDbs, ShowCollections, + /// `db.createCollection("name")` + CreateCollection(String), /// `db..(...)[.sort(...)][.limit(...)][.skip(...)]` Coll { coll: String, calls: Vec }, } @@ -1369,6 +1371,7 @@ fn calls(mut s: &str) -> Result, 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 { @@ -1383,6 +1386,17 @@ pub fn parse(stmt: &str) -> Result { _ => {} } 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(); @@ -1430,6 +1444,10 @@ pub async fn query(m: &MongoConn, stmt: &str) -> Result { + 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(); @@ -1578,6 +1596,32 @@ pub async fn query(m: &MongoConn, stmt: &str) -> Result { + 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")), } } diff --git a/src-tauri/src/db/terminal.rs b/src-tauri/src/db/terminal.rs index 8ffe6e02..105d4c27 100644 --- a/src-tauri/src/db/terminal.rs +++ b/src-tauri/src/db/terminal.rs @@ -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 @@ -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 { + 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, + turso_config: Option, ) -> Result { reap_unattached(state); let t0 = Instant::now(); @@ -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())); @@ -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"); diff --git a/src-tauri/src/mcp/access.rs b/src-tauri/src/mcp/access.rs index c68e68bc..8f405ca4 100644 --- a/src-tauri/src/mcp/access.rs +++ b/src-tauri/src/mcp/access.rs @@ -38,6 +38,8 @@ pub enum Dialect { ReadOnlyDoc, /// Elasticsearch and OpenSearch: Dev Tools requests, by method and path. Elastic, + /// DynamoDB: PartiQL, or a JSON API call such as CreateTable. + Dynamo, } impl Dialect { @@ -50,6 +52,7 @@ impl Dialect { "redis" => Self::Redis, "elasticsearch" | "opensearch" => Self::Elastic, "couchdb" | "firestore" => Self::ReadOnlyDoc, + "dynamodb" => Self::Dynamo, _ => Self::Sql, } } @@ -68,6 +71,7 @@ pub fn script_level_in(dialect: Dialect, sql: &str) -> u8 { match dialect { Dialect::Mongo => mongo_level(sql), Dialect::Redis => redis_level(sql), + Dialect::Dynamo => dynamo_level(sql), Dialect::ReadOnlyDoc => READ, Dialect::Elastic => match crate::db::elastic::parse(sql) { Ok(crate::db::elastic::Statement::Raw { method, path, .. }) => crate::db::elastic::request_level(&method, &path), @@ -114,6 +118,7 @@ pub fn statement_count(dialect: Dialect, sql: &str) -> usize { .max() .unwrap_or(0), Dialect::Oracle => crate::db::oracle::split_statements(sql).len().max(statement_count(Dialect::Sql, sql)), + Dialect::Dynamo if crate::db::dynamodb::parse_api_call(sql).is_none() => statement_count(Dialect::Sql, sql), _ => usize::from(!sql.trim().is_empty()), } } @@ -314,12 +319,35 @@ fn pragma_level(stmt: &str) -> u8 { } } +/// DynamoDB: an API call by its operation, PartiQL like any SQL. +fn dynamo_level(stmt: &str) -> u8 { + use crate::db::dynamodb::{parse_api_call, API_DANGEROUS, API_READS, API_SCHEMA, API_WRITES}; + match parse_api_call(stmt) { + None => script_level_in(Dialect::Sql, stmt), + Some(Err(_)) => DANGEROUS, + Some(Ok((op, _))) => { + let op = op.as_str(); + if API_READS.contains(&op) { + READ + } else if API_WRITES.contains(&op) { + WRITE + } else if API_SCHEMA.contains(&op) { + MODIFY + } else { + debug_assert!(API_DANGEROUS.contains(&op)); + DANGEROUS + } + } + } +} + /// Mongo shell: classified from what `db::mongo::parse` will actually run. fn mongo_level(stmt: &str) -> u8 { use crate::db::mongo::{parse, Stmt}; let Ok(parsed) = parse(stmt) else { return DANGEROUS }; match parsed { Stmt::Use(_) | Stmt::ShowDbs | Stmt::ShowCollections => READ, + Stmt::CreateCollection(_) => MODIFY, Stmt::Coll { calls, .. } => { let Some(head) = calls.first() else { return DANGEROUS }; let args: String = head.args.chars().filter(|c| !c.is_whitespace()).collect(); @@ -331,6 +359,8 @@ fn mongo_level(stmt: &str) -> u8 { | "distinct" | "getIndexes" => READ, "insertOne" | "insertMany" | "updateOne" | "replaceOne" | "deleteOne" => WRITE, "updateMany" | "deleteMany" => if all_rows { DANGEROUS } else { WRITE }, + "createIndex" | "dropIndex" => MODIFY, + // "drop" and anything unknown. _ => DANGEROUS, } } @@ -647,6 +677,20 @@ mod tests { assert_eq!(script_level("DELETE FROM \"Music\" WHERE Artist = 'x'"), WRITE); assert_eq!(script_level("TRUNCATE ks.t"), DANGEROUS); assert_eq!(script_level_in(Dialect::ReadOnlyDoc, "anything"), READ); + let d = |s: &str| script_level_in(Dialect::Dynamo, s); + assert_eq!(d(r#"SELECT * FROM "t""#), READ); + assert!(d(r#"DELETE FROM "t" WHERE pk = 'a'"#) >= WRITE); + assert_eq!(d(r#"{"operation": "Scan", "input": {"TableName": "t"}}"#), READ); + assert_eq!(d(r#"PutItem {"TableName": "t", "Item": {"pk": {"S": "a"}}}"#), WRITE); + assert_eq!(d(r#"CreateTable {"TableName": "t"}"#), MODIFY); + assert_eq!(d(r#"{"operation": "DeleteTable", "input": {"TableName": "t"}}"#), DANGEROUS); + assert_eq!(d(r#"{"operation": "Nope"}"#), DANGEROUS); + assert_eq!(Dialect::for_driver("dynamodb"), Dialect::Dynamo); + let m = |s: &str| script_level_in(Dialect::Mongo, s); + assert_eq!(m(r#"db.createCollection("shop")"#), MODIFY); + assert_eq!(m("db.users.createIndex({email: 1}, {unique: true})"), MODIFY); + assert_eq!(m(r#"db.users.dropIndex("email_1")"#), MODIFY); + assert_eq!(m("db.users.drop()"), DANGEROUS); let es = |s| script_level_in(Dialect::Elastic, s); assert_eq!(es("GET products/_search"), READ); assert_eq!(es("POST products/_search\n{\"query\": {\"match_all\": {}}}"), READ); @@ -669,7 +713,7 @@ mod tests { assert_eq!(Dialect::for_driver("elasticsearch"), Dialect::Elastic); assert_eq!(Dialect::for_driver("opensearch"), Dialect::Elastic); assert_eq!(Dialect::for_driver("cassandra"), Dialect::Sql); - assert_eq!(Dialect::for_driver("dynamodb"), Dialect::Sql); + assert_eq!(Dialect::for_driver("dynamodb"), Dialect::Dynamo); assert_eq!(Dialect::for_driver("oracle"), Dialect::Oracle); } diff --git a/src/lib/ai-query-languages.js b/src/lib/ai-query-languages.js index ce8ac812..c33a1c13 100644 --- a/src/lib/ai-query-languages.js +++ b/src/lib/ai-query-languages.js @@ -22,6 +22,7 @@ export const QUERY_LANGUAGES = { - Writes: insertOne, insertMany, updateOne/updateMany with operators ({$set: {...}}), replaceOne, deleteOne/deleteMany (with a filter). - Values: ObjectId("…"), ISODate("2026-01-01"), NumberLong(…), NumberDecimal("…"). Field names may be unquoted. - "db" is the database selected in the sidebar; "use ;" switches it. For odd names use db.getCollection("name"). +- Collections and indexes (schema access): db.createCollection("name"); db.users.createIndex({email: 1}, {unique: true}); db.users.dropIndex("email_1"). Inserting into a missing collection creates it. - Always add .limit() to find() unless the user asks for everything.`, }, couchdb: { @@ -84,7 +85,11 @@ export const QUERY_LANGUAGES = { - A SELECT without the partition key in WHERE scans the whole table: put the key in WHERE whenever possible. - No JOINs, no GROUP BY, no aggregate functions except size(), no LIMIT clause (results are paged). - Functions: begins_with(sk, 'ORDER#'), contains(tags, 'beta'), attribute_exists(x), attribute_type(x, 'S'), size(x). -- Writes: INSERT INTO "t" VALUE {'pk': 'a', 'sk': 'b'}; UPDATE "t" SET x = 1 WHERE pk = 'a' AND sk = 'b'; DELETE FROM "t" WHERE pk = 'a' AND sk = 'b';`, +- Writes: INSERT INTO "t" VALUE {'pk': 'a', 'sk': 'b'}; UPDATE "t" SET x = 1 WHERE pk = 'a' AND sk = 'b'; DELETE FROM "t" WHERE pk = 'a' AND sk = 'b'; +- PartiQL can't create or change tables. For that, run a DynamoDB API call as JSON in the same tool, one per call: + {"operation": "CreateTable", "input": {"TableName": "shop", "KeySchema": [{"AttributeName": "pk", "KeyType": "HASH"}, {"AttributeName": "sk", "KeyType": "RANGE"}], "AttributeDefinitions": [{"AttributeName": "pk", "AttributeType": "S"}, {"AttributeName": "sk", "AttributeType": "S"}], "BillingMode": "PAY_PER_REQUEST"}} + Also: DescribeTable, ListTables, UpdateTable, UpdateTimeToLive, DeleteTable, and the item calls PutItem, GetItem, UpdateItem, DeleteItem, Query, Scan, BatchGetItem, BatchWriteItem (up to 25 items), TransactWriteItems. Inputs use the AWS JSON format with typed values ({"S": "x"}, {"N": "1"}). +- A new table takes a few seconds to become ACTIVE: DescribeTable until TableStatus is ACTIVE before writing to it.`, }, redis: { name: 'Redis commands', diff --git a/src/lib/ai.js b/src/lib/ai.js index 76b1caaa..61ef81d6 100644 --- a/src/lib/ai.js +++ b/src/lib/ai.js @@ -1481,9 +1481,12 @@ const DESTRUCTIVE_RE = /^\s*(DELETE\b|DROP\b|TRUNCATE\b)/i /** @param {string} sql */ export function isDestructiveSql(sql) { const t = sql.trim() - return DESTRUCTIVE_RE.test(t) || NOSQL_DESTRUCTIVE_RE.test(t) + return DESTRUCTIVE_RE.test(t) || NOSQL_DESTRUCTIVE_RE.test(t) || DYNAMO_DESTRUCTIVE_RE.test(t) } +/** DynamoDB API calls that delete: a table, an item, or a batch of them. */ +const DYNAMO_DESTRUCTIVE_RE = /(^|"operation"\s*:\s*")(DeleteTable|DeleteItem|BatchWriteItem)\b/m + /** MongoDB bulk deletes and drops, Redis flushes and deletes. */ const NOSQL_DESTRUCTIVE_RE = /\.\s*(deleteMany|deleteOne|drop|dropDatabase|dropIndex|dropIndexes|remove|findOneAndDelete|delete|destroy)\s*\(|^(flushall|flushdb|del|unlink|hdel|srem|zrem|lrem|ltrim|xdel|xtrim|swapdb)\b/im diff --git a/src/lib/components/AiChat.svelte b/src/lib/components/AiChat.svelte index e3c8caff..61c29322 100644 --- a/src/lib/components/AiChat.svelte +++ b/src/lib/components/AiChat.svelte @@ -1532,6 +1532,8 @@ const onDown = (/** @type {MouseEvent} */ e) => { if (e.button !== 0) return; + // Stops WebKit's native drag ghost of the SVG's text. + e.preventDefault(); dragging = true; ox = e.clientX - tx; oy = e.clientY - ty; diff --git a/src/lib/components/BackupPage.svelte b/src/lib/components/BackupPage.svelte index f4883a36..0ccb57b2 100644 --- a/src/lib/components/BackupPage.svelte +++ b/src/lib/components/BackupPage.svelte @@ -27,6 +27,7 @@ import TriangleAlert from '@lucide/svelte/icons/triangle-alert' import CircleAlert from '@lucide/svelte/icons/circle-alert' import FileText from '@lucide/svelte/icons/file-text' + import ScrollText from '@lucide/svelte/icons/scroll-text' import FolderOpen from '@lucide/svelte/icons/folder-open' import Copy from '@lucide/svelte/icons/copy' import Search from '@lucide/svelte/icons/search' @@ -46,8 +47,33 @@ schemas = /** @type {string[]} */ ([]), /** @type {TableRow[]} */ tables = [], + /** After a restore that applied anything: reload the schema tree and tables. */ + onrestored = /** @type {() => void | Promise} */ (() => {}), } = $props() + // ── Live log ──────────────────────────────────────────────────────────────── + /** @typedef {{ id: number, at: number, level: string, text: string }} LogLine */ + /** Lines kept on the page; a long run drops the oldest. */ + const LOG_CAP = 2000 + /** @type {LogLine[]} */ + let logLines = $state([]) + let logSeq = 0 + let showLog = $state(false) + /** @param {string} level @param {string} text */ + function addLog(level, text) { + logLines.push({ id: logSeq++, at: Date.now(), level, text }) + if (logLines.length > LOG_CAP) logLines.splice(0, logLines.length - LOG_CAP) + } + /** @type {HTMLElement|null} */ + let logEl = $state(null) + // Follow new lines while the reader is at the bottom; stay put when they scrolled up. + let followLog = true + $effect(() => { + void logLines.length + if (logEl && followLog) logEl.scrollTop = logEl.scrollHeight + }) + const logTime = (/** @type {number} */ t) => new Date(t).toLocaleTimeString([], { hour12: false }) + /** @type {'backup'|'restore'} */ let activeTab = $state('backup') @@ -288,6 +314,12 @@ runTables.push({ key, schema: p.schema ?? null, name: p.name, est: null, status: 'pending', rows: 0, error: null }) } const row = runTables[i] + if (p.status !== row.status) { + const label = p.schema && multiSchema ? `${p.schema}.${p.name}` : p.name + if (p.status === 'running') addLog('info', `${label}: exporting`) + else if (p.status === 'done') addLog('ok', `${label}: ${rowsText(p.rows ?? 0)}`) + else if (p.status === 'error') addLog('error', `${label}: ${p.error ?? 'failed'}`) + } row.status = p.status row.rows = p.rows ?? 0 row.error = p.error ?? null @@ -295,6 +327,7 @@ /** @param {any} p */ function onBackupLog(p) { + if (p?.message) addLog(p.level ?? 'info', p.message) if (p?.level === 'warn' && !/^Cancelled/.test(p.message)) exportNotes.push(p.message) } @@ -325,6 +358,7 @@ exportPath = path runTables = [] runIndex = new Map() + logLines = [] exportStartedAt = Date.now() startClock() stopListening() @@ -462,11 +496,28 @@ importStartedAt = Date.now() startClock() stopListening() - unlisteners = [await listenTo('restore-progress', (p) => (importProgress = p))] + logLines = [] + let lastTenth = -1 + unlisteners = await Promise.all([ + listenTo('restore-progress', (p) => { + importProgress = p + // A line every 10% of the file, so the log moves on a clean run too. + const tenth = p?.bytesTotal ? Math.floor((p.bytesDone / p.bytesTotal) * 10) : -1 + if (tenth > lastTenth && tenth < 10) { + lastTenth = tenth + addLog('info', `${tenth * 10}% of the file · ${statementsText(p.ok)} applied${p.failed ? ` · ${fmtInt(p.failed)} failed` : ''}`) + } + }), + listenTo('restore-log', (p) => p?.message && addLog(p.level ?? 'info', p.message)), + ]) try { const result = await backupImport({ path: importPath }) importResult = result importPhase = 'done' + if (result.statementsOk > 0) { + addLog('info', 'Refreshing the schema') + try { await onrestored() } catch { /* the sidebar's own refresh still works */ } + } } catch (e) { importError = errText(e) importPhase = 'error' @@ -492,6 +543,45 @@ } +{#snippet logToggle()} + +{/snippet} + +{#snippet logPanel()} + + +
{ if (logEl) followLog = logEl.scrollHeight - logEl.scrollTop - logEl.clientHeight < 24 }} + class="app-scroll min-h-0 flex-1 overflow-y-auto border-t border-border/60 bg-background/40 px-4 py-2 font-mono text-ui-xs leading-relaxed" + role="log" + aria-label="Live log" + tabindex="0" + > + {#each logLines as line (line.id)} +
+ {logTime(line.at)} + {line.text} +
+ {:else} +

Waiting for the first line…

+ {/each} +
+{/snippet} + {#snippet progressBar(/** @type {number} */ fraction, /** @type {'running'|'done'|'warn'|'error'} */ tone)}
+ {@render logToggle()} {fmtElapsed(exportElapsed)}
{@render progressBar( @@ -732,7 +823,9 @@ {/if} - {#if runTables.length} + {#if showLog} + {@render logPanel()} + {:else if runTables.length}
    {#each runTables as t (t.key)}
  • @@ -854,6 +947,7 @@ {:else}Restore complete{/if}

    + {@render logToggle()} {fmtElapsed(importElapsed)} {@render progressBar( @@ -890,7 +984,9 @@ {/if} - {#if importResult?.errors.length} + {#if showLog} + {@render logPanel()} + {:else if importResult?.errors.length}
    diff --git a/src/lib/components/ErdCanvas.svelte b/src/lib/components/ErdCanvas.svelte index 890dc642..2d7f2416 100644 --- a/src/lib/components/ErdCanvas.svelte +++ b/src/lib/components/ErdCanvas.svelte @@ -1553,6 +1553,7 @@ e.preventDefault()} onpointerdown={onPointerDown} onpointermove={onPointerMove} onpointerup={onPointerUp} diff --git a/src/lib/components/ErdHierarchy.svelte b/src/lib/components/ErdHierarchy.svelte index d697b08e..d00a395e 100644 --- a/src/lib/components/ErdHierarchy.svelte +++ b/src/lib/components/ErdHierarchy.svelte @@ -420,6 +420,7 @@ role="group" aria-label="Tables in foreign-key order" data-zoom-surface + ondragstart={(e) => e.preventDefault()} onpointerdown={onPointerDown} onpointermove={onPointerMove} onpointerup={onPointerUp} diff --git a/src/lib/components/MermaidViewer.svelte b/src/lib/components/MermaidViewer.svelte index ea81c49e..b4dd1fee 100644 --- a/src/lib/components/MermaidViewer.svelte +++ b/src/lib/components/MermaidViewer.svelte @@ -281,20 +281,18 @@ if (!dragging) return dragging = false node.style.cursor = 'grab' - // Back to painted in place, so the next zoom is drawn, not stretched. - svg.style.willChange = '' window.removeEventListener('mousemove', onMove) window.removeEventListener('mouseup', onUp) } const onDown = (/** @type {MouseEvent} */ e) => { if (e.button !== 0) return + // Without this WebKit starts a native drag of the SVG's text and + // draws a translucent copy under the cursor: the "double" diagram. + e.preventDefault() dragging = true ox = e.clientX - tx; oy = e.clientY - ty node.style.cursor = 'grabbing' - // Its own layer for the drag only: a pan then moves a painted layer - // instead of repainting every glyph per frame. - svg.style.willChange = 'transform' window.addEventListener('mousemove', onMove) window.addEventListener('mouseup', onUp) } @@ -442,6 +440,6 @@
    diff --git a/src/lib/components/Sidebar.svelte b/src/lib/components/Sidebar.svelte index 3772c0fa..d1be831a 100644 --- a/src/lib/components/Sidebar.svelte +++ b/src/lib/components/Sidebar.svelte @@ -3232,11 +3232,11 @@ title={updateState === 'ready' ? 'The update is downloaded. Restart to install it.' : 'A new version of Stroke is available'} onclick={oncheckupdates} > - + {updateState === 'ready' ? 'Restart' : 'Update'} {:else} - + {/if}
    diff --git a/src/lib/components/StudioShell.svelte b/src/lib/components/StudioShell.svelte index e7af4324..bcf5a89a 100644 --- a/src/lib/components/StudioShell.svelte +++ b/src/lib/components/StudioShell.svelte @@ -10642,7 +10642,7 @@ let rowSearch = $state('') > {#await import('./BackupPage.svelte')}{:then { default: BackupPage }} - ({ name: t.name, rowCount: t.rowCount }))} /> + ({ name: t.name, rowCount: t.rowCount }))} onrestored={async () => { await loadSchemas(); await loadTables({ force: true }); bumpObjects() }} /> {/await} diff --git a/src/lib/sql-write.js b/src/lib/sql-write.js index ee280b6f..07c886ce 100644 --- a/src/lib/sql-write.js +++ b/src/lib/sql-write.js @@ -378,6 +378,9 @@ export function sqlRunEffects(sql) { /** MongoDB shell calls that change data, indexes or the database. */ const MONGO_WRITE_RE = /\.\s*(insert|insertOne|insertMany|update|updateOne|updateMany|replaceOne|remove|deleteOne|deleteMany|bulkWrite|drop|dropDatabase|dropIndex|dropIndexes|createIndex|createIndexes|createCollection|renameCollection|findOneAndUpdate|findOneAndReplace|findOneAndDelete|findAndModify)\s*\(/ +/** DynamoDB API calls that change items or tables (JSON or `Op {...}` form). */ +const DYNAMO_WRITE_RE = /(^|"operation"\s*:\s*")(PutItem|UpdateItem|DeleteItem|BatchWriteItem|TransactWriteItems|CreateTable|UpdateTable|DeleteTable|UpdateTimeToLive)\b/m + /** Firestore and CouchDB shell-style writes. */ const DOC_WRITE_RE = /\.\s*(add|set|update|delete|insert|put|remove|bulkDocs|destroy|create)\s*\(/ @@ -422,6 +425,8 @@ export function isWriteFor(sql, engine) { case 'elasticsearch': case 'opensearch': return isEsWrite(text) + case 'dynamodb': + return DYNAMO_WRITE_RE.test(text) default: return false }