aboutsummaryrefslogtreecommitdiffstats
path: root/crates/atuin-server-sqlite
diff options
context:
space:
mode:
authorJohn Oxley <john.oxley@gmail.com>2026-05-14 22:53:32 +0100
committerGitHub <noreply@github.com>2026-05-14 14:53:32 -0700
commitd98ecd58af9f6d850461b8bef430dfef70111692 (patch)
tree7b83a7718fa3b3a742134a9700b29330deafc400 /crates/atuin-server-sqlite
parentfix(ci): fossier install in scan workflow (#3485) (diff)
downloadatuin-d98ecd58af9f6d850461b8bef430dfef70111692.zip
refactor: Implement From<sqlx::Error> and clean up fix_error (#3484)
In the database crates for atuin-server, there is `fn fix_error`. This PR implements `From<sqlx::Error>` on `DbError` which makes it possible to mostly use `?` to bubble up the errors. There are cases where `?` is not being used e.g. ```rust async fn get_session(&self, token: &str) -> DbResult<Session> { sqlx::query_as("select id, user_id, token from sessions where token = $1") .bind(token) .fetch_one(&self.pool) .await .map_err(fix_error) .map(|DbSession(session)| session) } ``` There are two options ## 1. Use `Into::into` ```rust async fn get_session(&self, token: &str) -> DbResult<Session> { sqlx::query_as("select id, user_id, token from sessions where token = $1") .bind(token) .fetch_one(&self.pool) .await .map_err(fix_error) .map(|DbSession(session)| session) } ``` ## 2. Create a variable and use `?` ```rust async fn get_session(&self, token: &str) -> DbResult<Session> { let session = sqlx::query_as("select id, user_id, token from sessions where token = $1") .bind(token) .fetch_one(&self.pool) .await .map(|DbSession(session)| session)?; Ok(session) } ``` I chose to do option 1 as it was just a find/replace but say the word and I'll convert them all to option 2 ## Checks - [X] I am happy for maintainers to push small adjustments to this PR, to speed up the review cycle - [X] I have checked that there are no existing pull requests for the same thing
Diffstat (limited to 'crates/atuin-server-sqlite')
-rw-r--r--crates/atuin-server-sqlite/src/lib.rs80
1 files changed, 27 insertions, 53 deletions
diff --git a/crates/atuin-server-sqlite/src/lib.rs b/crates/atuin-server-sqlite/src/lib.rs
index d69258c4..7d4dcb86 100644
--- a/crates/atuin-server-sqlite/src/lib.rs
+++ b/crates/atuin-server-sqlite/src/lib.rs
@@ -23,25 +23,14 @@ pub struct Sqlite {
pool: sqlx::Pool<sqlx::sqlite::Sqlite>,
}
-fn fix_error(error: sqlx::Error) -> DbError {
- match error {
- sqlx::Error::RowNotFound => DbError::NotFound,
- error => DbError::Other(error.into()),
- }
-}
-
#[async_trait]
impl Database for Sqlite {
async fn new(settings: &DbSettings) -> DbResult<Self> {
- let opts = SqliteConnectOptions::from_str(&settings.db_uri)
- .map_err(fix_error)?
+ let opts = SqliteConnectOptions::from_str(&settings.db_uri)?
.journal_mode(SqliteJournalMode::Wal)
.create_if_missing(true);
- let pool = SqlitePoolOptions::new()
- .connect_with(opts)
- .await
- .map_err(fix_error)?;
+ let pool = SqlitePoolOptions::new().connect_with(opts).await?;
sqlx::migrate!("./migrations")
.run(&pool)
@@ -57,7 +46,7 @@ impl Database for Sqlite {
.bind(token)
.fetch_one(&self.pool)
.await
- .map_err(fix_error)
+ .map_err(Into::into)
.map(|DbSession(session)| session)
}
@@ -72,7 +61,7 @@ impl Database for Sqlite {
.bind(token)
.fetch_one(&self.pool)
.await
- .map_err(fix_error)
+ .map_err(Into::into)
.map(|DbUser(user)| user)
}
@@ -88,8 +77,7 @@ impl Database for Sqlite {
.bind(session.user_id)
.bind(token)
.execute(&self.pool)
- .await
- .map_err(fix_error)?;
+ .await?;
Ok(())
}
@@ -100,7 +88,7 @@ impl Database for Sqlite {
.bind(username)
.fetch_one(&self.pool)
.await
- .map_err(fix_error)
+ .map_err(Into::into)
.map(|DbUser(user)| user)
}
@@ -110,7 +98,7 @@ impl Database for Sqlite {
.bind(u.id)
.fetch_one(&self.pool)
.await
- .map_err(fix_error)
+ .map_err(Into::into)
.map(|DbSession(session)| session)
}
@@ -130,8 +118,7 @@ impl Database for Sqlite {
.bind(email)
.bind(password)
.fetch_one(&self.pool)
- .await
- .map_err(fix_error)?;
+ .await?;
Ok(res.0)
}
@@ -146,8 +133,7 @@ impl Database for Sqlite {
.bind(&user.password)
.bind(user.id)
.execute(&self.pool)
- .await
- .map_err(fix_error)?;
+ .await?;
Ok(())
}
@@ -164,8 +150,7 @@ impl Database for Sqlite {
)
.bind(user.id)
.fetch_one(&self.pool)
- .await
- .map_err(fix_error)?;
+ .await?;
Ok(res.0)
}
@@ -180,20 +165,17 @@ impl Database for Sqlite {
sqlx::query("delete from sessions where user_id = $1")
.bind(u.id)
.execute(&self.pool)
- .await
- .map_err(fix_error)?;
+ .await?;
sqlx::query("delete from users where id = $1")
.bind(u.id)
.execute(&self.pool)
- .await
- .map_err(fix_error)?;
+ .await?;
sqlx::query("delete from history where user_id = $1")
.bind(u.id)
.execute(&self.pool)
- .await
- .map_err(fix_error)?;
+ .await?;
Ok(())
}
@@ -210,8 +192,7 @@ impl Database for Sqlite {
.bind(id)
.bind(time::OffsetDateTime::now_utc())
.fetch_all(&self.pool)
- .await
- .map_err(fix_error)?;
+ .await?;
Ok(())
}
@@ -229,8 +210,7 @@ impl Database for Sqlite {
)
.bind(user.id)
.fetch_all(&self.pool)
- .await
- .map_err(fix_error)?;
+ .await?;
let res = res.iter().map(|row| row.get("client_id")).collect();
@@ -244,15 +224,14 @@ impl Database for Sqlite {
)
.bind(user.id)
.execute(&self.pool)
- .await
- .map_err(fix_error)?;
+ .await?;
Ok(())
}
#[instrument(skip_all)]
async fn add_records(&self, user: &User, records: &[Record<EncryptedData>]) -> DbResult<()> {
- let mut tx = self.pool.begin().await.map_err(fix_error)?;
+ let mut tx = self.pool.begin().await?;
for i in records {
let id = atuin_common::utils::uuid_v7();
@@ -275,11 +254,10 @@ impl Database for Sqlite {
.bind(&i.data.content_encryption_key)
.bind(user.id)
.execute(&mut *tx)
- .await
- .map_err(fix_error)?;
+ .await?;
}
- tx.commit().await.map_err(fix_error)?;
+ tx.commit().await?;
Ok(())
}
@@ -312,7 +290,7 @@ impl Database for Sqlite {
.bind(count as i64)
.fetch_all(&self.pool)
.await
- .map_err(fix_error);
+ .map_err(Into::into);
let ret = match records {
Ok(records) => {
@@ -343,8 +321,7 @@ impl Database for Sqlite {
let res: Vec<(Uuid, String, i64)> = sqlx::query_as(STATUS_SQL)
.bind(user.id)
.fetch_all(&self.pool)
- .await
- .map_err(fix_error)?;
+ .await?;
let mut status = RecordStatus::new();
@@ -371,8 +348,7 @@ impl Database for Sqlite {
.bind(into_utc(range.start))
.bind(into_utc(range.end))
.fetch_one(&self.pool)
- .await
- .map_err(fix_error)?;
+ .await?;
Ok(res.0)
}
@@ -403,15 +379,14 @@ impl Database for Sqlite {
.fetch(&self.pool)
.map_ok(|DbHistory(h)| h)
.try_collect()
- .await
- .map_err(fix_error)?;
+ .await?;
Ok(res)
}
#[instrument(skip_all)]
async fn add_history(&self, history: &[NewHistory]) -> DbResult<()> {
- let mut tx = self.pool.begin().await.map_err(fix_error)?;
+ let mut tx = self.pool.begin().await?;
for i in history {
let client_id: &str = &i.client_id;
@@ -431,11 +406,10 @@ impl Database for Sqlite {
.bind(i.timestamp)
.bind(data)
.execute(&mut *tx)
- .await
- .map_err(fix_error)?;
+ .await?;
}
- tx.commit().await.map_err(fix_error)?;
+ tx.commit().await?;
Ok(())
}
@@ -451,7 +425,7 @@ impl Database for Sqlite {
.bind(user.id)
.fetch_one(&self.pool)
.await
- .map_err(fix_error)
+ .map_err(Into::into)
.map(|DbHistory(h)| h)
}
}