diff --git a/crates/core/src/db/datastore/locking_tx_datastore/datastore.rs b/crates/core/src/db/datastore/locking_tx_datastore/datastore.rs index 8c1fde5f39c..d915f542ac5 100644 --- a/crates/core/src/db/datastore/locking_tx_datastore/datastore.rs +++ b/crates/core/src/db/datastore/locking_tx_datastore/datastore.rs @@ -1261,10 +1261,7 @@ mod tests { } fn basic_table_schema() -> TableSchema { - TableSchema::new( - TableId::SENTINEL, - "Foo".into(), - map_array(basic_table_schema_cols()), + basic_table_schema_with_indices( vec![ IndexSchema { index_id: IndexId::SENTINEL, @@ -1297,6 +1294,16 @@ mod tests { }), }, ], + ) + } + + fn basic_table_schema_with_indices(indices: Vec, constraints: Vec) -> TableSchema { + TableSchema::new( + TableId::SENTINEL, + "Foo".into(), + map_array(basic_table_schema_cols()), + indices, + constraints, vec![SequenceSchema { sequence_id: SequenceId::SENTINEL, table_id: TableId::SENTINEL, @@ -2068,6 +2075,26 @@ mod tests { Ok(()) } + #[test] + fn test_update_no_such_index() -> ResultTest<()> { + let datastore = get_datastore()?; + let mut tx = datastore.begin_mut_tx(IsolationLevel::Serializable, Workload::ForTests); + let schema = basic_table_schema_with_indices([].into(), [].into()); + let table_id = datastore.create_table_mut_tx(&mut tx, schema)?; + + // There are no indices attached to `table_id`. + let index_id = 0.into(); + let row = to_vec(&u32_str_u32(42, "foo", 24)).unwrap(); + let err = datastore + .update_mut_tx(&mut tx, table_id, index_id, &row) + .expect_err("update using a non-existent index should error") + .into_index() + .expect("the error should be an index error"); + assert_eq!(err, IndexError::NotFound(index_id)); + + Ok(()) + } + #[test] /// Test that two read-only TXes can operate concurrently without deadlock or blocking, /// and that both observe correct results for a simple table scan. diff --git a/crates/core/src/error.rs b/crates/core/src/error.rs index 469541a42c5..a11c9e3d4d3 100644 --- a/crates/core/src/error.rs +++ b/crates/core/src/error.rs @@ -3,6 +3,7 @@ use std::num::ParseIntError; use std::path::PathBuf; use std::sync::{MutexGuard, PoisonError}; +use enum_as_inner::EnumAsInner; use hex::FromHexError; use spacetimedb_expr::errors::TypingError; use spacetimedb_sats::AlgebraicType; @@ -171,7 +172,7 @@ pub enum SequenceError { MultiColumnAutoInc(TableId, ColList), } -#[derive(Error, Debug)] +#[derive(Error, Debug, EnumAsInner)] pub enum DBError { #[error("LibError: {0}")] Lib(#[from] LibError),