diff --git a/atuin-client/migrations/20210422143411_create_history.sql b/atuin-client/migrations/20210422143411_create_history.sql index 23c63a4..1f3f868 100644 --- a/atuin-client/migrations/20210422143411_create_history.sql +++ b/atuin-client/migrations/20210422143411_create_history.sql @@ -1,7 +1,7 @@ -- Add migration script here create table if not exists history ( id text primary key, - timestamp text not null, + timestamp integer not null, duration integer not null, exit integer not null, command text not null, diff --git a/atuin-client/src/database.rs b/atuin-client/src/database.rs index 754a0ec..057b399 100644 --- a/atuin-client/src/database.rs +++ b/atuin-client/src/database.rs @@ -2,11 +2,15 @@ use std::path::Path; use std::str::FromStr; use async_trait::async_trait; +use chrono::prelude::*; use chrono::Utc; use eyre::Result; -use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePool, SqlitePoolOptions}; +use sqlx::sqlite::{ + SqliteConnectOptions, SqliteJournalMode, SqlitePool, SqlitePoolOptions, SqliteRow, +}; +use sqlx::Row; use super::history::History; @@ -78,7 +82,7 @@ impl Sqlite { values(?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)", ) .bind(h.id.as_str()) - .bind(h.timestamp.to_rfc3339()) + .bind(h.timestamp.timestamp_nanos()) .bind(h.duration) .bind(h.exit) .bind(h.command.as_str()) @@ -90,6 +94,19 @@ impl Sqlite { Ok(()) } + + fn query_history(row: SqliteRow) -> History { + History { + id: row.get("id"), + timestamp: Utc.timestamp_nanos(row.get("timestamp")), + duration: row.get("duration"), + exit: row.get("exit"), + command: row.get("command"), + cwd: row.get("cwd"), + session: row.get("session"), + hostname: row.get("hostname"), + } + } } #[async_trait] @@ -121,8 +138,9 @@ impl Database for Sqlite { async fn load(&self, id: &str) -> Result { debug!("loading history item {}", id); - let res = sqlx::query_as::<_, History>("select * from history where id = ?1") + let res = sqlx::query("select * from history where id = ?1") .bind(id) + .map(Self::query_history) .fetch_one(&self.pool) .await?; @@ -138,7 +156,7 @@ impl Database for Sqlite { where id = ?1", ) .bind(h.id.as_str()) - .bind(h.timestamp.to_rfc3339()) + .bind(h.timestamp.timestamp_nanos()) .bind(h.duration) .bind(h.exit) .bind(h.command.as_str()) @@ -181,7 +199,8 @@ impl Database for Sqlite { } ); - let res = sqlx::query_as::<_, History>(query.as_str()) + let res = sqlx::query(query.as_str()) + .map(Self::query_history) .fetch_all(&self.pool) .await?; @@ -195,11 +214,12 @@ impl Database for Sqlite { ) -> Result> { debug!("listing history from {:?} to {:?}", from, to); - let res = sqlx::query_as::<_, History>( + let res = sqlx::query( "select * from history where timestamp >= ?1 and timestamp <= ?2 order by timestamp asc", ) .bind(from) .bind(to) + .map(Self::query_history) .fetch_all(&self.pool) .await?; @@ -207,19 +227,20 @@ impl Database for Sqlite { } async fn first(&self) -> Result { - let res = sqlx::query_as::<_, History>( - "select * from history where duration >= 0 order by timestamp asc limit 1", - ) - .fetch_one(&self.pool) - .await?; + let res = + sqlx::query("select * from history where duration >= 0 order by timestamp asc limit 1") + .map(Self::query_history) + .fetch_one(&self.pool) + .await?; Ok(res) } async fn last(&self) -> Result { - let res = sqlx::query_as::<_, History>( + let res = sqlx::query( "select * from history where duration >= 0 order by timestamp desc limit 1", ) + .map(Self::query_history) .fetch_one(&self.pool) .await?; @@ -227,11 +248,12 @@ impl Database for Sqlite { } async fn before(&self, timestamp: chrono::DateTime, count: i64) -> Result> { - let res = sqlx::query_as::<_, History>( + let res = sqlx::query( "select * from history where timestamp < ?1 order by timestamp desc limit ?2", ) - .bind(timestamp) + .bind(timestamp.timestamp_nanos()) .bind(count) + .map(Self::query_history) .fetch_all(&self.pool) .await?; @@ -250,7 +272,7 @@ impl Database for Sqlite { let query = query.to_string().replace("*", "%"); // allow wildcard char let limit = limit.map_or("".to_owned(), |l| format!("limit {}", l)); - let res = sqlx::query_as::<_, History>( + let res = sqlx::query( format!( "select * from history where command like ?1 || '%' @@ -260,6 +282,7 @@ impl Database for Sqlite { .as_str(), ) .bind(query) + .map(Self::query_history) .fetch_all(&self.pool) .await?; @@ -267,7 +290,8 @@ impl Database for Sqlite { } async fn query_history(&self, query: &str) -> Result> { - let res = sqlx::query_as::<_, History>(query) + let res = sqlx::query(query) + .map(Self::query_history) .fetch_all(&self.pool) .await?; diff --git a/atuin-client/src/sync.rs b/atuin-client/src/sync.rs index 9440801..813c2ed 100644 --- a/atuin-client/src/sync.rs +++ b/atuin-client/src/sync.rs @@ -122,7 +122,6 @@ async fn sync_upload( // anything left over outside of the 100 block size client.post_history(&buffer).await?; cursor = buffer.last().unwrap().timestamp; - remote_count = client.count().await?; }