Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 66 additions & 36 deletions crates/cli/src/commands/local_db/pipeline/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ mod tests {
use alloy::primitives::Address;
use async_trait::async_trait;
use raindex_app_settings::local_db_manifest::DB_SCHEMA_VERSION;
use raindex_common::local_db::query::clear_tables::clear_tables_stmt;
use raindex_common::local_db::query::create_tables::create_tables_stmt;
use raindex_common::local_db::query::clear_tables::{clear_tables_batch, vacuum_stmt};
use raindex_common::local_db::query::create_tables::create_tables_batch;
use raindex_common::local_db::query::insert_db_metadata::insert_db_metadata_stmt;
use raindex_common::local_db::query::{
FromDbJson, LocalDbQueryError, LocalDbQueryExecutor, SqlStatement, SqlStatementBatch,
Expand Down Expand Up @@ -78,6 +78,48 @@ mod tests {
.iter()
.fold(self, |db, stmt| db.with_text(stmt, "ok"))
}

fn with_batch(self, batch: &SqlStatementBatch) -> Self {
batch
.statements()
.iter()
.fold(self, |db, stmt| db.with_text(stmt, "ok"))
}

fn with_reset_batches(self) -> Self {
self.with_batch(&clear_tables_batch())
.with_text(&vacuum_stmt(), "ok")
.with_batch(&create_tables_batch())
}
}

fn reset_batch_sqls() -> Vec<String> {
let mut statements = clear_tables_batch().statements().to_vec();
statements.push(vacuum_stmt());
statements.extend(create_tables_batch().statements().iter().cloned());
statements
.iter()
.map(|stmt| stmt.sql().to_string())
.collect()
}

fn assert_reset_batches_were_called(calls: &[String]) {
for sql in reset_batch_sqls() {
assert!(calls.contains(&sql), "missing reset SQL: {sql}");
}
}

fn first_reset_sql() -> String {
clear_tables_batch().statements()[0].sql().to_string()
}

fn last_reset_sql() -> String {
create_tables_batch()
.statements()
.last()
.unwrap()
.sql()
.to_string()
}

#[cfg_attr(target_family = "wasm", async_trait(?Send))]
Expand Down Expand Up @@ -119,8 +161,7 @@ mod tests {
async fn engine_run_resets_and_does_not_import_when_no_dump() {
let adapter = ProducerBootstrapAdapter::new();
let db = MockDb::default()
.with_text(&clear_tables_stmt(), "ok")
.with_text(&create_tables_stmt(), "ok")
.with_reset_batches()
.with_text(&insert_db_metadata_stmt(DB_SCHEMA_VERSION), "ok")
.with_views();

Expand All @@ -135,27 +176,23 @@ mod tests {
adapter.engine_run(&db, &cfg).await.unwrap();

let calls = db.calls();
// Presence assertions
let clear = clear_tables_stmt().sql().to_string();
let create = create_tables_stmt().sql().to_string();
let reset_start = first_reset_sql();
let reset_end = last_reset_sql();
let insert = insert_db_metadata_stmt(DB_SCHEMA_VERSION).sql().to_string();

assert!(calls.contains(&clear));
assert!(calls.contains(&create));
assert_reset_batches_were_called(&calls);
assert!(calls.contains(&insert));

// Ordering: clear -> create -> insert
let idx = |s: &String| calls.iter().position(|c| c == s).unwrap();
assert!(idx(&clear) < idx(&create));
assert!(idx(&create) < idx(&insert));
assert!(idx(&reset_start) < idx(&reset_end));
assert!(idx(&reset_end) < idx(&insert));
}

#[tokio::test]
async fn engine_run_executes_view_creation() {
let adapter = ProducerBootstrapAdapter::new();
let db = MockDb::default()
.with_text(&clear_tables_stmt(), "ok")
.with_text(&create_tables_stmt(), "ok")
.with_reset_batches()
.with_text(&insert_db_metadata_stmt(DB_SCHEMA_VERSION), "ok")
.with_views();

Expand Down Expand Up @@ -187,8 +224,7 @@ mod tests {
let adapter = ProducerBootstrapAdapter::new();
let dump_stmt = SqlStatement::new("--dump-sql");
let db = MockDb::default()
.with_text(&clear_tables_stmt(), "ok")
.with_text(&create_tables_stmt(), "ok")
.with_reset_batches()
.with_text(&insert_db_metadata_stmt(DB_SCHEMA_VERSION), "ok")
.with_text(&dump_stmt, "ok")
.with_views();
Expand All @@ -204,21 +240,18 @@ mod tests {
adapter.engine_run(&db, &cfg).await.unwrap();

let calls = db.calls();
// Presence assertions
let clear = clear_tables_stmt().sql().to_string();
let create = create_tables_stmt().sql().to_string();
let reset_start = first_reset_sql();
let reset_end = last_reset_sql();
let insert = insert_db_metadata_stmt(DB_SCHEMA_VERSION).sql().to_string();
let dump = dump_stmt.sql().to_string();

assert!(calls.contains(&clear));
assert!(calls.contains(&create));
assert_reset_batches_were_called(&calls);
assert!(calls.contains(&insert));
assert!(calls.contains(&dump));

// Ordering: clear -> create -> insert -> dump
let idx = |s: &String| calls.iter().position(|c| c == s).unwrap();
assert!(idx(&clear) < idx(&create));
assert!(idx(&create) < idx(&insert));
assert!(idx(&reset_start) < idx(&reset_end));
assert!(idx(&reset_end) < idx(&insert));
assert!(idx(&insert) < idx(&dump));
}

Expand All @@ -227,8 +260,7 @@ mod tests {
let adapter = ProducerBootstrapAdapter::new();
let dump_stmt = SqlStatement::new("--dump-sql-missing");
let db = MockDb::default()
.with_text(&clear_tables_stmt(), "ok")
.with_text(&create_tables_stmt(), "ok")
.with_reset_batches()
.with_text(&insert_db_metadata_stmt(DB_SCHEMA_VERSION), "ok")
.with_views();

Expand All @@ -245,28 +277,26 @@ mod tests {
assert!(result.is_err());

let calls = db.calls();
let clear = clear_tables_stmt().sql().to_string();
let create = create_tables_stmt().sql().to_string();
let reset_start = first_reset_sql();
let reset_end = last_reset_sql();
let insert = insert_db_metadata_stmt(DB_SCHEMA_VERSION).sql().to_string();
let dump = dump_stmt.sql().to_string();

assert!(calls.contains(&clear));
assert!(calls.contains(&create));
assert_reset_batches_were_called(&calls);
assert!(calls.contains(&insert));
assert!(calls.contains(&dump));

// Ordering: clear -> create -> insert -> dump (dump last attempted and fails)
let idx = |s: &String| calls.iter().position(|c| c == s).unwrap();
assert!(idx(&clear) < idx(&create));
assert!(idx(&create) < idx(&insert));
assert!(idx(&reset_start) < idx(&reset_end));
assert!(idx(&reset_end) < idx(&insert));
assert!(idx(&insert) < idx(&dump));
}

#[tokio::test]
async fn engine_run_propagates_reset_error() {
let adapter = ProducerBootstrapAdapter::new();
let db = MockDb::default()
.with_text(&clear_tables_stmt(), "ok")
.with_batch(&clear_tables_batch())
.with_views();

let cfg = BootstrapConfig {
Expand All @@ -284,8 +314,8 @@ mod tests {
}

let calls = db.calls();
assert!(calls.contains(&clear_tables_stmt().sql().to_string()));
assert!(calls.contains(&create_tables_stmt().sql().to_string()));
assert_eq!(calls.len(), clear_tables_batch().len() + 1);
assert_eq!(calls.last().unwrap(), vacuum_stmt().sql());
}

#[tokio::test]
Expand Down
20 changes: 10 additions & 10 deletions crates/common/src/local_db/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ fn sqlite_file_paths(db_path: &Path) -> Vec<PathBuf> {
#[cfg(test)]
mod tests {
use super::*;
use crate::local_db::query::create_tables::create_tables_stmt;
use crate::local_db::query::create_tables::{create_tables_batch, create_tables_sql};
use tempfile::TempDir;

#[test]
Expand All @@ -309,15 +309,15 @@ mod tests {
#[test]
fn stamped_db_passes_schema_guard() {
let conn = Connection::open_in_memory().unwrap();
conn.execute_batch(create_tables_stmt().sql())
conn.execute_batch(create_tables_sql())
.expect("create tables stamps the header");
verify_schema_guard(&conn).expect("correctly stamped db should pass the guard");
}

#[test]
fn wrong_application_id_is_detected() {
let conn = Connection::open_in_memory().unwrap();
conn.execute_batch(create_tables_stmt().sql())
conn.execute_batch(create_tables_sql())
.expect("create tables stamps the header");
// Re-stamp the header with a foreign application_id (a SQLite file that
// is not a raindex local-db) while leaving user_version valid.
Expand All @@ -337,7 +337,7 @@ mod tests {
#[test]
fn negative_application_id_is_detected() {
let conn = Connection::open_in_memory().unwrap();
conn.execute_batch(create_tables_stmt().sql())
conn.execute_batch(create_tables_sql())
.expect("create tables stamps the header");
// A foreign file whose application_id has the top bit set reads back as
// a negative i32. It is neither zero (unstamped) nor the raindex magic,
Expand All @@ -358,7 +358,7 @@ mod tests {
#[test]
fn wrong_user_version_is_detected() {
let conn = Connection::open_in_memory().unwrap();
conn.execute_batch(create_tables_stmt().sql())
conn.execute_batch(create_tables_sql())
.expect("create tables stamps the header");
// Keep the raindex application_id but advance user_version to a stale /
// future schema number.
Expand All @@ -378,7 +378,7 @@ mod tests {
#[test]
fn stale_user_version_is_detected() {
let conn = Connection::open_in_memory().unwrap();
conn.execute_batch(create_tables_stmt().sql())
conn.execute_batch(create_tables_sql())
.expect("create tables stamps the header");
// Keep the raindex application_id but roll user_version back to an older
// schema number. A stale (lower) version must be rejected just like a
Expand All @@ -405,7 +405,7 @@ mod tests {
// in the file header so a fresh open must reject it.
{
let conn = Connection::open(&db_path).unwrap();
conn.execute_batch(create_tables_stmt().sql()).unwrap();
conn.execute_batch(create_tables_sql()).unwrap();
conn.pragma_update(None, "application_id", RAINDEX_APPLICATION_ID + 7)
.unwrap();
}
Expand All @@ -428,7 +428,7 @@ mod tests {

{
let conn = Connection::open(&db_path).unwrap();
conn.execute_batch(create_tables_stmt().sql()).unwrap();
conn.execute_batch(create_tables_sql()).unwrap();
conn.pragma_update(None, "user_version", DB_SCHEMA_VERSION as i32 + 1)
.unwrap();
}
Expand All @@ -451,7 +451,7 @@ mod tests {

let exec = RusqliteExecutor::new(&db_path);
// First open hits an empty file (application_id == 0) and stamps it.
exec.query_text(&create_tables_stmt()).await.unwrap();
exec.execute_batch(&create_tables_batch()).await.unwrap();

// A subsequent open must read back the stamped header and succeed.
#[derive(serde::Deserialize)]
Expand All @@ -477,7 +477,7 @@ mod tests {
let db_path = temp_dir.path().join("dump.db");

let exec = RusqliteExecutor::new(&db_path);
exec.query_text(&create_tables_stmt()).await.unwrap();
exec.execute_batch(&create_tables_batch()).await.unwrap();

// Apply a data-only insert batch (the shape produced by export_data_only).
let mut batch = SqlStatementBatch::new();
Expand Down
Loading
Loading