mirror of
https://github.com/atuinsh/atuin.git
synced 2024-11-22 08:13:57 +01:00
refactor: String -> HistoryId (#1512)
This commit is contained in:
parent
179c6d20ef
commit
4e56f5a41e
@ -259,7 +259,9 @@ impl<'a> Client<'a> {
|
|||||||
|
|
||||||
self.client
|
self.client
|
||||||
.delete(url)
|
.delete(url)
|
||||||
.json(&DeleteHistoryRequest { client_id: h.id })
|
.json(&DeleteHistoryRequest {
|
||||||
|
client_id: h.id.to_string(),
|
||||||
|
})
|
||||||
.send()
|
.send()
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
@ -153,7 +153,7 @@ impl Sqlite {
|
|||||||
"insert or ignore into history(id, timestamp, duration, exit, command, cwd, session, hostname, deleted_at)
|
"insert or ignore into history(id, timestamp, duration, exit, command, cwd, session, hostname, deleted_at)
|
||||||
values(?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)",
|
values(?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)",
|
||||||
)
|
)
|
||||||
.bind(h.id.as_str())
|
.bind(h.id.0.as_str())
|
||||||
.bind(h.timestamp.unix_timestamp_nanos() as i64)
|
.bind(h.timestamp.unix_timestamp_nanos() as i64)
|
||||||
.bind(h.duration)
|
.bind(h.duration)
|
||||||
.bind(h.exit)
|
.bind(h.exit)
|
||||||
@ -236,7 +236,7 @@ impl Database for Sqlite {
|
|||||||
set timestamp = ?2, duration = ?3, exit = ?4, command = ?5, cwd = ?6, session = ?7, hostname = ?8, deleted_at = ?9
|
set timestamp = ?2, duration = ?3, exit = ?4, command = ?5, cwd = ?6, session = ?7, hostname = ?8, deleted_at = ?9
|
||||||
where id = ?1",
|
where id = ?1",
|
||||||
)
|
)
|
||||||
.bind(h.id.as_str())
|
.bind(h.id.0.as_str())
|
||||||
.bind(h.timestamp.unix_timestamp_nanos() as i64)
|
.bind(h.timestamp.unix_timestamp_nanos() as i64)
|
||||||
.bind(h.duration)
|
.bind(h.duration)
|
||||||
.bind(h.exit)
|
.bind(h.exit)
|
||||||
|
@ -166,7 +166,7 @@ fn encode(h: &History) -> Result<Vec<u8>> {
|
|||||||
// INFO: ensure this is updated when adding new fields
|
// INFO: ensure this is updated when adding new fields
|
||||||
encode::write_array_len(&mut output, 9)?;
|
encode::write_array_len(&mut output, 9)?;
|
||||||
|
|
||||||
encode::write_str(&mut output, &h.id)?;
|
encode::write_str(&mut output, &h.id.0)?;
|
||||||
encode::write_str(&mut output, &(format_rfc3339(h.timestamp)?))?;
|
encode::write_str(&mut output, &(format_rfc3339(h.timestamp)?))?;
|
||||||
encode::write_sint(&mut output, h.duration)?;
|
encode::write_sint(&mut output, h.duration)?;
|
||||||
encode::write_sint(&mut output, h.exit)?;
|
encode::write_sint(&mut output, h.exit)?;
|
||||||
@ -234,7 +234,7 @@ fn decode(bytes: &[u8]) -> Result<History> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Ok(History {
|
Ok(History {
|
||||||
id: id.to_owned(),
|
id: id.to_owned().into(),
|
||||||
timestamp: OffsetDateTime::parse(timestamp, &Rfc3339)?,
|
timestamp: OffsetDateTime::parse(timestamp, &Rfc3339)?,
|
||||||
duration,
|
duration,
|
||||||
exit,
|
exit,
|
||||||
@ -312,7 +312,7 @@ mod test {
|
|||||||
108, 117, 100, 103, 97, 116, 101, 192,
|
108, 117, 100, 103, 97, 116, 101, 192,
|
||||||
];
|
];
|
||||||
let history = History {
|
let history = History {
|
||||||
id: "66d16cbee7cd47538e5c5b8b44e9006e".to_owned(),
|
id: "66d16cbee7cd47538e5c5b8b44e9006e".to_owned().into(),
|
||||||
timestamp: datetime!(2023-05-28 18:35:40.633872 +00:00),
|
timestamp: datetime!(2023-05-28 18:35:40.633872 +00:00),
|
||||||
duration: 49206000,
|
duration: 49206000,
|
||||||
exit: 0,
|
exit: 0,
|
||||||
@ -333,7 +333,7 @@ mod test {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_decode_deleted() {
|
fn test_decode_deleted() {
|
||||||
let history = History {
|
let history = History {
|
||||||
id: "66d16cbee7cd47538e5c5b8b44e9006e".to_owned(),
|
id: "66d16cbee7cd47538e5c5b8b44e9006e".to_owned().into(),
|
||||||
timestamp: datetime!(2023-05-28 18:35:40.633872 +00:00),
|
timestamp: datetime!(2023-05-28 18:35:40.633872 +00:00),
|
||||||
duration: 49206000,
|
duration: 49206000,
|
||||||
exit: 0,
|
exit: 0,
|
||||||
@ -364,7 +364,7 @@ mod test {
|
|||||||
108, 117, 100, 103, 97, 116, 101,
|
108, 117, 100, 103, 97, 116, 101,
|
||||||
];
|
];
|
||||||
let history = History {
|
let history = History {
|
||||||
id: "66d16cbee7cd47538e5c5b8b44e9006e".to_owned(),
|
id: "66d16cbee7cd47538e5c5b8b44e9006e".to_owned().into(),
|
||||||
timestamp: datetime!(2023-05-28 18:35:40.633872 +00:00),
|
timestamp: datetime!(2023-05-28 18:35:40.633872 +00:00),
|
||||||
duration: 49206000,
|
duration: 49206000,
|
||||||
exit: 0,
|
exit: 0,
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
|
use core::fmt::Formatter;
|
||||||
use rmp::decode::ValueReadError;
|
use rmp::decode::ValueReadError;
|
||||||
use rmp::{decode::Bytes, Marker};
|
use rmp::{decode::Bytes, Marker};
|
||||||
use std::env;
|
use std::env;
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
use atuin_common::record::DecryptedData;
|
use atuin_common::record::DecryptedData;
|
||||||
use atuin_common::utils::uuid_v7;
|
use atuin_common::utils::uuid_v7;
|
||||||
@ -17,6 +19,21 @@ pub mod store;
|
|||||||
const HISTORY_VERSION: &str = "v0";
|
const HISTORY_VERSION: &str = "v0";
|
||||||
const HISTORY_TAG: &str = "history";
|
const HISTORY_TAG: &str = "history";
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
|
||||||
|
pub struct HistoryId(pub String);
|
||||||
|
|
||||||
|
impl Display for HistoryId {
|
||||||
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
|
write!(f, "{}", self.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<String> for HistoryId {
|
||||||
|
fn from(s: String) -> Self {
|
||||||
|
Self(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Client-side history entry.
|
/// Client-side history entry.
|
||||||
///
|
///
|
||||||
/// Client stores data unencrypted, and only encrypts it before sending to the server.
|
/// Client stores data unencrypted, and only encrypts it before sending to the server.
|
||||||
@ -35,7 +52,7 @@ pub struct History {
|
|||||||
/// A client-generated ID, used to identify the entry when syncing.
|
/// A client-generated ID, used to identify the entry when syncing.
|
||||||
///
|
///
|
||||||
/// Stored as `client_id` in the database.
|
/// Stored as `client_id` in the database.
|
||||||
pub id: String,
|
pub id: HistoryId,
|
||||||
/// When the command was run.
|
/// When the command was run.
|
||||||
pub timestamp: OffsetDateTime,
|
pub timestamp: OffsetDateTime,
|
||||||
/// How long the command took to run.
|
/// How long the command took to run.
|
||||||
@ -78,7 +95,7 @@ impl History {
|
|||||||
});
|
});
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
id: uuid_v7().as_simple().to_string(),
|
id: uuid_v7().as_simple().to_string().into(),
|
||||||
timestamp,
|
timestamp,
|
||||||
command,
|
command,
|
||||||
cwd,
|
cwd,
|
||||||
@ -103,7 +120,7 @@ impl History {
|
|||||||
// INFO: ensure this is updated when adding new fields
|
// INFO: ensure this is updated when adding new fields
|
||||||
encode::write_array_len(&mut output, 9)?;
|
encode::write_array_len(&mut output, 9)?;
|
||||||
|
|
||||||
encode::write_str(&mut output, &self.id)?;
|
encode::write_str(&mut output, &self.id.0)?;
|
||||||
encode::write_u64(&mut output, self.timestamp.unix_timestamp_nanos() as u64)?;
|
encode::write_u64(&mut output, self.timestamp.unix_timestamp_nanos() as u64)?;
|
||||||
encode::write_sint(&mut output, self.duration)?;
|
encode::write_sint(&mut output, self.duration)?;
|
||||||
encode::write_sint(&mut output, self.exit)?;
|
encode::write_sint(&mut output, self.exit)?;
|
||||||
@ -170,7 +187,7 @@ impl History {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Ok(History {
|
Ok(History {
|
||||||
id: id.to_owned(),
|
id: id.to_owned().into(),
|
||||||
timestamp: OffsetDateTime::from_unix_timestamp_nanos(timestamp as i128)?,
|
timestamp: OffsetDateTime::from_unix_timestamp_nanos(timestamp as i128)?,
|
||||||
duration,
|
duration,
|
||||||
exit,
|
exit,
|
||||||
@ -402,7 +419,7 @@ mod tests {
|
|||||||
];
|
];
|
||||||
|
|
||||||
let history = History {
|
let history = History {
|
||||||
id: "66d16cbee7cd47538e5c5b8b44e9006e".to_owned(),
|
id: "66d16cbee7cd47538e5c5b8b44e9006e".to_owned().into(),
|
||||||
timestamp: datetime!(2023-05-28 18:35:40.633872 +00:00),
|
timestamp: datetime!(2023-05-28 18:35:40.633872 +00:00),
|
||||||
duration: 49206000,
|
duration: 49206000,
|
||||||
exit: 0,
|
exit: 0,
|
||||||
@ -429,7 +446,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_serialize_deserialize_deleted() {
|
fn test_serialize_deserialize_deleted() {
|
||||||
let history = History {
|
let history = History {
|
||||||
id: "66d16cbee7cd47538e5c5b8b44e9006e".to_owned(),
|
id: "66d16cbee7cd47538e5c5b8b44e9006e".to_owned().into(),
|
||||||
timestamp: datetime!(2023-05-28 18:35:40.633872 +00:00),
|
timestamp: datetime!(2023-05-28 18:35:40.633872 +00:00),
|
||||||
duration: 49206000,
|
duration: 49206000,
|
||||||
exit: 0,
|
exit: 0,
|
||||||
|
@ -85,7 +85,7 @@ pub struct HistoryFromDb {
|
|||||||
impl From<HistoryFromDb> for History {
|
impl From<HistoryFromDb> for History {
|
||||||
fn from(from_db: HistoryFromDb) -> Self {
|
fn from(from_db: HistoryFromDb) -> Self {
|
||||||
History {
|
History {
|
||||||
id: from_db.id,
|
id: from_db.id.into(),
|
||||||
timestamp: from_db.timestamp,
|
timestamp: from_db.timestamp,
|
||||||
exit: from_db.exit,
|
exit: from_db.exit,
|
||||||
command: from_db.command,
|
command: from_db.command,
|
||||||
|
@ -4,7 +4,7 @@ use rmp::decode::Bytes;
|
|||||||
use crate::record::{encryption::PASETO_V4, sqlite_store::SqliteStore, store::Store};
|
use crate::record::{encryption::PASETO_V4, sqlite_store::SqliteStore, store::Store};
|
||||||
use atuin_common::record::{DecryptedData, Host, HostId, Record, RecordIdx};
|
use atuin_common::record::{DecryptedData, Host, HostId, Record, RecordIdx};
|
||||||
|
|
||||||
use super::{History, HISTORY_TAG, HISTORY_VERSION};
|
use super::{History, HistoryId, HISTORY_TAG, HISTORY_VERSION};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct HistoryStore {
|
pub struct HistoryStore {
|
||||||
@ -15,8 +15,8 @@ pub struct HistoryStore {
|
|||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq, Clone)]
|
#[derive(Debug, Eq, PartialEq, Clone)]
|
||||||
pub enum HistoryRecord {
|
pub enum HistoryRecord {
|
||||||
Create(History), // Create a history record
|
Create(History), // Create a history record
|
||||||
Delete(String), // Delete a history record, identified by ID
|
Delete(HistoryId), // Delete a history record, identified by ID
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HistoryRecord {
|
impl HistoryRecord {
|
||||||
@ -51,7 +51,7 @@ impl HistoryRecord {
|
|||||||
HistoryRecord::Delete(id) => {
|
HistoryRecord::Delete(id) => {
|
||||||
// 1 -> a history delete
|
// 1 -> a history delete
|
||||||
encode::write_u8(&mut output, 1)?;
|
encode::write_u8(&mut output, 1)?;
|
||||||
encode::write_str(&mut output, id)?;
|
encode::write_str(&mut output, id.0.as_str())?;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -92,7 +92,7 @@ impl HistoryRecord {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(HistoryRecord::Delete(id.to_string()))
|
Ok(HistoryRecord::Delete(id.to_string().into()))
|
||||||
}
|
}
|
||||||
|
|
||||||
n => {
|
n => {
|
||||||
@ -134,7 +134,7 @@ impl HistoryStore {
|
|||||||
Ok(idx)
|
Ok(idx)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete(&self, id: String) -> Result<RecordIdx> {
|
pub async fn delete(&self, id: HistoryId) -> Result<RecordIdx> {
|
||||||
let record = HistoryRecord::Delete(id);
|
let record = HistoryRecord::Delete(id);
|
||||||
|
|
||||||
self.push_record(record).await
|
self.push_record(record).await
|
||||||
@ -171,7 +171,7 @@ mod tests {
|
|||||||
];
|
];
|
||||||
|
|
||||||
let history = History {
|
let history = History {
|
||||||
id: "018cd4fe81757cd2aee65cd7861f9c81".to_owned(),
|
id: "018cd4fe81757cd2aee65cd7861f9c81".to_owned().into(),
|
||||||
timestamp: datetime!(2024-01-04 00:00:00.000000 +00:00),
|
timestamp: datetime!(2024-01-04 00:00:00.000000 +00:00),
|
||||||
duration: 100,
|
duration: 100,
|
||||||
exit: 0,
|
exit: 0,
|
||||||
@ -203,7 +203,7 @@ mod tests {
|
|||||||
204, 1, 217, 32, 48, 49, 56, 99, 100, 52, 102, 101, 56, 49, 55, 53, 55, 99, 100, 50,
|
204, 1, 217, 32, 48, 49, 56, 99, 100, 52, 102, 101, 56, 49, 55, 53, 55, 99, 100, 50,
|
||||||
97, 101, 101, 54, 53, 99, 100, 55, 56, 54, 49, 102, 57, 99, 56, 49,
|
97, 101, 101, 54, 53, 99, 100, 55, 56, 54, 49, 102, 57, 99, 56, 49,
|
||||||
];
|
];
|
||||||
let record = HistoryRecord::Delete("018cd4fe81757cd2aee65cd7861f9c81".to_string());
|
let record = HistoryRecord::Delete("018cd4fe81757cd2aee65cd7861f9c81".to_string().into());
|
||||||
|
|
||||||
let serialized = record.serialize().expect("failed to serialize history");
|
let serialized = record.serialize().expect("failed to serialize history");
|
||||||
assert_eq!(serialized.0, bytes);
|
assert_eq!(serialized.0, bytes);
|
||||||
|
@ -73,7 +73,7 @@ async fn sync_download(
|
|||||||
.map(|h| serde_json::from_str(h).expect("invalid base64"))
|
.map(|h| serde_json::from_str(h).expect("invalid base64"))
|
||||||
.map(|h| decrypt(h, key).expect("failed to decrypt history! check your key"))
|
.map(|h| decrypt(h, key).expect("failed to decrypt history! check your key"))
|
||||||
.map(|mut h| {
|
.map(|mut h| {
|
||||||
if remote_deleted.contains(h.id.as_str()) {
|
if remote_deleted.contains(h.id.0.as_str()) {
|
||||||
h.deleted_at = Some(time::OffsetDateTime::now_utc());
|
h.deleted_at = Some(time::OffsetDateTime::now_utc());
|
||||||
h.command = String::from("");
|
h.command = String::from("");
|
||||||
}
|
}
|
||||||
@ -157,7 +157,7 @@ async fn sync_upload(
|
|||||||
let data = serde_json::to_string(&data)?;
|
let data = serde_json::to_string(&data)?;
|
||||||
|
|
||||||
let add_hist = AddHistoryRequest {
|
let add_hist = AddHistoryRequest {
|
||||||
id: i.id,
|
id: i.id.to_string(),
|
||||||
timestamp: i.timestamp,
|
timestamp: i.timestamp,
|
||||||
data,
|
data,
|
||||||
hostname: hash_str(&i.hostname),
|
hostname: hash_str(&i.hostname),
|
||||||
@ -177,7 +177,7 @@ async fn sync_upload(
|
|||||||
let deleted = db.deleted().await?;
|
let deleted = db.deleted().await?;
|
||||||
|
|
||||||
for i in deleted {
|
for i in deleted {
|
||||||
if remote_deleted.contains(&i.id) {
|
if remote_deleted.contains(&i.id.to_string()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user