From 7f868711f0a7c77c868a2dd956fcc594d3d95ec8 Mon Sep 17 00:00:00 2001 From: Scotte Zinn Date: Mon, 23 Jun 2025 07:31:55 -0400 Subject: feat: Add sqlite server support for self-hosting (#2770) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Move db_uri setting to DbSettings * WIP: sqlite crate framework * WIP: Migrations * WIP: sqlite implementation * Add sqlite3 to Docker image * verified_at needed for user query * chore(deps): bump debian (#2772) Bumps debian from bookworm-20250428-slim to bookworm-20250520-slim. --- updated-dependencies: - dependency-name: debian dependency-version: bookworm-20250520-slim dependency-type: direct:production ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * fix(doctor): mention the required ble.sh version (#2774) References: https://forum.atuin.sh/t/1047 * fix: Don't print errors in `zsh_autosuggest` helper (#2780) Previously, this would result in long multi-line errors when typing, making it hard to see the shell prompt: ``` $ Error: could not load client settings Caused by: 0: could not create config file 1: failed to create file `/home/jyn/.config/atuin/config.toml` 2: Required key not available (os error 126) Location: atuin-client/src/settings.rs:675:54 fError: could not load client settings Caused by: 0: could not create config file 1: failed to create file `/home/jyn/.config/atuin/config.toml` 2: Required key not available (os error 126) Location: atuin-client/src/settings.rs:675:54 faError: could not load client settings ``` Silence these in autosuggestions, such that they only show up when explicitly invoking atuin. * fix: `atuin.nu` enchancements (#2778) * PR feedback * Remove sqlite3 package * fix(search): prevent panic on malformed format strings (#2776) (#2777) * fix(search): prevent panic on malformed format strings (#2776) - Wrap format operations in panic catcher for graceful error handling - Improve error messages with context-aware guidance for common issues - Let runtime-format parser handle validation to avoid blocking valid formats Fixes crash when using malformed format strings by catching formatting errors gracefully and providing actionable guidance without restricting legitimate format patterns like {command} or {time}. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude * Satisfy cargo fmt * test(search): add regression tests for format string panic (#2776) - Add test for malformed JSON format strings that previously caused panics - Add test to ensure valid format strings continue to work - Prevent future regressions of the format string panic issue 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --------- Co-authored-by: Claude --------- Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Koichi Murase Co-authored-by: jyn Co-authored-by: Tyarel8 <98483313+Tyarel8@users.noreply.github.com> Co-authored-by: Brian Cosgrove Co-authored-by: Claude --- crates/atuin-server-database/Cargo.toml | 1 + crates/atuin-server-database/src/lib.rs | 50 +++++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 3 deletions(-) (limited to 'crates/atuin-server-database') diff --git a/crates/atuin-server-database/Cargo.toml b/crates/atuin-server-database/Cargo.toml index e3e38e3f..823b5d39 100644 --- a/crates/atuin-server-database/Cargo.toml +++ b/crates/atuin-server-database/Cargo.toml @@ -17,3 +17,4 @@ time = { workspace = true } eyre = { workspace = true } serde = { workspace = true } async-trait = { workspace = true } +url = "2.5.2" diff --git a/crates/atuin-server-database/src/lib.rs b/crates/atuin-server-database/src/lib.rs index 1c577f59..9df36d14 100644 --- a/crates/atuin-server-database/src/lib.rs +++ b/crates/atuin-server-database/src/lib.rs @@ -15,7 +15,7 @@ use self::{ }; use async_trait::async_trait; use atuin_common::record::{EncryptedData, HostId, Record, RecordIdx, RecordStatus}; -use serde::{Serialize, de::DeserializeOwned}; +use serde::{Deserialize, Serialize}; use time::{Date, Duration, Month, OffsetDateTime, Time, UtcOffset}; use tracing::instrument; @@ -41,10 +41,54 @@ impl std::error::Error for DbError {} pub type DbResult = Result; +#[derive(Debug, PartialEq)] +pub enum DbType { + Postgres, + Sqlite, + Unknown, +} + +#[derive(Clone, Deserialize, Serialize)] +pub struct DbSettings { + pub db_uri: String, +} + +impl DbSettings { + pub fn db_type(&self) -> DbType { + if self.db_uri.starts_with("postgres://") { + DbType::Postgres + } else if self.db_uri.starts_with("sqlite://") { + DbType::Sqlite + } else { + DbType::Unknown + } + } +} + +// Do our best to redact passwords so they're not logged in the event of an error. +impl Debug for DbSettings { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + if self.db_type() == DbType::Postgres { + let redacted_uri = url::Url::parse(&self.db_uri) + .map(|mut url| { + let _ = url.set_password(Some("****")); + url.to_string() + }) + .unwrap_or(self.db_uri.clone()); + f.debug_struct("DbSettings") + .field("db_uri", &redacted_uri) + .finish() + } else { + f.debug_struct("DbSettings") + .field("db_uri", &self.db_uri) + .finish() + } + } +} + #[async_trait] pub trait Database: Sized + Clone + Send + Sync + 'static { - type Settings: Debug + Clone + DeserializeOwned + Serialize + Send + Sync + 'static; - async fn new(settings: &Self::Settings) -> DbResult; + async fn new(settings: &DbSettings) -> DbResult; async fn get_session(&self, token: &str) -> DbResult; async fn get_session_user(&self, token: &str) -> DbResult; -- cgit v1.3.1