summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEllie Huxtable <e@elm.sh>2021-04-25 21:27:51 +0100
committerEllie Huxtable <e@elm.sh>2021-04-25 21:27:51 +0100
commit4f16e8411e24891b140690798c47747f5c9bb91e (patch)
tree56db5166f3eb81df78f3654162e01d73eed6eba0
parent156893d774b4da5b541fdbb08428f9ec392949a0 (diff)
Revert to storing history as nanos
-rw-r--r--atuin-client/migrations/20210422143411_create_history.sql2
-rw-r--r--atuin-client/src/database.rs56
-rw-r--r--atuin-client/src/sync.rs1
3 files changed, 41 insertions, 18 deletions
diff --git a/atuin-client/migrations/20210422143411_create_history.sql b/atuin-client/migrations/20210422143411_create_history.sql
index 23c63a4f..1f3f8686 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 754a0ecf..057b399c 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<History> {
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<Vec<History>> {
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<History> {
- 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<History> {
- 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<Utc>, count: i64) -> Result<Vec<History>> {
- 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<Vec<History>> {
- 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 94408018..813c2ed4 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?;
}