mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-12-02 13:04:08 +01:00
PR 2: proxy services (#787)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
686294939b
commit
9bbd28ce4a
26 files changed
+3802
-50
No files matched your search
@@ -1,10 +1,11 @@
|
||||
use include_dir::{Dir, include_dir};
|
||||
use r2d2::Pool;
|
||||
use r2d2_sqlite::SqliteConnectionManager;
|
||||
use rusqlite::{Transaction, TransactionBehavior};
|
||||
use rusqlite::{OpenFlags, Transaction, TransactionBehavior};
|
||||
use std::path::Path;
|
||||
use std::time::Duration;
|
||||
use yaak_database::{ConnectionOrTx, DbContext, Error, Result, SqlitePool, run_migrations};
|
||||
use yaak_models::blob_manager::BlobManager;
|
||||
|
||||
static MIGRATIONS: Dir<'static> = include_dir!("$CARGO_MANIFEST_DIR/migrations");
|
||||
|
||||
@@ -26,6 +27,13 @@ impl ProxyQueryManager {
|
||||
Ok(Self { readers: file_pool(db_path, 5)?, writer })
|
||||
}
|
||||
|
||||
/// `proxy.db` for reading while another process may be writing it, or none is. Writes
|
||||
/// fail. No migrations run, so an older database reads as it is.
|
||||
pub fn open_read_only(db_path: &Path) -> Result<Self> {
|
||||
let readers = read_only_pool(db_path)?;
|
||||
Ok(Self { writer: readers.clone(), readers })
|
||||
}
|
||||
|
||||
pub fn with_conn<F, T>(&self, func: F) -> T
|
||||
where
|
||||
F: FnOnce(&DbContext) -> T,
|
||||
@@ -62,6 +70,23 @@ impl ProxyQueryManager {
|
||||
}
|
||||
}
|
||||
|
||||
/// `blobs.sqlite` for reading, alongside [`ProxyQueryManager::open_read_only`].
|
||||
pub fn open_blobs_read_only(path: &Path) -> Result<BlobManager> {
|
||||
let readers = read_only_pool(path)?;
|
||||
Ok(BlobManager::new(readers.clone(), readers))
|
||||
}
|
||||
|
||||
fn read_only_pool(path: &Path) -> Result<SqlitePool> {
|
||||
let manager = SqliteConnectionManager::file(path)
|
||||
.with_flags(OpenFlags::SQLITE_OPEN_READ_ONLY | OpenFlags::SQLITE_OPEN_NO_MUTEX)
|
||||
.with_init(|conn| conn.busy_timeout(Duration::from_millis(5000)));
|
||||
Pool::builder()
|
||||
.max_size(2)
|
||||
.connection_timeout(Duration::from_secs(10))
|
||||
.build(manager)
|
||||
.map_err(|e| Error::Database(e.to_string()))
|
||||
}
|
||||
|
||||
fn file_pool(path: &Path, max_size: u32) -> Result<SqlitePool> {
|
||||
let manager = SqliteConnectionManager::file(path).with_init(|conn| {
|
||||
conn.pragma_update(None, "journal_mode", "WAL")?;
|
||||
@@ -85,6 +110,28 @@ mod tests {
|
||||
stmt.query_map([], |r| r.get::<_, String>("name")).unwrap().map(|c| c.unwrap()).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reads_flows_read_only_before_migrations() {
|
||||
use crate::models::Flow;
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = dir.path().join("proxy.db");
|
||||
drop(ProxyQueryManager::new(&path).unwrap());
|
||||
|
||||
// As a build from before the saved request column left it.
|
||||
let conn = Connection::open(&path).unwrap();
|
||||
conn.execute_batch(
|
||||
"ALTER TABLE flows DROP COLUMN saved_request_id;
|
||||
INSERT INTO flows (id, url, method) VALUES ('fl_old', 'https://example.com', 'GET');",
|
||||
)
|
||||
.unwrap();
|
||||
drop(conn);
|
||||
|
||||
let db = ProxyQueryManager::open_read_only(&path).unwrap();
|
||||
let flows = db.with_conn(|c| c.find_all::<Flow>()).unwrap();
|
||||
assert_eq!(flows.len(), 1);
|
||||
assert_eq!((flows[0].id.as_str(), flows[0].saved_request_id.as_deref()), ("fl_old", None));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn migrates_an_old_schema_db() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
@@ -124,8 +171,7 @@ mod tests {
|
||||
assert!(flow_columns.contains(&"response_truncated".to_string()));
|
||||
assert!(!flow_columns.iter().any(|c| c.contains("body")), "{flow_columns:?}");
|
||||
|
||||
let journal_mode: String =
|
||||
conn.query_row("PRAGMA journal_mode", [], |r| r.get(0)).unwrap();
|
||||
let journal_mode: String = conn.query_row("PRAGMA journal_mode", [], |r| r.get(0)).unwrap();
|
||||
assert_eq!(journal_mode, "wal");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user