1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
use std::path::Path;
use std::str::FromStr;
use std::time::Duration;
use crate::atuin_common::record::HostId;
use eyre::{Result, eyre};
use sqlx::sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePool, SqlitePoolOptions};
use time::{OffsetDateTime, format_description::well_known::Rfc3339};
use tokio::sync::OnceCell;
use tracing::debug;
use uuid::Uuid;
const KEY_HOST_ID: &str = "host_id";
const KEY_LAST_SYNC: &str = "last_sync_time";
const KEY_SESSION: &str = "session";
pub(crate) struct MetaStore {
pool: SqlitePool,
cached_host_id: OnceCell<HostId>,
}
impl MetaStore {
pub(crate) async fn new(path: impl AsRef<Path>, timeout: f64) -> Result<Self> {
let path = path.as_ref();
let path_str = path
.as_os_str()
.to_str()
.ok_or_else(|| eyre!("meta database path is not valid UTF-8: {path:?}"))?;
debug!("opening meta sqlite database at {path:?}");
let is_memory = path_str.contains(":memory:");
if !is_memory
&& !path.exists()
&& let Some(dir) = path.parent()
{
fs_err::create_dir_all(dir)?;
}
// Use DELETE journal mode instead of WAL. This is a small, infrequently-
// written KV store — WAL's concurrency benefits aren't needed, and DELETE
// mode avoids creating auxiliary -wal/-shm files that complicate
// permission handling.
let opts = SqliteConnectOptions::from_str(path_str)?
.journal_mode(SqliteJournalMode::Delete)
.optimize_on_close(true, None)
.create_if_missing(true);
let pool = SqlitePoolOptions::new()
.acquire_timeout(Duration::from_secs_f64(timeout))
.connect_with(opts)
.await?;
sqlx::migrate!("./db/client-meta-migrations")
.run(&pool)
.await?;
// Session tokens are stored in this database, so restrict permissions.
#[cfg(unix)]
if !is_memory {
use std::os::unix::fs::PermissionsExt;
std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o600))?;
}
let store = Self {
pool,
cached_host_id: OnceCell::const_new(),
};
Ok(store)
}
// Generic key-value operations
pub(crate) async fn get(&self, key: &str) -> Result<Option<String>> {
let row: Option<(String,)> = sqlx::query_as("SELECT value FROM meta WHERE key = ?1")
.bind(key)
.fetch_optional(&self.pool)
.await?;
Ok(row.map(|r| r.0))
}
pub(crate) async fn set(&self, key: &str, value: &str) -> Result<()> {
sqlx::query(
"
INSERT INTO meta (key, value, updated_at)
VALUES (?1, ?2, strftime('%s', 'now'))
ON CONFLICT(key) DO UPDATE
SET value = ?2, updated_at = strftime('%s', 'now')
",
)
.bind(key)
.bind(value)
.execute(&self.pool)
.await?;
Ok(())
}
pub(crate) async fn delete(&self, key: &str) -> Result<()> {
sqlx::query("DELETE FROM meta WHERE key = ?1")
.bind(key)
.execute(&self.pool)
.await?;
Ok(())
}
// Typed accessors
pub(crate) async fn host_id(&self) -> Result<HostId> {
self.cached_host_id
.get_or_try_init(|| async {
if let Some(id) = self.get(KEY_HOST_ID).await? {
let parsed = Uuid::from_str(id.as_str())
.map_err(|e| eyre!("failed to parse host ID: {e}"))?;
return Ok(HostId(parsed));
}
let uuid = crate::atuin_common::utils::uuid_v7();
self.set(KEY_HOST_ID, uuid.as_simple().to_string().as_ref())
.await?;
Ok(HostId(uuid))
})
.await
.copied()
}
pub(crate) async fn last_sync(&self) -> Result<OffsetDateTime> {
match self.get(KEY_LAST_SYNC).await? {
Some(v) => Ok(OffsetDateTime::parse(v.as_str(), &Rfc3339)?),
None => Ok(OffsetDateTime::UNIX_EPOCH),
}
}
pub(crate) async fn save_sync_time(&self) -> Result<()> {
self.set(
KEY_LAST_SYNC,
OffsetDateTime::now_utc().format(&Rfc3339)?.as_str(),
)
.await
}
pub(crate) async fn session_token(&self) -> Result<Option<String>> {
self.get(KEY_SESSION).await
}
pub(crate) async fn save_session(&self, token: &str) -> Result<()> {
self.set(KEY_SESSION, token).await
}
pub(crate) async fn delete_session(&self) -> Result<()> {
self.delete(KEY_SESSION).await
}
pub(crate) async fn logged_in(&self) -> Result<bool> {
Ok(self.session_token().await?.is_some())
}
}
#[cfg(test)]
mod tests {
use super::*;
async fn new_test_store() -> MetaStore {
MetaStore::new("sqlite::memory:", 2.0).await.unwrap()
}
#[tokio::test]
async fn test_get_set_delete() {
let store = new_test_store().await;
assert_eq!(store.get("foo").await.unwrap(), None);
store.set("foo", "bar").await.unwrap();
assert_eq!(store.get("foo").await.unwrap(), Some("bar".to_string()));
store.set("foo", "baz").await.unwrap();
assert_eq!(store.get("foo").await.unwrap(), Some("baz".to_string()));
store.delete("foo").await.unwrap();
assert_eq!(store.get("foo").await.unwrap(), None);
}
#[tokio::test]
async fn test_host_id_generation_and_stability() {
let store = new_test_store().await;
let id1 = store.host_id().await.unwrap();
let id2 = store.host_id().await.unwrap();
assert_eq!(id1, id2, "host_id should be stable across calls");
}
#[tokio::test]
async fn test_sync_time() {
let store = new_test_store().await;
let t = store.last_sync().await.unwrap();
assert_eq!(t, OffsetDateTime::UNIX_EPOCH);
store.save_sync_time().await.unwrap();
let t = store.last_sync().await.unwrap();
assert!(t > OffsetDateTime::UNIX_EPOCH);
}
#[tokio::test]
async fn test_session_crud() {
let store = new_test_store().await;
assert!(!store.logged_in().await.unwrap());
assert_eq!(store.session_token().await.unwrap(), None);
store.save_session("tok123").await.unwrap();
assert!(store.logged_in().await.unwrap());
assert_eq!(
store.session_token().await.unwrap(),
Some("tok123".to_string())
);
store.delete_session().await.unwrap();
assert!(!store.logged_in().await.unwrap());
}
}
|