diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 60680abe..e722a9bc 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -208,6 +208,7 @@ dependencies = [ "sha2 0.10.9", "sqlx", "sysinfo", + "tar", "tauri", "tauri-build", "tauri-plugin-autostart", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 3a004773..f2dd320f 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -35,6 +35,7 @@ axum = { version = "0.7", features = ["json", "macros", "ws"] } tower-http = { version = "0.5", features = ["cors"] } sqlx = { version = "0.8", features = ["runtime-tokio", "tls-rustls", "postgres", "sqlite", "mysql", "json", "chrono", "uuid", "rust_decimal"] } flate2 = "1" +tar = "0.4" encoding_rs = "0.8" reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls-webpki-roots", "stream"] } futures = "0.3" diff --git a/src-tauri/src/db/migrations.rs b/src-tauri/src/db/migrations.rs new file mode 100644 index 00000000..3db78839 --- /dev/null +++ b/src-tauri/src/db/migrations.rs @@ -0,0 +1,235 @@ +// The migrations a Prisma or Drizzle project has on disk, for the Migrations +// page. What ran comes from the database (`_prisma_migrations`, +// `__drizzle_migrations`), read by the page with plain SELECTs; this side only +// reads the project folder, so the page can show what's still pending and spot +// a migration whose file changed after it ran. +// +// Prisma: `/migrations//migration.sql`, its checksum the SHA-256 of +// the file (what Prisma stores in `_prisma_migrations.checksum`). +// Drizzle: `/meta/_journal.json` lists the migrations in order ({ idx, +// tag, when }); `/.sql` is the SQL and its SHA-256 is the `hash` +// Drizzle stores in `__drizzle_migrations`. + +use serde::Serialize; +use sha2::{Digest, Sha256}; +use std::path::{Path, PathBuf}; + +#[derive(Debug, Clone, Serialize, PartialEq)] +#[serde(rename_all = "camelCase")] +pub struct MigrationFile { + /// Prisma: the folder name. Drizzle: the journal's `tag`. + pub name: String, + pub sql: String, + /// SHA-256 of the SQL, hex. + pub checksum: String, + /// Drizzle's journal `when` (ms); None for Prisma. + pub when: Option, +} + +#[derive(Debug, Clone, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct MigrationProject { + /// prisma | drizzle + pub tool: String, + /// The folder the migrations were read from. + pub dir: String, + pub files: Vec, +} + +const MAX_SQL: usize = 2 * 1024 * 1024; + +fn sha256_hex(bytes: &[u8]) -> String { + hex::encode(Sha256::digest(bytes)) +} + +fn read_sql(path: &Path) -> Option<(String, String)> { + let bytes = std::fs::read(path).ok()?; + let checksum = sha256_hex(&bytes); + let mut sql = String::from_utf8_lossy(&bytes).into_owned(); + if sql.len() > MAX_SQL { + let mut cut = MAX_SQL; + while !sql.is_char_boundary(cut) { + cut -= 1; + } + sql.truncate(cut); + sql.push_str("\n-- (cut: the file is larger than 2 MB)"); + } + Some((sql, checksum)) +} + +/// `migrations/` folders that hold Prisma migrations (a `migration_lock.toml`, +/// or subfolders with a `migration.sql`). +fn is_prisma_dir(dir: &Path) -> bool { + if dir.join("migration_lock.toml").is_file() { + return true; + } + std::fs::read_dir(dir) + .map(|rd| rd.flatten().any(|e| e.path().join("migration.sql").is_file())) + .unwrap_or(false) +} + +/// Drizzle output folders: they hold `meta/_journal.json`. +fn is_drizzle_dir(dir: &Path) -> bool { + dir.join("meta").join("_journal.json").is_file() +} + +/// Folders worth looking into for a project root, a few levels down, skipping +/// the ones that are never migrations. +fn walk(root: &Path, depth: usize, out: &mut Vec) { + out.push(root.to_path_buf()); + if depth == 0 { + return; + } + let Ok(rd) = std::fs::read_dir(root) else { return }; + let mut dirs: Vec = rd + .flatten() + .filter(|e| e.file_type().is_ok_and(|t| t.is_dir())) + .map(|e| e.path()) + .filter(|p| { + let n = p.file_name().and_then(|n| n.to_str()).unwrap_or(""); + !n.starts_with('.') && !matches!(n, "node_modules" | "target" | "dist" | "build" | ".next" | "coverage" | "vendor") + }) + .collect(); + dirs.sort(); + for d in dirs { + walk(&d, depth - 1, out); + } +} + +/// Find the migrations of `tool` (or either) in a project folder: the folder +/// itself, or one up to four levels below it. +pub fn find(root: &Path, tool: Option<&str>) -> Option<(String, PathBuf)> { + let mut dirs = Vec::new(); + walk(root, 4, &mut dirs); + let want_prisma = tool.is_none_or(|t| t == "prisma"); + let want_drizzle = tool.is_none_or(|t| t == "drizzle"); + // Prefer the shallowest match: a monorepo's app folder over a fixture deep down. + dirs.sort_by_key(|d| d.components().count()); + for d in &dirs { + if want_drizzle && is_drizzle_dir(d) { + return Some(("drizzle".into(), d.clone())); + } + if want_prisma && d.file_name().is_some_and(|n| n == "migrations") && is_prisma_dir(d) { + return Some(("prisma".into(), d.clone())); + } + } + None +} + +pub fn read_prisma(dir: &Path) -> Vec { + let Ok(rd) = std::fs::read_dir(dir) else { return Vec::new() }; + let mut names: Vec = rd + .flatten() + .filter(|e| e.path().join("migration.sql").is_file()) + .filter_map(|e| e.file_name().to_str().map(String::from)) + .collect(); + // Prisma applies them in folder-name order (timestamp prefixes). + names.sort(); + names + .into_iter() + .filter_map(|name| { + let (sql, checksum) = read_sql(&dir.join(&name).join("migration.sql"))?; + Some(MigrationFile { name, sql, checksum, when: None }) + }) + .collect() +} + +pub fn read_drizzle(dir: &Path) -> Result, String> { + let journal = std::fs::read_to_string(dir.join("meta").join("_journal.json")) + .map_err(|e| format!("Could not read meta/_journal.json: {e}"))?; + let v: serde_json::Value = serde_json::from_str(&journal).map_err(|e| format!("meta/_journal.json isn't valid JSON: {e}"))?; + let mut entries: Vec<(i64, String, Option)> = v + .get("entries") + .and_then(|e| e.as_array()) + .map(|a| { + a.iter() + .filter_map(|e| { + let tag = e.get("tag")?.as_str()?.to_string(); + let idx = e.get("idx").and_then(|i| i.as_i64()).unwrap_or(0); + Some((idx, tag, e.get("when").and_then(|w| w.as_i64()))) + }) + .collect() + }) + .unwrap_or_default(); + entries.sort_by_key(|e| e.0); + Ok(entries + .into_iter() + .map(|(_, tag, when)| { + let (sql, checksum) = read_sql(&dir.join(format!("{tag}.sql"))).unwrap_or_else(|| ("-- The file for this migration is missing".into(), String::new())); + MigrationFile { name: tag, sql, checksum, when } + }) + .collect()) +} + +/// The migrations in a project folder. `tool` narrows the search when a +/// project has both. +#[tauri::command] +pub async fn migrations_project(dir: String, tool: Option) -> Result { + tokio::task::spawn_blocking(move || { + let root = PathBuf::from(&dir); + if !root.is_dir() { + return Err(format!("{dir} isn't a folder")); + } + let (tool, found) = find(&root, tool.as_deref()).ok_or_else(|| { + "No Prisma or Drizzle migrations in this folder. Pick the project folder (the one with prisma/ or drizzle.config).".to_string() + })?; + let files = if tool == "prisma" { read_prisma(&found) } else { read_drizzle(&found)? }; + Ok(MigrationProject { tool, dir: found.to_string_lossy().into_owned(), files }) + }) + .await + .map_err(|e| e.to_string())? +} + +#[cfg(test)] +mod tests { + use super::*; + + fn tmp(name: &str) -> PathBuf { + let d = std::env::temp_dir().join(format!("stroke-mig-{name}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&d); + std::fs::create_dir_all(&d).unwrap(); + d + } + + #[test] + fn prisma_migrations_are_found_and_read_in_order() { + let root = tmp("prisma"); + let m = root.join("app").join("prisma").join("migrations"); + for (n, sql) in [("20240102_add_posts", "CREATE TABLE posts();"), ("20240101_init", "CREATE TABLE users();")] { + std::fs::create_dir_all(m.join(n)).unwrap(); + std::fs::write(m.join(n).join("migration.sql"), sql).unwrap(); + } + std::fs::write(m.join("migration_lock.toml"), "provider = \"postgresql\"").unwrap(); + let (tool, dir) = find(&root, None).unwrap(); + assert_eq!((tool.as_str(), dir.clone()), ("prisma", m.clone())); + let files = read_prisma(&dir); + assert_eq!(files.iter().map(|f| f.name.as_str()).collect::>(), ["20240101_init", "20240102_add_posts"]); + assert_eq!(files[0].checksum, sha256_hex(b"CREATE TABLE users();")); + let _ = std::fs::remove_dir_all(&root); + } + + #[test] + fn drizzle_journal_drives_the_order_and_hashes() { + let root = tmp("drizzle"); + let out = root.join("drizzle"); + std::fs::create_dir_all(out.join("meta")).unwrap(); + std::fs::write( + out.join("meta").join("_journal.json"), + r#"{"entries":[{"idx":1,"tag":"0001_posts","when":1700000002000},{"idx":0,"tag":"0000_init","when":1700000001000},{"idx":2,"tag":"0002_gone","when":1700000003000}]}"#, + ) + .unwrap(); + std::fs::write(out.join("0000_init.sql"), "CREATE TABLE users();").unwrap(); + std::fs::write(out.join("0001_posts.sql"), "CREATE TABLE posts();").unwrap(); + // node_modules is never searched. + std::fs::create_dir_all(root.join("node_modules").join("x").join("meta")).unwrap(); + let (tool, dir) = find(&root, None).unwrap(); + assert_eq!((tool.as_str(), dir.clone()), ("drizzle", out.clone())); + let files = read_drizzle(&dir).unwrap(); + assert_eq!(files.iter().map(|f| f.name.as_str()).collect::>(), ["0000_init", "0001_posts", "0002_gone"]); + assert_eq!(files[1].when, Some(1700000002000)); + assert_eq!(files[0].checksum, sha256_hex(b"CREATE TABLE users();")); + assert!(files[2].checksum.is_empty()); + assert!(find(&root, Some("prisma")).is_none()); + let _ = std::fs::remove_dir_all(&root); + } +} diff --git a/src-tauri/src/db/mod.rs b/src-tauri/src/db/mod.rs index 8bd430d8..b3a800fc 100644 --- a/src-tauri/src/db/mod.rs +++ b/src-tauri/src/db/mod.rs @@ -1,5 +1,6 @@ pub mod admin; pub mod backup; +pub mod migrations; pub mod clickhouse; pub mod parked; pub mod posthog; diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 9e0dc0fb..401c4bdd 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -12,6 +12,7 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; mod account; mod backup_crypto; mod cloud_backup; +mod local_servers; mod shares; mod nightly; mod agents; @@ -939,6 +940,18 @@ pub fn run() { db::parked::parked_connections, commands::export_parquet, cloud_backup::backup_jobs_list, + db::migrations::migrations_project, + local_servers::local_servers_env, + local_servers::local_servers_list, + local_servers::local_server_create, + local_servers::local_server_start, + local_servers::local_server_stop, + local_servers::local_server_delete, + local_servers::local_server_update, + local_servers::local_server_logs, + local_servers::local_server_free_port, + local_servers::brew_services_list, + local_servers::brew_service_action, cloud_backup::backup_job_save, cloud_backup::backup_job_patch, cloud_backup::backup_job_delete, diff --git a/src-tauri/src/local_servers.rs b/src-tauri/src/local_servers.rs new file mode 100644 index 00000000..f2c2a599 --- /dev/null +++ b/src-tauri/src/local_servers.rs @@ -0,0 +1,866 @@ +// Local database servers: Postgres, MySQL, MariaDB, Redis and MongoDB running +// on this computer, any version side by side, each on its own port. +// +// Two runtimes: +// - Docker: one container per server, `stroke-`, its data in the named +// volume of the same name, published on 127.0.0.1 only. +// - Native: Postgres binaries downloaded per version (theseus-rs builds, the +// checksum checked), or the MySQL / Redis already installed on this computer. +// The data lives in the app's data folder, one directory per server. +// +// Homebrew services (macOS, Linux) are listed beside them, with Start/Stop. +// +// Every server listens on 127.0.0.1 with no password (trust auth, an empty +// root password): it's for development on this machine, like DBngin. + +use serde::{Deserialize, Serialize}; +use std::path::{Path, PathBuf}; +use std::time::Duration; +use tauri::{AppHandle, Emitter, Manager}; +use tokio::process::Command; + +use crate::proc::quiet; + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct LocalServer { + pub id: String, + pub name: String, + /// postgres | mysql | mariadb | redis | mongodb + pub engine: String, + /// What was asked for ("16", "8.4"); native Postgres also keeps the exact build. + pub version: String, + pub port: u16, + /// docker | native + pub runtime: String, + #[serde(default)] + pub created_at: i64, +} + +#[derive(Debug, Clone, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct ServerState { + #[serde(flatten)] + pub server: LocalServer, + pub running: bool, + /// Why it can't run right now (Docker not running, binary gone). + pub problem: Option, +} + +#[derive(Debug, Clone, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct Environment { + /// `docker version` answered: the engine is installed and running. + pub docker: bool, + /// The CLI exists but the engine didn't answer (Docker Desktop not started). + pub docker_installed: bool, + pub brew: bool, + /// Engines that can run natively here, besides Postgres (always downloadable). + pub native: Vec, +} + +#[derive(Debug, Clone, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct BrewService { + pub name: String, + pub status: String, + pub running: bool, +} + +// ── Storage ───────────────────────────────────────────────────────────────── + +fn root(app: &AppHandle) -> Result { + let dir = app.path().app_data_dir().map_err(|e| e.to_string())?.join("local-servers"); + std::fs::create_dir_all(&dir).map_err(|e| format!("Could not create {}: {e}", dir.display()))?; + Ok(dir) +} + +fn load(app: &AppHandle) -> Result, String> { + let path = root(app)?.join("servers.json"); + match std::fs::read(&path) { + Ok(b) => serde_json::from_slice(&b).map_err(|e| format!("{} is damaged: {e}", path.display())), + Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(Vec::new()), + Err(e) => Err(e.to_string()), + } +} + +fn store(app: &AppHandle, servers: &[LocalServer]) -> Result<(), String> { + let path = root(app)?.join("servers.json"); + let tmp = path.with_extension("json.tmp"); + std::fs::write(&tmp, serde_json::to_vec_pretty(servers).map_err(|e| e.to_string())?).map_err(|e| e.to_string())?; + std::fs::rename(&tmp, &path).map_err(|e| e.to_string()) +} + +fn find(app: &AppHandle, id: &str) -> Result { + load(app)?.into_iter().find(|s| s.id == id).ok_or_else(|| "That server no longer exists".to_string()) +} + +fn server_dir(app: &AppHandle, id: &str) -> Result { + Ok(root(app)?.join(id)) +} + +// ── Engines ───────────────────────────────────────────────────────────────── + +pub const ENGINES: &[&str] = &["postgres", "mysql", "mariadb", "redis", "mongodb"]; + +/// The port each engine listens on inside its container, and by default. +pub fn default_port(engine: &str) -> u16 { + match engine { + "postgres" => 5432, + "mysql" | "mariadb" => 3306, + "redis" => 6379, + "mongodb" => 27017, + _ => 0, + } +} + +fn valid_version(v: &str) -> bool { + !v.is_empty() && v.len() <= 20 && v.chars().all(|c| c.is_ascii_alphanumeric() || c == '.' || c == '-') +} + +/// The Docker image for an engine and version, and its environment. +fn docker_image(engine: &str, version: &str) -> Result<(String, Vec<&'static str>), String> { + if !valid_version(version) { + return Err(format!("\"{version}\" isn't a version")); + } + Ok(match engine { + "postgres" => (format!("postgres:{version}"), vec!["POSTGRES_HOST_AUTH_METHOD=trust"]), + "mysql" => (format!("mysql:{version}"), vec!["MYSQL_ALLOW_EMPTY_PASSWORD=yes"]), + "mariadb" => (format!("mariadb:{version}"), vec!["MARIADB_ALLOW_EMPTY_ROOT_PASSWORD=yes"]), + "redis" => (format!("redis:{version}"), vec![]), + "mongodb" => (format!("mongo:{version}"), vec![]), + _ => return Err(format!("{engine} isn't supported")), + }) +} + +/// Where each engine keeps its data inside the container. +fn docker_data_path(engine: &str) -> &'static str { + match engine { + "postgres" => "/var/lib/postgresql/data", + "mysql" | "mariadb" => "/var/lib/mysql", + "redis" => "/data", + _ => "/data/db", + } +} + +fn container(id: &str) -> String { + format!("stroke-{id}") +} + +// ── Running commands ──────────────────────────────────────────────────────── + +/// Run a program to completion; stdout on success, its stderr as the error. +async fn run(program: &Path, args: &[&str]) -> Result { + let mut cmd = Command::new(program); + cmd.args(args).stdin(std::process::Stdio::null()); + let out = tokio::time::timeout(Duration::from_secs(600), quiet(&mut cmd).output()) + .await + .map_err(|_| format!("{} did not finish in time", program.display()))? + .map_err(|e| format!("Could not run {}: {e}", program.display()))?; + if out.status.success() { + Ok(String::from_utf8_lossy(&out.stdout).trim().to_string()) + } else { + let err = String::from_utf8_lossy(&out.stderr).trim().to_string(); + let err = if err.is_empty() { String::from_utf8_lossy(&out.stdout).trim().to_string() } else { err }; + Err(if err.is_empty() { format!("{} exited with {}", program.display(), out.status) } else { err }) + } +} + +/// A program on PATH, or in the usual Homebrew and distro folders the app's +/// PATH misses when it's started from the Dock. +pub fn which(name: &str) -> Option { + let exe = if cfg!(windows) { format!("{name}.exe") } else { name.to_string() }; + let mut dirs: Vec = std::env::var_os("PATH").map(|p| std::env::split_paths(&p).collect()).unwrap_or_default(); + for extra in ["/opt/homebrew/bin", "/usr/local/bin", "/usr/bin", "/usr/sbin", "/usr/local/sbin", "/opt/homebrew/sbin"] { + dirs.push(PathBuf::from(extra)); + } + if cfg!(windows) { + dirs.push(PathBuf::from(r"C:\Program Files\Docker\Docker\resources\bin")); + } + if cfg!(target_os = "macos") { + // Docker Desktop's per-user install and its app bundle, when the + // /usr/local/bin link wasn't made. + if let Some(home) = std::env::var_os("HOME") { + dirs.push(PathBuf::from(home).join(".docker").join("bin")); + } + dirs.push(PathBuf::from("/Applications/Docker.app/Contents/Resources/bin")); + dirs.push(PathBuf::from("/Applications/OrbStack.app/Contents/MacOS/xbin")); + } + dirs.into_iter().map(|d| d.join(&exe)).find(|p| p.is_file()) +} + +fn docker() -> Result { + which("docker").ok_or_else(|| "Docker isn't installed. Install Docker Desktop, or pick Native.".to_string()) +} + +async fn docker_up() -> bool { + match which("docker") { + Some(d) => run(&d, &["version", "--format", "{{.Server.Version}}"]).await.is_ok(), + None => false, + } +} + +/// Binaries a native server of this engine needs, when they're installed. +fn native_binary(engine: &str) -> Option { + match engine { + "mysql" => which("mysqld"), + "mariadb" => which("mariadbd").or_else(|| which("mysqld")), + "redis" => which("redis-server"), + "mongodb" => which("mongod"), + _ => None, + } +} + +/// Is something already listening on this port? +async fn port_busy(port: u16) -> bool { + tokio::time::timeout(Duration::from_millis(400), tokio::net::TcpStream::connect(("127.0.0.1", port))) + .await + .is_ok_and(|r| r.is_ok()) +} + +/// The first port from `from` up that no server here owns and nothing listens on. +async fn free_port(from: u16, servers: &[LocalServer]) -> u16 { + let mut p = from.max(1024); + while p < u16::MAX && (servers.iter().any(|s| s.port == p) || port_busy(p).await) { + p += 1; + } + p +} + +/// A port to suggest for a new server: the engine's default, or the next free one. +#[tauri::command] +pub async fn local_server_free_port(app: AppHandle, from: u16) -> Result { + Ok(free_port(from, &load(&app)?).await) +} + +/// Wait until the server accepts connections, up to `secs`. +async fn wait_for_port(port: u16, secs: u64) -> bool { + let end = std::time::Instant::now() + Duration::from_secs(secs); + while std::time::Instant::now() < end { + if port_busy(port).await { + return true; + } + tokio::time::sleep(Duration::from_millis(400)).await; + } + false +} + +fn progress(app: &AppHandle, id: &str, message: &str, fraction: Option) { + let _ = app.emit("local-server-progress", serde_json::json!({ "id": id, "message": message, "fraction": fraction })); +} + +// ── Native Postgres builds ────────────────────────────────────────────────── + +fn pg_target() -> Option<&'static str> { + Some(match (std::env::consts::OS, std::env::consts::ARCH) { + ("macos", "aarch64") => "aarch64-apple-darwin", + ("macos", "x86_64") => "x86_64-apple-darwin", + ("linux", "aarch64") => "aarch64-unknown-linux-gnu", + ("linux", "x86_64") => "x86_64-unknown-linux-gnu", + ("windows", "x86_64") => "x86_64-pc-windows-msvc", + ("windows", "aarch64") => "aarch64-pc-windows-msvc", + _ => return None, + }) +} + +/// The newest build of a Postgres major ("16" → "16.11.0"). +async fn pg_release(major: &str) -> Result { + let url = "https://api.github.com/repos/theseus-rs/postgresql-binaries/releases?per_page=100"; + let releases: Vec = reqwest::Client::new() + .get(url) + .header("user-agent", "stroke-desktop") + .header("accept", "application/vnd.github+json") + .send() + .await + .map_err(|e| format!("Could not list Postgres builds: {e}"))? + .error_for_status() + .map_err(|e| format!("Could not list Postgres builds: {e}"))? + .json() + .await + .map_err(|e| e.to_string())?; + let mut tags: Vec> = releases + .iter() + .filter_map(|r| r.get("tag_name")?.as_str()) + .filter(|t| t.split('.').next() == Some(major)) + .map(|t| t.split('.').filter_map(|p| p.parse().ok()).collect()) + .collect(); + tags.sort(); + tags.pop() + .map(|v| v.iter().map(u32::to_string).collect::>().join(".")) + .ok_or_else(|| format!("There's no Postgres {major} build for this computer")) +} + +/// Download and unpack a Postgres build once; servers of that version share it. +async fn pg_install(app: &AppHandle, id: &str, major: &str) -> Result { + let target = pg_target().ok_or("Native Postgres isn't available for this computer. Pick Docker.")?; + let builds = root(app)?.join("builds"); + // A version already unpacked: no network at all. + if let Ok(rd) = std::fs::read_dir(&builds) { + let mut have: Vec = rd.flatten().map(|e| e.path()).filter(|p| { + p.file_name().and_then(|n| n.to_str()).is_some_and(|n| n.starts_with(&format!("postgresql-{major}."))) + && pg_bin(p).is_some() + }).collect(); + have.sort(); + if let Some(p) = have.pop() { + return Ok(p); + } + } + progress(app, id, &format!("Finding the newest Postgres {major}"), None); + let version = pg_release(major).await?; + let name = format!("postgresql-{version}-{target}.tar.gz"); + let base = format!("https://github.com/theseus-rs/postgresql-binaries/releases/download/{version}/{name}"); + let client = reqwest::Client::builder().user_agent("stroke-desktop").build().map_err(|e| e.to_string())?; + let sum = client.get(format!("{base}.sha256")).send().await.and_then(|r| r.error_for_status()).map_err(|e| e.to_string())? + .text().await.map_err(|e| e.to_string())?; + let want = sum.split_whitespace().next().unwrap_or_default().to_ascii_lowercase(); + + let mut resp = client.get(&base).send().await.and_then(|r| r.error_for_status()).map_err(|e| format!("Could not download Postgres {version}: {e}"))?; + let total = resp.content_length(); + let mut bytes = Vec::with_capacity(total.unwrap_or(0) as usize); + let mut last = 0.0; + while let Some(chunk) = resp.chunk().await.map_err(|e| format!("Download stopped: {e}"))? { + bytes.extend_from_slice(&chunk); + if let Some(t) = total { + let f = bytes.len() as f64 / t as f64; + if f - last >= 0.02 { + last = f; + progress(app, id, &format!("Downloading Postgres {version}"), Some(f)); + } + } + } + use sha2::Digest; + let got = hex::encode(sha2::Sha256::digest(&bytes)); + if want.len() == 64 && got != want { + return Err("The Postgres download didn't match its checksum. Try again.".into()); + } + progress(app, id, &format!("Unpacking Postgres {version}"), None); + let dest = builds.join(format!("postgresql-{version}")); + let tmp = builds.join(format!(".postgresql-{version}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&tmp); + std::fs::create_dir_all(&tmp).map_err(|e| e.to_string())?; + let unpack = tmp.clone(); + tokio::task::spawn_blocking(move || { + tar::Archive::new(flate2::read::GzDecoder::new(bytes.as_slice())).unpack(&unpack) + }) + .await + .map_err(|e| e.to_string())? + .map_err(|e| format!("Could not unpack Postgres: {e}"))?; + let _ = std::fs::remove_dir_all(&dest); + std::fs::rename(&tmp, &dest).map_err(|e| e.to_string())?; + pg_bin(&dest).ok_or("The Postgres download has no pg_ctl")?; + Ok(dest) +} + +/// The `bin` folder inside an unpacked build (it sits one folder down). +fn pg_bin(build: &Path) -> Option { + let exe = if cfg!(windows) { "pg_ctl.exe" } else { "pg_ctl" }; + let direct = build.join("bin"); + if direct.join(exe).is_file() { + return Some(direct); + } + std::fs::read_dir(build).ok()?.flatten().map(|e| e.path().join("bin")).find(|b| b.join(exe).is_file()) +} + +fn bin_exe(dir: &Path, name: &str) -> PathBuf { + dir.join(if cfg!(windows) { format!("{name}.exe") } else { name.to_string() }) +} + +/// The build a native Postgres server runs: recorded at create time. +fn pg_build_of(app: &AppHandle, id: &str) -> Result { + let p = std::fs::read_to_string(server_dir(app, id)?.join("build")).map_err(|_| "This server's Postgres build is missing. Delete it and create it again.".to_string())?; + let build = PathBuf::from(p.trim()); + pg_bin(&build).ok_or_else(|| "This server's Postgres build is missing. Delete it and create it again.".to_string()) +} + +// ── Native process bookkeeping ────────────────────────────────────────────── + +fn pid_file(app: &AppHandle, id: &str) -> Result { + Ok(server_dir(app, id)?.join("server.pid")) +} + +fn pid_alive(pid: u32) -> bool { + use sysinfo::{Pid, ProcessesToUpdate, System}; + let mut sys = System::new(); + let p = Pid::from_u32(pid); + sys.refresh_processes(ProcessesToUpdate::Some(&[p]), true); + sys.process(p).is_some() +} + +fn read_pid(app: &AppHandle, id: &str) -> Option { + std::fs::read_to_string(pid_file(app, id).ok()?).ok()?.lines().next()?.trim().parse().ok() +} + +fn kill_pid(pid: u32) { + use sysinfo::{Pid, ProcessesToUpdate, Signal, System}; + let mut sys = System::new(); + let p = Pid::from_u32(pid); + sys.refresh_processes(ProcessesToUpdate::Some(&[p]), true); + if let Some(proc_) = sys.process(p) { + if proc_.kill_with(Signal::Term).is_none() { + proc_.kill(); + } + } +} + +/// Start a long-running server process, detached from the app, its output in `log`. +fn spawn_detached(program: &Path, args: &[String], log: &Path) -> Result { + let out = std::fs::File::create(log).map_err(|e| e.to_string())?; + let err = out.try_clone().map_err(|e| e.to_string())?; + let mut cmd = std::process::Command::new(program); + cmd.args(args).stdin(std::process::Stdio::null()).stdout(out).stderr(err); + #[cfg(unix)] + { + use std::os::unix::process::CommandExt; + // Its own process group: closing the app or Ctrl+C in a terminal leaves it running. + cmd.process_group(0); + } + crate::proc::quiet_std(&mut cmd); + let child = cmd.spawn().map_err(|e| format!("Could not start {}: {e}", program.display()))?; + Ok(child.id()) +} + +fn log_tail(path: &Path) -> String { + let text = std::fs::read_to_string(path).unwrap_or_default(); + let lines: Vec<&str> = text.lines().rev().take(6).collect(); + lines.into_iter().rev().collect::>().join("\n") +} + +// ── Status ────────────────────────────────────────────────────────────────── + +async fn docker_running(id: &str) -> Result { + let d = docker()?; + match run(&d, &["inspect", "-f", "{{.State.Running}}", &container(id)]).await { + Ok(s) => Ok(s.trim() == "true"), + Err(e) if e.contains("No such") => Err("The container is gone. Delete this server and create it again.".into()), + Err(e) => Err(e), + } +} + +async fn state_of(app: &AppHandle, s: LocalServer, docker_ok: bool) -> ServerState { + let (running, problem) = if s.runtime == "docker" { + if !docker_ok { + (false, Some("Docker isn't running".to_string())) + } else { + match docker_running(&s.id).await { + Ok(r) => (r, None), + Err(e) => (false, Some(e)), + } + } + } else if s.engine == "postgres" { + match pg_build_of(app, &s.id) { + Ok(bin) => { + let data = server_dir(app, &s.id).map(|d| d.join("data")).unwrap_or_default(); + let up = run(&bin_exe(&bin, "pg_ctl"), &["status", "-D", &data.to_string_lossy()]).await.is_ok(); + (up, None) + } + Err(e) => (false, Some(e)), + } + } else { + let up = read_pid(app, &s.id).is_some_and(pid_alive); + let problem = if up || native_binary(&s.engine).is_some() { None } else { Some(format!("{} isn't installed anymore", s.engine)) }; + (up, problem) + }; + ServerState { server: s, running, problem } +} + +// ── Commands ──────────────────────────────────────────────────────────────── + +#[tauri::command] +pub async fn local_servers_env() -> Result { + let docker_installed = which("docker").is_some(); + let docker = docker_installed && docker_up().await; + let native = ["mysql", "mariadb", "redis", "mongodb"] + .into_iter() + .filter(|e| native_binary(e).is_some()) + .map(String::from) + .collect(); + Ok(Environment { docker, docker_installed, brew: which("brew").is_some(), native }) +} + +#[tauri::command] +pub async fn local_servers_list(app: AppHandle) -> Result, String> { + let servers = load(&app)?; + let docker_ok = if servers.iter().any(|s| s.runtime == "docker") { docker_up().await } else { false }; + let mut out = Vec::with_capacity(servers.len()); + for s in servers { + out.push(state_of(&app, s, docker_ok).await); + } + Ok(out) +} + +#[derive(Debug, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct NewServer { + pub name: String, + pub engine: String, + pub version: String, + pub port: u16, + pub runtime: String, +} + +/// Check a new server before anything is downloaded or created. +fn validate(spec: &NewServer, existing: &[LocalServer]) -> Result<(), String> { + if !ENGINES.contains(&spec.engine.as_str()) { + return Err(format!("{} isn't supported", spec.engine)); + } + if !valid_version(&spec.version) { + return Err(format!("\"{}\" isn't a version", spec.version)); + } + if spec.port < 1024 { + return Err("Pick a port from 1024 up".into()); + } + if spec.name.trim().is_empty() || spec.name.len() > 60 { + return Err("Give the server a name".into()); + } + if let Some(other) = existing.iter().find(|s| s.port == spec.port) { + return Err(format!("{} already uses port {}", other.name, spec.port)); + } + match spec.runtime.as_str() { + "docker" => Ok(()), + "native" if spec.engine == "postgres" => { + if spec.version.contains('.') { + Err("Native Postgres takes a major version, like 16".into()) + } else { + Ok(()) + } + } + "native" if native_binary(&spec.engine).is_some() => Ok(()), + "native" => Err(format!("{} isn't installed on this computer. Pick Docker.", spec.engine)), + other => Err(format!("Unknown runtime {other}")), + } +} + +#[tauri::command] +pub async fn local_server_create(app: AppHandle, spec: NewServer) -> Result { + let mut servers = load(&app)?; + validate(&spec, &servers)?; + // Before any download: a port something else listens on can never start. + if port_busy(spec.port).await { + let free = free_port(spec.port + 1, &servers).await; + return Err(format!("Port {} is in use on this computer. Try {free}.", spec.port)); + } + let id = uuid::Uuid::new_v4().simple().to_string()[..12].to_string(); + let server = LocalServer { + id: id.clone(), + name: spec.name.trim().to_string(), + engine: spec.engine.clone(), + version: spec.version.clone(), + port: spec.port, + runtime: spec.runtime.clone(), + created_at: chrono::Utc::now().timestamp_millis(), + }; + let dir = server_dir(&app, &id)?; + let made = async { + if server.runtime == "docker" { + let d = docker()?; + if !docker_up().await { + return Err("Docker isn't running. Start Docker Desktop and try again.".into()); + } + let (image, env) = docker_image(&server.engine, &server.version)?; + progress(&app, &id, &format!("Pulling {image}"), None); + run(&d, &["pull", &image]).await?; + let publish = format!("127.0.0.1:{}:{}", server.port, default_port(&server.engine)); + let volume = format!("{}:{}", container(&id), docker_data_path(&server.engine)); + let name = container(&id); + let mut args = vec!["create", "--name", &name, "-p", &publish, "-v", &volume, "--label", "app=stroke"]; + for e in &env { + args.push("-e"); + args.push(e); + } + args.push(&image); + progress(&app, &id, "Creating the container", None); + run(&d, &args).await?; + } else { + std::fs::create_dir_all(&dir).map_err(|e| e.to_string())?; + let data = dir.join("data"); + match server.engine.as_str() { + "postgres" => { + let build = pg_install(&app, &id, &server.version).await?; + std::fs::write(dir.join("build"), build.to_string_lossy().as_bytes()).map_err(|e| e.to_string())?; + let bin = pg_bin(&build).ok_or("The Postgres build has no bin folder")?; + progress(&app, &id, "Creating the database cluster", None); + run(&bin_exe(&bin, "initdb"), &["-D", &data.to_string_lossy(), "-U", "postgres", "-A", "trust", "-E", "UTF8", "--no-locale"]).await?; + } + "mysql" | "mariadb" => { + let bin = native_binary(&server.engine).ok_or("mysqld isn't installed")?; + std::fs::create_dir_all(&data).map_err(|e| e.to_string())?; + progress(&app, &id, "Creating the data directory", None); + let datadir = format!("--datadir={}", data.to_string_lossy()); + if server.engine == "mariadb" || bin.file_name().is_some_and(|n| n.to_string_lossy().starts_with("mariadb")) { + let install = which("mariadb-install-db").or_else(|| which("mysql_install_db")).ok_or("mariadb-install-db isn't installed")?; + run(&install, &[&datadir, "--auth-root-authentication-method=normal"]).await?; + } else { + run(&bin, &["--initialize-insecure", &datadir]).await?; + } + } + _ => { + std::fs::create_dir_all(&data).map_err(|e| e.to_string())?; + } + } + } + Ok::<(), String>(()) + } + .await; + if let Err(e) = made { + let _ = std::fs::remove_dir_all(&dir); + if server.runtime == "docker" { + if let Ok(d) = docker() { + let _ = run(&d, &["rm", "-f", &container(&id)]).await; + } + } + return Err(e); + } + servers.push(server.clone()); + store(&app, &servers)?; + progress(&app, &id, "Ready", Some(1.0)); + Ok(server) +} + +#[tauri::command] +pub async fn local_server_start(app: AppHandle, id: String) -> Result<(), String> { + let s = find(&app, &id)?; + if port_busy(s.port).await { + if state_of(&app, s.clone(), true).await.running { + return Ok(()); + } + return Err(format!("Something else is using port {}. Stop it, or change this server's port.", s.port)); + } + let dir = server_dir(&app, &id)?; + let data = dir.join("data"); + let log = dir.join("server.log"); + if s.runtime == "docker" { + run(&docker()?, &["start", &container(&id)]).await?; + } else { + match s.engine.as_str() { + "postgres" => { + let bin = pg_build_of(&app, &id)?; + let mut opts = format!("-p {} -c listen_addresses=127.0.0.1", s.port); + if cfg!(unix) { + // No Unix socket: Stroke connects over TCP, two versions can't + // fight over /tmp/.s.PGSQL.5432, and a socket path under + // macOS's "Application Support" can pass the 107-byte limit. + opts.push_str(" -k \"\""); + } + run(&bin_exe(&bin, "pg_ctl"), &["start", "-D", &data.to_string_lossy(), "-l", &log.to_string_lossy(), "-o", &opts, "-w", "-t", "60"]) + .await + .map_err(|e| format!("{e}\n{}", log_tail(&log)))?; + } + "mysql" | "mariadb" => { + let bin = native_binary(&s.engine).ok_or("mysqld isn't installed")?; + let mut args = vec![ + format!("--datadir={}", data.to_string_lossy()), + format!("--port={}", s.port), + "--bind-address=127.0.0.1".to_string(), + format!("--pid-file={}", dir.join("mysqld.pid").to_string_lossy()), + ]; + if cfg!(unix) { + args.push(format!("--socket={}", dir.join("mysql.sock").to_string_lossy())); + } + let pid = spawn_detached(&bin, &args, &log)?; + std::fs::write(pid_file(&app, &id)?, pid.to_string()).map_err(|e| e.to_string())?; + } + "redis" => { + let bin = native_binary("redis").ok_or("redis-server isn't installed")?; + let args = vec![ + "--port".to_string(), s.port.to_string(), + "--bind".to_string(), "127.0.0.1".to_string(), + "--dir".to_string(), data.to_string_lossy().to_string(), + "--save".to_string(), "60 1".to_string(), + ]; + let pid = spawn_detached(&bin, &args, &log)?; + std::fs::write(pid_file(&app, &id)?, pid.to_string()).map_err(|e| e.to_string())?; + } + "mongodb" => { + let bin = native_binary("mongodb").ok_or("mongod isn't installed")?; + let args = vec![ + "--port".to_string(), s.port.to_string(), + "--bind_ip".to_string(), "127.0.0.1".to_string(), + "--dbpath".to_string(), data.to_string_lossy().to_string(), + ]; + let pid = spawn_detached(&bin, &args, &log)?; + std::fs::write(pid_file(&app, &id)?, pid.to_string()).map_err(|e| e.to_string())?; + } + e => return Err(format!("{e} isn't supported")), + } + } + if !wait_for_port(s.port, 60).await { + let tail = if s.runtime == "docker" { + run(&docker()?, &["logs", "--tail", "6", &container(&id)]).await.unwrap_or_default() + } else { + log_tail(&log) + }; + return Err(format!("{} started but isn't answering on port {}.\n{tail}", s.name, s.port).trim().to_string()); + } + Ok(()) +} + + +#[tauri::command] +pub async fn local_server_stop(app: AppHandle, id: String) -> Result<(), String> { + let s = find(&app, &id)?; + if s.runtime == "docker" { + run(&docker()?, &["stop", &container(&id)]).await?; + return Ok(()); + } + if s.engine == "postgres" { + let bin = pg_build_of(&app, &id)?; + let data = server_dir(&app, &id)?.join("data"); + run(&bin_exe(&bin, "pg_ctl"), &["stop", "-D", &data.to_string_lossy(), "-m", "fast", "-w"]).await?; + return Ok(()); + } + if let Some(pid) = read_pid(&app, &id) { + kill_pid(pid); + for _ in 0..50 { + if !pid_alive(pid) { + break; + } + tokio::time::sleep(Duration::from_millis(200)).await; + } + let _ = std::fs::remove_file(pid_file(&app, &id)?); + } + Ok(()) +} + +#[tauri::command] +pub async fn local_server_delete(app: AppHandle, id: String) -> Result<(), String> { + let s = find(&app, &id)?; + let _ = local_server_stop(app.clone(), id.clone()).await; + if s.runtime == "docker" { + if let Ok(d) = docker() { + let name = container(&id); + let _ = run(&d, &["rm", "-f", &name]).await; + let _ = run(&d, &["volume", "rm", &name]).await; + } + } + let _ = std::fs::remove_dir_all(server_dir(&app, &id)?); + let servers: Vec = load(&app)?.into_iter().filter(|x| x.id != id).collect(); + store(&app, &servers) +} + +/// Rename a server or move it to another port (it has to be stopped for a port change). +#[tauri::command] +pub async fn local_server_update(app: AppHandle, id: String, name: Option, port: Option) -> Result { + let mut servers = load(&app)?; + let idx = servers.iter().position(|s| s.id == id).ok_or("That server no longer exists")?; + if let Some(n) = name.map(|n| n.trim().to_string()).filter(|n| !n.is_empty()) { + servers[idx].name = n; + } + if let Some(p) = port.filter(|p| *p != servers[idx].port) { + if p < 1024 { + return Err("Pick a port from 1024 up".into()); + } + if let Some(o) = servers.iter().find(|s| s.port == p) { + return Err(format!("{} already uses port {p}", o.name)); + } + if state_of(&app, servers[idx].clone(), docker_up().await).await.running { + return Err("Stop the server before changing its port".into()); + } + if servers[idx].runtime == "docker" { + // A container's published port is fixed: make a new one on the same volume. + let s = servers[idx].clone(); + let d = docker()?; + let (image, env) = docker_image(&s.engine, &s.version)?; + let name = container(&id); + run(&d, &["rm", "-f", &name]).await?; + let publish = format!("127.0.0.1:{p}:{}", default_port(&s.engine)); + let volume = format!("{name}:{}", docker_data_path(&s.engine)); + let mut args = vec!["create", "--name", &name, "-p", &publish, "-v", &volume, "--label", "app=stroke"]; + for e in &env { + args.push("-e"); + args.push(e); + } + args.push(&image); + run(&d, &args).await?; + } + servers[idx].port = p; + } + store(&app, &servers)?; + Ok(servers[idx].clone()) +} + +/// The last lines a server wrote, for when it won't start. +#[tauri::command] +pub async fn local_server_logs(app: AppHandle, id: String) -> Result { + let s = find(&app, &id)?; + if s.runtime == "docker" { + let d = docker()?; + let mut cmd = Command::new(&d); + cmd.args(["logs", "--tail", "200", &container(&id)]); + let out = quiet(&mut cmd).output().await.map_err(|e| e.to_string())?; + // Server images log to stderr; keep both, in order of the streams. + return Ok(format!("{}{}", String::from_utf8_lossy(&out.stdout), String::from_utf8_lossy(&out.stderr))); + } + let text = std::fs::read_to_string(server_dir(&app, &id)?.join("server.log")).unwrap_or_default(); + let lines: Vec<&str> = text.lines().collect(); + Ok(lines[lines.len().saturating_sub(200)..].join("\n")) +} + +// ── Homebrew ──────────────────────────────────────────────────────────────── + +/// Database services Homebrew knows about (postgresql@16, mysql, redis, ...). +#[tauri::command] +pub async fn brew_services_list() -> Result, String> { + let Some(brew) = which("brew") else { return Ok(Vec::new()) }; + let out = run(&brew, &["services", "list", "--json"]).await?; + let v: Vec = serde_json::from_str(&out).unwrap_or_default(); + Ok(v.into_iter() + .filter_map(|s| { + let name = s.get("name")?.as_str()?.to_string(); + let status = s.get("status").and_then(|x| x.as_str()).unwrap_or("none").to_string(); + let db = ["postgres", "mysql", "mariadb", "redis", "mongo", "valkey", "memcached", "clickhouse"].iter().any(|k| name.contains(k)); + db.then(|| BrewService { running: status == "started", name, status }) + }) + .collect()) +} + +#[tauri::command] +pub async fn brew_service_action(name: String, action: String) -> Result<(), String> { + if !matches!(action.as_str(), "start" | "stop" | "restart") { + return Err(format!("Unknown action {action}")); + } + if !name.chars().all(|c| c.is_ascii_alphanumeric() || "@.-_".contains(c)) { + return Err("That isn't a Homebrew service".into()); + } + let brew = which("brew").ok_or("Homebrew isn't installed")?; + run(&brew, &["services", &action, &name]).await.map(|_| ()) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn spec(engine: &str, version: &str, port: u16, runtime: &str) -> NewServer { + NewServer { name: "t".into(), engine: engine.into(), version: version.into(), port, runtime: runtime.into() } + } + + #[test] + fn new_servers_are_checked_first() { + assert!(validate(&spec("postgres", "16", 5433, "docker"), &[]).is_ok()); + assert!(validate(&spec("postgres", "16", 5433, "native"), &[]).is_ok()); + assert!(validate(&spec("postgres", "16.4", 5433, "native"), &[]).unwrap_err().contains("major")); + assert!(validate(&spec("oracle", "23", 1521, "docker"), &[]).is_err()); + assert!(validate(&spec("redis", "7; rm -rf /", 6380, "docker"), &[]).is_err()); + assert!(validate(&spec("redis", "7", 80, "docker"), &[]).unwrap_err().contains("1024")); + let taken = LocalServer { id: "a".into(), name: "Air".into(), engine: "postgres".into(), version: "16".into(), port: 5432, runtime: "docker".into(), created_at: 0 }; + assert!(validate(&spec("mysql", "8.4", 5432, "docker"), &[taken]).unwrap_err().contains("Air")); + } + + #[test] + fn docker_images_and_ports() { + assert_eq!(docker_image("mongodb", "8.0").unwrap().0, "mongo:8.0"); + assert_eq!(docker_image("postgres", "17").unwrap().1, vec!["POSTGRES_HOST_AUTH_METHOD=trust"]); + assert!(docker_image("mysql", "8.4 --privileged").is_err()); + assert_eq!(default_port("mariadb"), 3306); + assert_eq!(docker_data_path("mongodb"), "/data/db"); + } + + #[test] + fn a_postgres_build_is_found_one_folder_down() { + let dir = std::env::temp_dir().join(format!("stroke-pgbin-{}", std::process::id())); + let bin = dir.join("postgresql-16.0.0-x").join("bin"); + std::fs::create_dir_all(&bin).unwrap(); + std::fs::write(bin.join(if cfg!(windows) { "pg_ctl.exe" } else { "pg_ctl" }), b"").unwrap(); + assert_eq!(pg_bin(&dir), Some(bin)); + let _ = std::fs::remove_dir_all(&dir); + } +} diff --git a/src-tauri/src/mac_chrome.rs b/src-tauri/src/mac_chrome.rs index c4e8d951..c7d0b891 100644 --- a/src-tauri/src/mac_chrome.rs +++ b/src-tauri/src/mac_chrome.rs @@ -106,10 +106,14 @@ const TOOLS_ITEMS: &[&[Item]] = &[ ], &[ ("dataDiff", "Data Diff", None, true), + ("migrations", "Migrations", None, true), ("dashboard", "Dashboard", None, true), ("logs", "Activity Log", Some("CmdOrCtrl+Shift+L"), true), ], - &[("extensions", "Extensions", Some("CmdOrCtrl+Shift+X"), false)], + &[ + ("localServers", "Local Servers…", None, false), + ("extensions", "Extensions", Some("CmdOrCtrl+Shift+X"), false), + ], ]; const HELP_ITEMS: &[&[Item]] = &[ diff --git a/src/lib/components/AppMenuBar.svelte b/src/lib/components/AppMenuBar.svelte index 6338a664..7b273d78 100644 --- a/src/lib/components/AppMenuBar.svelte +++ b/src/lib/components/AppMenuBar.svelte @@ -136,9 +136,11 @@ {@render item("network", "Schema diagram", "erd", "", !connected)} {@render item("git-compare", "Data diff", "dataDiff", "", !connected)} + {@render item("git-merge", "Migrations", "migrations", "", !connected)} {@render item("layout-dashboard", "Dashboard", "dashboard", "", !connected)} {@render item("history", "Activity log", "logs", "view.logs", !connected)} + {@render item("database", "Local servers", "localServers", "")} {@render item("blocks", "Extensions", "extensions", "view.extensions")} diff --git a/src/lib/components/CommandPalette.svelte b/src/lib/components/CommandPalette.svelte index 1386111a..7afc05df 100644 --- a/src/lib/components/CommandPalette.svelte +++ b/src/lib/components/CommandPalette.svelte @@ -112,6 +112,8 @@ onopennotebookfile = () => {}, openschematimeline = () => {}, opendatadiff = () => {}, + openmigrations = () => {}, + openlocalservers = () => {}, /** Live AI schema/db context for the inline quick-ask. */ schemaContext = null, /** Escalate a quick-ask question into the full AI sidebar/chat. @param {string} q */ @@ -275,6 +277,8 @@ { icon: 'archive', label: 'Backup & restore', action: onopenbackup, show: connected && !isKeyValue, value: 'backup restore export import dump data' }, { icon: 'braces', label: 'JSON viewer', action: onopenJsonViewer, show: connected, value: 'open json viewer explorer jsonpath tool' }, { icon: 'git-branch', label: 'Schema timeline', action: openschematimeline, show: connected && !isKeyValue, value: 'schema timeline drift detection history changes diff' }, + { icon: 'database', label: 'Local servers', action: openlocalservers, show: true, value: 'local servers database postgres mysql mariadb redis mongodb docker native homebrew dbngin start stop version' }, + { icon: 'git-merge', label: 'Migrations', action: openmigrations, show: connected && !isKeyValue, value: 'migrations prisma drizzle applied pending failed migrate schema history' }, { icon: 'git-compare', label: 'Data diff', action: opendatadiff, show: connected && !isKeyValue, value: 'data diff compare tables rows changes' }, { icon: 'file-text', label: 'New notebook', action: onopennotebook, show: connected && !isKeyValue, value: 'new sql notebook jupyter cells sql markdown' }, { icon: 'file-text', label: 'Open notebook…', action: onopennotebookfile, show: connected && !isKeyValue, value: 'open notebook file sqlnb' }, diff --git a/src/lib/components/LocalServersDialog.svelte b/src/lib/components/LocalServersDialog.svelte new file mode 100644 index 00000000..2e8d6e29 --- /dev/null +++ b/src/lib/components/LocalServersDialog.svelte @@ -0,0 +1,481 @@ + + + + + {#if view === 'list'} + + Local servers + Database servers running on this computer. + + + + +
+ {#if env && !env.docker} +

+ + {env.dockerInstalled ? "Docker isn't running. Start Docker Desktop for Docker servers; native Postgres works without it." : "Docker isn't installed. Native Postgres works without it."} +

+ {/if} + + {#if servers.length === 0 && !loading} +
+ +
+

No local servers yet

+

Postgres, MySQL, MariaDB, Redis or MongoDB, any version, ready in a minute.

+
+ +
+ {:else} +
    + {#each servers as s (s.id)} + {@const e = engineOf(s.engine)} +
  • + + + + +
    +

    {s.name}

    +

    + {#if s.problem}{s.problem} + {:else}{e.label} {s.version} · {s.running ? `127.0.0.1:${s.port}` : `port ${s.port}`} · {runtimeLabel(s.runtime)}{/if} +

    +
    + + + + + + + + void showLogs(s)}>Logs + void editPort(s)}>Change port… + + void remove(s)}>Delete with its data + + +
  • + {/each} +
+ {/if} + + {#if env?.brew && brew.length} +
+ + {#if brewOpen} +
    + {#each brew as b (b.name)} +
  • + {b.name} + +
  • + {/each} +
+ {/if} +
+ {/if} +
+ {:else if view === 'port' && portFor} + + Change the port of {portFor.name} + Pick a new port for this server. + +
{ e.preventDefault(); void savePort() }}> + + {#if portError}{/if} +
+ + +
+
+ {:else if view === 'logs'} + + {logsFor} logs + The last lines the server wrote. + + +
{logs}
+ {:else} + + New local server + Pick an engine, a version and where it runs. + +
{ e.preventDefault(); void create() }}> +
+ Engine +
+ {#each ENGINES as e (e.id)} + {@const on = fEngine === e.id} + + {/each} +
+
+
+ Version +
+ {#each engine.versions as v (v)} + {@const on = fVersion === v} + + {/each} +
+
+
+ Runs on +
+ {#each [ + { id: /** @type {const} */ ('docker'), Icon: Container, title: 'Docker', ok: dockerOk, + body: dockerOk ? 'A container on 127.0.0.1, its data in a Docker volume.' : env?.dockerInstalled ? 'Start Docker Desktop to use this.' : 'Install Docker Desktop to use this.' }, + { id: /** @type {const} */ ('native'), Icon: Cpu, title: 'Native', ok: nativeOk, + body: fEngine === 'postgres' ? `Downloads Postgres ${fVersion} once, about 30 MB.` : nativeOk ? `Uses the ${engine.label} installed here.` : `${engine.label} isn't installed on this computer.` }, + ] as r (r.id)} + {@const on = fRuntime === r.id} + + {/each} +
+
+
+ + +
+ {#if portTaken}

{portTaken.name} already uses port {portNum}.

{/if} + {#if progress} +
+

{progress.message}

+
+
+
+
+ {/if} + {#if formError}{/if} +
+ + +
+
+ {/if} +
+
diff --git a/src/lib/components/MigrationsPage.svelte b/src/lib/components/MigrationsPage.svelte new file mode 100644 index 00000000..95905437 --- /dev/null +++ b/src/lib/components/MigrationsPage.svelte @@ -0,0 +1,296 @@ + + +
+ +
+ setTool(v)} + /> + {#if list.length} +

+ {c.applied} applied + {#if files}{c.pending} pending{/if} + {#if c.failed}{c.failed} failed{/if} + {#if c.rolledBack}{c.rolledBack} rolled back{/if} +

+ {/if} + + + +
+ +
+
+ {#if projectError} + + {:else if !projectDir && list.length} +

+ Showing what ran. to see pending migrations too. +

+ {/if} + + {#if error} + + {:else if neither} +
+ +
+

No migrations in this database yet

+

Neither _prisma_migrations nor __drizzle_migrations is here. Pick the project folder to see what would run.

+
+ +
+ {:else if !loading && !list.length} +

+ {tool === 'prisma' ? 'No Prisma migrations' : 'No Drizzle migrations'} {rows === null ? 'in this database' : 'yet'}. +

+ {:else} +
    + {#each list as m (m.name)} + {@const st = STATUS[m.status]} + {@const expanded = open === m.name} +
  1. + + {#if expanded} +
    + {#if m.durationMs != null || m.steps != null} +

    + {[m.durationMs != null ? `Took ${took(m.durationMs)}` : '', m.steps != null ? `${m.steps} step${m.steps === 1 ? '' : 's'} applied` : ''].filter(Boolean).join(' · ')} +

    + {/if} + {#if m.logs} +
    {m.logs}
    + {/if} + {#if m.sql} +
    + {#await import('./CodeEditor.svelte') then { default: CodeEditor }} + + {/await} +
    +
    + {#if onopensql} + + {/if} + +
    + {:else if !files} +

    Pick the project folder to see this migration's SQL.

    + {/if} +
    + {/if} +
  2. + {/each} +
+ {/if} +
+
+
diff --git a/src/lib/components/StudioShell.svelte b/src/lib/components/StudioShell.svelte index 1803ebdd..b03354a2 100644 --- a/src/lib/components/StudioShell.svelte +++ b/src/lib/components/StudioShell.svelte @@ -229,6 +229,8 @@ findSchemaTimelineTab, createDataDiffTab, findDataDiffTab, + createMigrationsTab, + findMigrationsTab, createLicenseTab, createAiTab, findAiTab, @@ -576,6 +578,7 @@ } } let showDockerModal = $state(false) + let showLocalServers = $state(false) let dockerInitialDb = $state(/** @type {string | null} */ (null)) /** Bottom query-log console visibility. */ let queryLogOpen = $state(false) @@ -1158,6 +1161,7 @@ let searchEverOpened = $state(false) let schemaTimelineEverOpened = $state(false) let dataDiffEverOpened = $state(false) + let migrationsEverOpened = $state(false) let ormSchemaEverOpened = $state(false) /** @type {{ focusEditor: () => void, openQuery?: (content: string) => void } | null} */ let sqlConsoleRef = $state(null) @@ -1459,6 +1463,7 @@ if (activeTab?.kind === 'search') searchEverOpened = true if (activeTab?.kind === 'schema-timeline') schemaTimelineEverOpened = true if (activeTab?.kind === 'data-diff') dataDiffEverOpened = true + if (activeTab?.kind === 'migrations') migrationsEverOpened = true if (activeTab?.kind === 'orm-schema') ormSchemaEverOpened = true }) @@ -1486,6 +1491,7 @@ { kind: 'erd', get: () => erdEverOpened, set: (/** @type {boolean} */ v) => (erdEverOpened = v) }, { kind: 'diagrams', get: () => diagramsEverOpened, set: (/** @type {boolean} */ v) => (diagramsEverOpened = v) }, { kind: 'schema-timeline', get: () => schemaTimelineEverOpened, set: (/** @type {boolean} */ v) => (schemaTimelineEverOpened = v) }, + { kind: 'migrations', get: () => migrationsEverOpened, set: (/** @type {boolean} */ v) => (migrationsEverOpened = v) }, { kind: 'orm-schema', get: () => ormSchemaEverOpened, set: (/** @type {boolean} */ v) => (ormSchemaEverOpened = v) }, ]) /** kind → Date.now() when it last became hidden; absent while active or unmounted. */ @@ -3471,6 +3477,8 @@ let rowSearch = $state('') schema: () => openSchemaTab(), erd: () => openErdTab(), dataDiff: () => openDataDiffTab(), + migrations: () => openMigrationsTab(), + localServers: () => { commandOpen = false; showLocalServers = true }, dashboard: () => openDashboardTab(), logs: () => openLogsTab(), extensions: () => openExtensionsTab(), @@ -4818,6 +4826,10 @@ let rowSearch = $state('') openSingletonTab({ find: findDataDiffTab, create: createDataDiffTab }) } + function openMigrationsTab() { + openSingletonTab({ find: findMigrationsTab, create: createMigrationsTab }) + } + /** * Update the state of a notebook tab (called by NotebookEditor via onupdate). * @param {string} tabId @@ -9920,6 +9932,11 @@ let rowSearch = $state('') dialect={dbType} onopeninsql={(sql) => { if (aiMode) exitAiMode(); void openQueryInEditor(sql) }} /> +{#if showLocalServers} + {#await import('./LocalServersDialog.svelte') then { default: LocalServersDialog }} + void handleDockerConnect(c)} /> + {/await} +{/if} {#if dialogMounted.docker} {#await lazyDialogs.docker() then { default: DockerLaunchModal }} { commandOpen = false; void openNotebookFromFile() }} openschematimeline={() => { commandOpen = false; openSchemaTimelineTab() }} opendatadiff={() => { commandOpen = false; openDataDiffTab() }} + openmigrations={() => { commandOpen = false; openMigrationsTab() }} + openlocalservers={() => { commandOpen = false; showLocalServers = true }} schemaContext={aiSchemaContext} onaskcontinue={handleAskContinue} /> @@ -11094,6 +11113,26 @@ let rowSearch = $state('') {/if} + + {#if migrationsEverOpened} +
+ + {#await import('./MigrationsPage.svelte')}{:then { default: MigrationsPage }} + openSqlTabWith(sql, title)} + /> + {/await} + +
+ {/if} + {#if dataDiffEverOpened}
diff --git a/src/lib/icon-registry.js b/src/lib/icon-registry.js index 44880d06..b278638b 100644 --- a/src/lib/icon-registry.js +++ b/src/lib/icon-registry.js @@ -93,6 +93,7 @@ import FlaskConical from '@lucide/svelte/icons/flask-conical' import Workflow from '@lucide/svelte/icons/workflow' import Share2 from '@lucide/svelte/icons/share-2' import GitCompare from '@lucide/svelte/icons/git-compare' +import GitMerge from '@lucide/svelte/icons/git-merge' import Blocks from '@lucide/svelte/icons/blocks' import Plug from '@lucide/svelte/icons/plug' import NotebookPen from '@lucide/svelte/icons/notebook-pen' @@ -163,7 +164,7 @@ import { HardDriveIcon, DashboardSquare02Icon, SquareUnlock01Icon, Moon02Icon, RadioIcon, ServerStack01Icon, Settings02Icon, Sun03Icon, ArrowTurnBackwardIcon, Unlink01Icon, WifiConnected01Icon, WifiDisconnected01Icon, TestTubeIcon, WorkflowSquare02Icon, - Share08Icon, GitCompareIcon, PuzzleIcon, PlugSocketIcon, NoteEditIcon, ChartBarLineIcon, GlobalIcon, + Share08Icon, GitCompareIcon, GitMergeIcon, PuzzleIcon, PlugSocketIcon, NoteEditIcon, ChartBarLineIcon, GlobalIcon, InformationCircleIcon, ArrowUpDownIcon, ArrowLeftRightIcon, Bookmark01Icon, BugIcon, UnavailableIcon, Target01Icon, EraserIcon, FileDownloadIcon, FileUploadIcon, File01Icon, FunctionSquareIcon, HashIcon, InfinityIcon, KeyboardIcon, Layers01Icon, LeftToRightListBulletIcon, Link01Icon, @@ -260,6 +261,7 @@ export const ICON_MAP = { 'workflow': { lucide: Workflow, huge: WorkflowSquare02Icon }, 'share-2': { lucide: Share2, huge: Share08Icon }, 'git-compare': { lucide: GitCompare, huge: GitCompareIcon }, + 'git-merge': { lucide: GitMerge, huge: GitMergeIcon }, 'blocks': { lucide: Blocks, huge: PuzzleIcon }, // Database objects (views, routines, types): not 'blocks', which is Extensions. 'shapes': { lucide: Shapes, huge: Shapes01Icon }, diff --git a/src/lib/local-servers.js b/src/lib/local-servers.js new file mode 100644 index 00000000..d7203471 --- /dev/null +++ b/src/lib/local-servers.js @@ -0,0 +1,72 @@ +// Local database servers (src-tauri/src/local_servers.rs): the IPC calls, the +// engines and versions on offer, and the connection a running server opens as. + +import { invoke } from '@tauri-apps/api/core' + +/** + * @typedef {{ id: string, name: string, engine: string, version: string, port: number, runtime: 'docker' | 'native', createdAt: number, running: boolean, problem: string | null }} LocalServer + * @typedef {{ docker: boolean, dockerInstalled: boolean, brew: boolean, native: string[] }} LocalEnv + * @typedef {{ name: string, status: string, running: boolean }} BrewService + */ + +/** Engines, their versions (newest first) and default port. */ +export const ENGINES = [ + { id: 'postgres', label: 'PostgreSQL', short: 'Pg', color: '#336791', port: 5432, versions: ['18', '17', '16', '15', '14', '13'] }, + { id: 'mysql', label: 'MySQL', short: 'My', color: '#f29111', port: 3306, versions: ['9.4', '8.4', '8.0'] }, + { id: 'mariadb', label: 'MariaDB', short: 'Ma', color: '#c0765a', port: 3306, versions: ['11.8', '11.4', '10.11'] }, + { id: 'redis', label: 'Redis', short: 'Re', color: '#d82c20', port: 6379, versions: ['8.2', '7.4', '7.2'] }, + { id: 'mongodb', label: 'MongoDB', short: 'Mo', color: '#47a248', port: 27017, versions: ['8.0', '7.0', '6.0'] }, +] + +/** @param {string} id */ +export const engineOf = (id) => ENGINES.find((e) => e.id === id) ?? ENGINES[0] + +/** + * The first port from the engine's default up that no server here uses. + * @param {string} engine @param {{ port: number }[]} servers + */ +export function nextPort(engine, servers) { + const taken = new Set(servers.map((s) => s.port)) + let p = engineOf(engine).port + while (taken.has(p)) p++ + return p +} + +/** + * The saved connection a server opens as: 127.0.0.1, its port, the engine's + * default superuser and no password (local servers run with trust auth). + * @param {LocalServer} s + */ +export function connectionFor(s) { + const base = { id: `local-${s.id}`, name: s.name, host: '127.0.0.1', port: s.port, environment: /** @type {'dev'} */ ('dev') } + switch (s.engine) { + case 'postgres': return { ...base, type: 'postgres', user: 'postgres', password: '', database: 'postgres' } + case 'mysql': + case 'mariadb': return { ...base, type: s.engine, user: 'root', password: '', database: '' } + case 'redis': return { ...base, type: 'redis', db: 0, password: '' } + default: return { ...base, type: 'mongodb', uri: `mongodb://127.0.0.1:${s.port}/?directConnection=true`, database: '' } + } +} + +/** @returns {Promise} */ +export const localEnv = () => invoke('local_servers_env') +/** @returns {Promise} */ +export const listServers = () => invoke('local_servers_list') +/** @param {{ name: string, engine: string, version: string, port: number, runtime: string }} spec */ +export const createServer = (spec) => invoke('local_server_create', { spec }) +/** @param {string} id */ +export const startServer = (id) => invoke('local_server_start', { id }) +/** @param {string} id */ +export const stopServer = (id) => invoke('local_server_stop', { id }) +/** @param {string} id */ +export const deleteServer = (id) => invoke('local_server_delete', { id }) +/** @param {string} id @param {{ name?: string, port?: number }} patch */ +export const updateServer = (id, patch) => invoke('local_server_update', { id, name: patch.name ?? null, port: patch.port ?? null }) +/** @param {string} id @returns {Promise} */ +export const serverLogs = (id) => invoke('local_server_logs', { id }) +/** @param {number} from @returns {Promise} */ +export const freePort = (from) => invoke('local_server_free_port', { from }) +/** @returns {Promise} */ +export const brewServices = () => invoke('brew_services_list') +/** @param {string} name @param {'start' | 'stop'} action */ +export const brewAction = (name, action) => invoke('brew_service_action', { name, action }) diff --git a/src/lib/migrations.js b/src/lib/migrations.js new file mode 100644 index 00000000..0ae779ce --- /dev/null +++ b/src/lib/migrations.js @@ -0,0 +1,185 @@ +// The Migrations page's logic: what Prisma or Drizzle recorded in the +// database, merged with the migration files in the project folder, as one +// list with a status per migration. Pure: no IPC, no Svelte. +// +// Prisma keeps a row per attempt in `_prisma_migrations`: finished_at set = +// applied; neither finished_at nor rolled_back_at = failed (Prisma refuses to +// go on until it's resolved); rolled_back_at = rolled back. Its checksum is the +// SHA-256 of migration.sql. +// Drizzle keeps `__drizzle_migrations` (id, hash, created_at): a migration +// counts as applied when its journal `when` is at or before the newest +// created_at, which is how Drizzle's migrator decides; `hash` is the SHA-256 +// of the .sql file. Drizzle records no failures (a failed run rolls back). + +/** + * @typedef {'applied' | 'pending' | 'failed' | 'rolled-back'} MigrationStatus + * @typedef {{ + * name: string, + * status: MigrationStatus, + * appliedAt: number | null, + * startedAt: number | null, + * durationMs: number | null, + * steps: number | null, + * logs: string | null, + * sql: string | null, + * changed: boolean, + * missingFile: boolean, + * attempts: number, + * }} Migration + * @typedef {{ name: string, sql: string, checksum: string, when: number | null }} MigrationFile + */ + +/** @param {unknown} v @returns {number | null} */ +export function toMs(v) { + if (v == null || v === '') return null + if (typeof v === 'number') return v < 1e11 ? v * 1000 : v + if (typeof v === 'bigint') return Number(v) + const s = String(v).trim() + if (/^\d+$/.test(s)) return toMs(Number(s)) + // "2024-01-02 10:00:00.123" from MySQL/SQLite has no zone: Prisma writes UTC. + const iso = /^\d{4}-\d{2}-\d{2} \d/.test(s) && !/[zZ]|[+-]\d{2}:?\d{2}$/.test(s) ? `${s.replace(' ', 'T')}Z` : s + const t = Date.parse(iso) + return Number.isNaN(t) ? null : t +} + +/** Rows as objects keyed by column name. @param {{ columns?: any[], rows?: unknown[][] }} r */ +export function rowsOf(r) { + const cols = (r?.columns ?? []).map((c) => String(c?.name ?? c)) + return (r?.rows ?? []).map((row) => Object.fromEntries(cols.map((c, i) => [c, row[i]]))) +} + +/** + * @param {Record[]} rows `_prisma_migrations` + * @param {MigrationFile[] | null} files null when no project folder is set + * @returns {Migration[]} + */ +export function mergePrisma(rows, files) { + /** @type {Map[]>} */ + const byName = new Map() + for (const r of rows) { + const n = String(r.migration_name ?? '') + if (!byName.has(n)) byName.set(n, []) + byName.get(n)?.push(r) + } + const fileByName = new Map((files ?? []).map((f) => [f.name, f])) + const names = [...new Set([...byName.keys(), ...fileByName.keys()])].sort() + return names.map((name) => { + const attempts = (byName.get(name) ?? []).slice().sort((a, b) => (toMs(a.started_at) ?? 0) - (toMs(b.started_at) ?? 0)) + const last = attempts[attempts.length - 1] + const file = fileByName.get(name) ?? null + /** @type {MigrationStatus} */ + let status = 'pending' + if (last) status = last.rolled_back_at ? 'rolled-back' : last.finished_at ? 'applied' : 'failed' + const started = last ? toMs(last.started_at) : null + const finished = last ? toMs(last.finished_at) : null + return { + name, + status, + appliedAt: status === 'applied' ? finished : null, + startedAt: started, + durationMs: started != null && finished != null ? Math.max(0, finished - started) : null, + steps: last?.applied_steps_count != null ? Number(last.applied_steps_count) : null, + logs: last?.logs ? String(last.logs) : null, + sql: file?.sql ?? null, + changed: !!(file && last && status === 'applied' && last.checksum && file.checksum && last.checksum !== file.checksum), + missingFile: !!files && !file, + attempts: attempts.length, + } + }) +} + +/** + * @param {Record[]} rows `__drizzle_migrations` + * @param {MigrationFile[] | null} files in journal order; null when no folder is set + * @returns {Migration[]} + */ +export function mergeDrizzle(rows, files) { + const applied = rows + .map((r) => ({ hash: String(r.hash ?? ''), at: toMs(r.created_at) })) + .sort((a, b) => (a.at ?? 0) - (b.at ?? 0)) + const newest = applied.reduce((m, r) => Math.max(m, r.at ?? 0), 0) + const hashes = new Set(applied.map((r) => r.hash)) + if (!files) { + return applied.map((r, i) => ({ + name: `#${i + 1} · ${r.hash.slice(0, 12)}`, + status: /** @type {MigrationStatus} */ ('applied'), + appliedAt: r.at, + startedAt: null, + durationMs: null, + steps: null, + logs: null, + sql: null, + changed: false, + missingFile: false, + attempts: 1, + })) + } + const fileHashes = new Set(files.map((f) => f.checksum)) + /** @type {Migration[]} */ + const out = files.map((f) => { + const byHash = hashes.has(f.checksum) + const byTime = f.when != null && f.when <= newest + const ran = byHash || byTime + const row = applied.find((r) => r.hash === f.checksum) ?? (byTime ? applied.find((r) => r.at === f.when) : undefined) + return { + name: f.name, + status: ran ? 'applied' : 'pending', + appliedAt: ran ? row?.at ?? f.when : null, + startedAt: null, + durationMs: null, + steps: null, + logs: null, + sql: f.sql, + // Ran (by its timestamp) but no row has this file's hash: edited since. + changed: ran && !byHash && !!f.checksum, + missingFile: !f.checksum, + attempts: ran ? 1 : 0, + } + }) + // Rows whose file is no longer in the project. + applied.filter((r) => !fileHashes.has(r.hash) && !files.some((f) => f.when != null && f.when === r.at)).forEach((r) => { + out.push({ name: `Not in the project · ${r.hash.slice(0, 12)}`, status: 'applied', appliedAt: r.at, startedAt: null, durationMs: null, steps: null, logs: null, sql: null, changed: false, missingFile: true, attempts: 1 }) + }) + return out +} + +/** @param {Migration[]} list */ +export function counts(list) { + const c = { applied: 0, pending: 0, failed: 0, rolledBack: 0, changed: 0 } + for (const m of list) { + if (m.status === 'applied') c.applied++ + else if (m.status === 'pending') c.pending++ + else if (m.status === 'failed') c.failed++ + else c.rolledBack++ + if (m.changed) c.changed++ + } + return c +} + +/** + * The SELECTs that read each tool's table, per engine family. Prisma's table + * lives in the connection's schema; Drizzle's in `drizzle` on Postgres. + * @param {'prisma' | 'drizzle'} tool @param {string} family @param {string | null} schema + * @returns {string[]} tried in order until one works + */ +export function migrationQueries(tool, family, schema) { + const q = family === 'mysql' ? (/** @type {string} */ s) => `\`${s.replace(/`/g, '``')}\`` : (/** @type {string} */ s) => `"${s.replace(/"/g, '""')}"` + if (tool === 'prisma') { + const cols = 'id, checksum, finished_at, migration_name, logs, rolled_back_at, started_at, applied_steps_count' + const table = q('_prisma_migrations') + const out = [] + if (family === 'postgres' && schema) out.push(`SELECT ${cols} FROM ${q(schema)}.${table} ORDER BY started_at`) + out.push(`SELECT ${cols} FROM ${table} ORDER BY started_at`) + return out + } + const table = q('__drizzle_migrations') + const out = [] + if (family === 'postgres') out.push(`SELECT id, hash, created_at FROM ${q('drizzle')}.${table} ORDER BY created_at`) + out.push(`SELECT id, hash, created_at FROM ${table} ORDER BY created_at`) + return out +} + +/** Whether an error just means "this table isn't there". @param {unknown} e */ +export function isMissingTable(e) { + return /does not exist|doesn't exist|no such table|not found|unknown table|invalid object name/i.test(String(e)) +} diff --git a/src/lib/migrations.test.js b/src/lib/migrations.test.js new file mode 100644 index 00000000..a66e829d --- /dev/null +++ b/src/lib/migrations.test.js @@ -0,0 +1,76 @@ +import { describe, it, expect } from 'vitest' +import { mergePrisma, mergeDrizzle, counts, toMs, migrationQueries, isMissingTable, rowsOf } from './migrations.js' + +const file = (name, checksum, when = null) => ({ name, sql: `-- ${name}`, checksum, when }) + +describe('mergePrisma', () => { + const rows = [ + { migration_name: '20240101_init', checksum: 'aaa', started_at: '2024-01-01T00:00:00Z', finished_at: '2024-01-01T00:00:02Z', rolled_back_at: null, logs: null, applied_steps_count: 1 }, + { migration_name: '20240102_posts', checksum: 'bbb', started_at: '2024-01-02T00:00:00Z', finished_at: null, rolled_back_at: null, logs: 'relation "users" does not exist', applied_steps_count: 0 }, + { migration_name: '20240103_tags', checksum: 'ccc', started_at: '2024-01-03T00:00:00Z', finished_at: null, rolled_back_at: '2024-01-03T01:00:00Z', logs: null, applied_steps_count: 0 }, + ] + it('reads applied, failed and rolled back runs, and pending files', () => { + const list = mergePrisma(rows, [file('20240101_init', 'aaa'), file('20240102_posts', 'bbb'), file('20240103_tags', 'ccc'), file('20240104_likes', 'ddd')]) + expect(list.map((m) => [m.name, m.status])).toEqual([ + ['20240101_init', 'applied'], ['20240102_posts', 'failed'], ['20240103_tags', 'rolled-back'], ['20240104_likes', 'pending'], + ]) + expect(list[0].durationMs).toBe(2000) + expect(list[1].logs).toContain('does not exist') + expect(counts(list)).toEqual({ applied: 1, pending: 1, failed: 1, rolledBack: 1, changed: 0 }) + }) + it('flags a file edited after it ran, and rows with no file', () => { + const list = mergePrisma(rows.slice(0, 1), [file('20240101_init', 'zzz')]) + expect(list[0].changed).toBe(true) + expect(mergePrisma(rows.slice(0, 1), []).at(0)?.missingFile).toBe(true) + // No folder: nothing is "missing". + expect(mergePrisma(rows.slice(0, 1), null).at(0)?.missingFile).toBe(false) + }) + it('a retry that succeeded counts as applied', () => { + const retry = { ...rows[1], started_at: '2024-01-02T02:00:00Z', finished_at: '2024-01-02T02:00:01Z', logs: null } + const [m] = mergePrisma([rows[1], retry], null) + expect(m.status).toBe('applied') + expect(m.attempts).toBe(2) + }) +}) + +describe('mergeDrizzle', () => { + const rows = [{ id: 1, hash: 'h0', created_at: '1700000001000' }, { id: 2, hash: 'h1', created_at: 1700000002000 }] + it('applied up to the newest created_at, pending after', () => { + const list = mergeDrizzle(rows, [file('0000_init', 'h0', 1700000001000), file('0001_posts', 'h1', 1700000002000), file('0002_tags', 'h2', 1700000003000)]) + expect(list.map((m) => [m.name, m.status])).toEqual([['0000_init', 'applied'], ['0001_posts', 'applied'], ['0002_tags', 'pending']]) + expect(list[1].appliedAt).toBe(1700000002000) + }) + it('flags an edited file and rows the project no longer has', () => { + const list = mergeDrizzle(rows, [file('0000_init', 'edited', 1700000001000)]) + expect(list[0]).toMatchObject({ status: 'applied', changed: true }) + expect(list.at(-1)).toMatchObject({ missingFile: true, status: 'applied' }) + }) + it('lists the rows alone when no folder is set', () => { + const list = mergeDrizzle(rows, null) + expect(list).toHaveLength(2) + expect(list.every((m) => m.status === 'applied')).toBe(true) + }) +}) + +describe('helpers', () => { + it('reads timestamps the way each engine returns them', () => { + expect(toMs('2024-01-01 00:00:00')).toBe(Date.UTC(2024, 0, 1)) + expect(toMs(1700000000)).toBe(1700000000000) + expect(toMs('1700000000000')).toBe(1700000000000) + expect(toMs(null)).toBeNull() + }) + it('quotes per engine and tries the schema first', () => { + expect(migrationQueries('prisma', 'postgres', 'app')[0]).toContain('FROM "app"."_prisma_migrations"') + expect(migrationQueries('prisma', 'mysql', null)[0]).toContain('FROM `_prisma_migrations`') + expect(migrationQueries('drizzle', 'postgres', null)[0]).toContain('"drizzle"."__drizzle_migrations"') + expect(migrationQueries('drizzle', 'sqlite', null)).toHaveLength(1) + }) + it('knows a missing table from a real error', () => { + expect(isMissingTable('relation "_prisma_migrations" does not exist')).toBe(true) + expect(isMissingTable('no such table: __drizzle_migrations')).toBe(true) + expect(isMissingTable('permission denied')).toBe(false) + }) + it('turns a result into objects', () => { + expect(rowsOf({ columns: [{ name: 'a' }, { name: 'b' }], rows: [[1, 2]] })).toEqual([{ a: 1, b: 2 }]) + }) +}) diff --git a/src/lib/studio-tabs.js b/src/lib/studio-tabs.js index 312cb946..9ee37320 100644 --- a/src/lib/studio-tabs.js +++ b/src/lib/studio-tabs.js @@ -1,4 +1,4 @@ -/** @typedef {'table' | 'sql' | 'ddl' | 'welcome' | 'ai' | 'schema' | 'orm' | 'security' | 'logs' | 'extensions' | 'extension-detail' | 'backup' | 'json' | 'charts' | 'dashboard' | 'erd' | 'reltree' | 'diagrams' | 'search' | 'notebook' | 'schema-timeline' | 'data-diff' | 'insights' | 'objects' | 'redis' | 'dynamodb' | 'docs' | 'license' | 'orm-schema' | 'map' | 'advisor' | 'golf' | 'terminal' | 'shortcuts' | 'shares'} StudioTabKind */ +/** @typedef {'table' | 'sql' | 'ddl' | 'welcome' | 'ai' | 'schema' | 'orm' | 'security' | 'logs' | 'extensions' | 'extension-detail' | 'backup' | 'json' | 'charts' | 'dashboard' | 'erd' | 'reltree' | 'diagrams' | 'search' | 'notebook' | 'schema-timeline' | 'data-diff' | 'migrations' | 'insights' | 'objects' | 'redis' | 'dynamodb' | 'docs' | 'license' | 'orm-schema' | 'map' | 'advisor' | 'golf' | 'terminal' | 'shortcuts' | 'shares'} StudioTabKind */ import { loadDefaultPageSize } from '$lib/table-query.js' @@ -233,6 +233,7 @@ const SINGLETON_TAB_TITLES = { search: 'Find in database', 'schema-timeline': 'Schema timeline', 'data-diff': 'Data diff', + migrations: 'Migrations', golf: 'VACUUM', terminal: 'Terminal', shortcuts: 'Keyboard shortcuts', @@ -281,6 +282,7 @@ export const createLicenseTab = () => createKindTab('license') export const createSearchTab = () => createKindTab('search') export const createSchemaTimelineTab = () => createKindTab('schema-timeline') export const createDataDiffTab = () => createKindTab('data-diff') +export const createMigrationsTab = () => createKindTab('migrations') /** The connection's own CLI (psql, mysql, sqlite3...) in a real terminal. */ export const createTerminalTab = () => createKindTab('terminal') /** Every shortcut, searchable and remappable. */ @@ -345,6 +347,8 @@ export const findSearchTab = (tabs) => findTabByKind(tabs, 'search') export const findSchemaTimelineTab = (tabs) => findTabByKind(tabs, 'schema-timeline') /** @type {(tabs: StudioTab[]) => StudioTab | null} */ export const findDataDiffTab = (tabs) => findTabByKind(tabs, 'data-diff') +/** @param {StudioTab[]} tabs */ +export const findMigrationsTab = (tabs) => findTabByKind(tabs, 'migrations') /** @param {string} extensionId @param {string} name */ export function createExtensionDetailTab(extensionId, name) {