diff options
| author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2026-06-13 15:41:04 +0200 |
|---|---|---|
| committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2026-06-13 15:41:04 +0200 |
| commit | 8e76a4aa661918de348c4bf5e6b1bcba0726cc82 (patch) | |
| tree | 9a263d65828376adb801b09696c704caf29f5694 /crates/turtle/src/atuin_client/database.rs | |
| parent | chore(treewide): Also fix all `clippy` warnings (diff) | |
| download | atuin-8e76a4aa661918de348c4bf5e6b1bcba0726cc82.zip | |
fix(sqlite): Ensure that database migration runs sequentially
Otherwise, we might run migration from multiple db-connections.
Diffstat (limited to 'crates/turtle/src/atuin_client/database.rs')
| -rw-r--r-- | crates/turtle/src/atuin_client/database.rs | 43 |
1 files changed, 15 insertions, 28 deletions
diff --git a/crates/turtle/src/atuin_client/database.rs b/crates/turtle/src/atuin_client/database.rs index c730b1d4..c3130c4c 100644 --- a/crates/turtle/src/atuin_client/database.rs +++ b/crates/turtle/src/atuin_client/database.rs @@ -2,19 +2,15 @@ use std::{ env, path::{Path, PathBuf}, str::FromStr, - time::Duration, }; -use crate::atuin_common::utils; -use fs_err as fs; +use crate::{atuin_client::utils::setup_db, atuin_common::utils}; +use fs_err::{self as fs}; use itertools::Itertools; use sql_builder::{SqlBuilder, SqlName, bind::Bind, esc, quote}; use sqlx::{ Result, Row, - sqlite::{ - SqliteConnectOptions, SqliteJournalMode, SqlitePool, SqlitePoolOptions, SqliteRow, - SqliteSynchronous, - }, + sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePool, SqliteRow, SqliteSynchronous}, }; use time::OffsetDateTime; use tracing::debug; @@ -103,6 +99,17 @@ pub(crate) struct ClientSqlite { impl ClientSqlite { pub(crate) async fn new(path: impl AsRef<Path>, timeout: f64) -> Result<Self> { + fn mk_opts(path: &str) -> Result<SqliteConnectOptions> { + let opts = SqliteConnectOptions::from_str(path)? + .journal_mode(SqliteJournalMode::Wal) + .optimize_on_close(true, None) + .synchronous(SqliteSynchronous::Normal) + .with_regexp() + .create_if_missing(true); + + Ok(opts) + } + let path = path.as_ref(); debug!("opening sqlite database at {path:?}"); @@ -120,30 +127,10 @@ impl ClientSqlite { fs::create_dir_all(dir)?; } - let opts = SqliteConnectOptions::from_str(path.as_os_str().to_str().unwrap())? - .journal_mode(SqliteJournalMode::Wal) - .optimize_on_close(true, None) - .synchronous(SqliteSynchronous::Normal) - .with_regexp() - .create_if_missing(true); - - let pool = SqlitePoolOptions::new() - .acquire_timeout(Duration::from_secs_f64(timeout)) - .connect_with(opts) - .await?; - - Self::setup_db(&pool).await?; + let pool = setup_db!(path, timeout, mk_opts, "./db/client-migrations").await?; Ok(Self { pool }) } - async fn setup_db(pool: &SqlitePool) -> Result<()> { - debug!("running sqlite database setup"); - - sqlx::migrate!("./db/client-migrations").run(pool).await?; - - Ok(()) - } - async fn save_raw(tx: &mut sqlx::Transaction<'_, sqlx::Sqlite>, h: &History) -> Result<()> { sqlx::query( "insert or ignore into history(id, timestamp, duration, exit, command, cwd, session, hostname, author, intent, deleted_at) |
