i KNEW there are problems, probably

This commit is contained in:
lda
2026-05-03 17:43:41 +07:00 Verified
parent b573e9f27f
commit 0c22fb5e41
16 changed files with 25 additions and 168 deletions
+13 -2
View File
@@ -17,10 +17,21 @@ pub struct Store {
db_path: PathBuf,
pool: SqlitePool,
}
impl Store {
/// Begins a new transaction with an IMMEDIATE write lock.
///
/// Use this for all transactions that perform writes to avoid SQLite upgrade deadlocks.
pub async fn begin_write(&self) -> Result<Transaction<'static, Sqlite>> {
self.pool
.begin_with("BEGIN IMMEDIATE")
.await
.context("failed to begin write transaction")
}
}
const SCHEMA_VERSION_KEY: &str = "schema_version";
pub(crate) const SCHEMA_VERSION_KEY: &str = "schema_version";
const SEEDED_ENROLL_TOKEN_PREFIX: &str = "seeded_enroll_token:";
const SCHEMA_VERSION: u32 = 2;
pub(crate) const SCHEMA_VERSION: u32 = 2;
pub(crate) mod agent_devices;
mod alerts;
@@ -15,11 +15,7 @@ impl Store {
agent_id: &str,
devices: &[Device],
) -> Result<usize> {
let mut tx = self
.pool
.begin()
.await
.context("failed starting device snapshot transaction")?;
let mut tx = self.begin_write().await?;
let mut incoming_keys = std::collections::HashSet::with_capacity(devices.len());
let snapshot_time = now_unix();
@@ -1,3 +1,4 @@
use super::helpers::core::*;
use super::*;
impl Store {
@@ -5,6 +6,8 @@ impl Store {
&self,
current: &[AlertState],
) -> Result<Vec<AlertTransition>> {
let mut tx = self.begin_write().await?;
let mut previous = std::collections::HashMap::<String, AlertState>::new();
let rows = sqlx::query_as!(
AlertStateRow,
@@ -13,7 +16,7 @@ impl Store {
value, threshold, last_seen_unix, metadata_json as "metadata_json!"
FROM active_alerts"#,
)
.fetch_all(&self.pool)
.fetch_all(&mut *tx)
.await
.context("failed iterating active_alerts table")?;
for row in rows {
@@ -63,11 +66,6 @@ impl Store {
});
}
let mut tx = self
.pool
.begin()
.await
.context("failed starting alert transaction")?;
sqlx::query!("DELETE FROM active_alerts")
.execute(&mut *tx)
.await
+1 -5
View File
@@ -115,11 +115,7 @@ impl Store {
}
let expires_at = now_unix().saturating_add(seed_ttl.as_secs().max(1));
let mut tx = self
.pool
.begin()
.await
.context("failed starting bootstrap token transaction")?;
let mut tx = self.begin_write().await?;
let expires_at_i64 = i64::try_from(expires_at).context("token expiry overflow")?;
sqlx::query!(
"INSERT OR REPLACE INTO enroll_tokens (token, expires_at_unix) VALUES (?1, ?2)",
+4 -20
View File
@@ -11,11 +11,7 @@ impl Store {
let device_id = format!("dev-{}", Uuid::new_v4());
let now = now_unix();
let mut tx = self
.pool
.begin()
.await
.context("failed starting known device transaction")?;
let mut tx = self.begin_write().await?;
let pinned = if input.pinned { 1_i64 } else { 0_i64 };
let now_i64 = i64::try_from(now).context("known device timestamp overflow")?;
sqlx::query!(
@@ -84,11 +80,7 @@ impl Store {
let now = now_unix();
let now_i64 = i64::try_from(now).context("known device timestamp overflow")?;
let mut tx = self
.pool
.begin()
.await
.context("failed starting known device merge transaction")?;
let mut tx = self.begin_write().await?;
let target_exists = sqlx::query_scalar!(
r#"SELECT COUNT(*) as "count!: i64" FROM known_devices WHERE device_id = ?1"#,
@@ -149,11 +141,7 @@ impl Store {
) -> Result<Option<KnownDevice>> {
let identifier = normalize_device_identifier(input)?;
let now = now_unix();
let mut tx = self
.pool
.begin()
.await
.context("failed starting device identifier transaction")?;
let mut tx = self.begin_write().await?;
let exists = sqlx::query_scalar!(
r#"SELECT COUNT(*) as "count!: i64" FROM known_devices WHERE device_id = ?1"#,
device_id
@@ -188,11 +176,7 @@ impl Store {
) -> Result<Option<KnownDevice>> {
let now = now_unix();
let now_i64 = i64::try_from(now).context("known device timestamp overflow")?;
let mut tx = self
.pool
.begin()
.await
.context("failed starting device identifier detach transaction")?;
let mut tx = self.begin_write().await?;
let exists = sqlx::query_scalar!(
r#"SELECT COUNT(*) as "count!: i64" FROM known_devices WHERE device_id = ?1"#,
device_id
@@ -2,11 +2,7 @@ use super::*;
impl Store {
pub async fn enroll(&self, enroll_token: &str) -> Result<IssuedAgent> {
let mut tx = self
.pool
.begin()
.await
.context("failed starting enroll transaction")?;
let mut tx = self.begin_write().await?;
let expires_at_unix = sqlx::query_scalar!(
"SELECT expires_at_unix FROM enroll_tokens WHERE token = ?1",
enroll_token
@@ -102,11 +98,7 @@ impl Store {
}
pub async fn revoke_agent(&self, agent_id: &str) -> Result<bool> {
let mut tx = self
.pool
.begin()
.await
.context("failed starting revoke transaction")?;
let mut tx = self.begin_write().await?;
let result = sqlx::query!("DELETE FROM agents WHERE agent_id = ?1", agent_id)
.execute(&mut *tx)
.await