From 8e4ae6229898dd3ff63ad4edb3c1a235058f6118 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Tue, 10 Oct 2023 08:59:22 +0200 Subject: [PATCH] refactor locking_tx_datastore tests --- .../db/datastore/locking_tx_datastore/mod.rs | 377 +++++++++--------- crates/core/src/db/datastore/system_tables.rs | 2 +- 2 files changed, 186 insertions(+), 193 deletions(-) diff --git a/crates/core/src/db/datastore/locking_tx_datastore/mod.rs b/crates/core/src/db/datastore/locking_tx_datastore/mod.rs index 00ad1f02f0..15e34d34b8 100644 --- a/crates/core/src/db/datastore/locking_tx_datastore/mod.rs +++ b/crates/core/src/db/datastore/locking_tx_datastore/mod.rs @@ -2151,9 +2151,9 @@ impl traits::MutProgrammable for Locking { #[cfg(test)] mod tests { - use super::{ColId, Locking, StTableRow}; + use super::{ColId, Locking, MutTxId, StTableRow}; use crate::db::datastore::system_tables::{StConstraintRow, ST_CONSTRAINTS_ID}; - use crate::db::datastore::traits::IndexId; + use crate::db::datastore::traits::{IndexId, TableId}; use crate::{ db::datastore::{ locking_tx_datastore::{ @@ -2182,6 +2182,66 @@ mod tests { Locking::bootstrap() } + fn index_row(index_id: u32, table_id: u32, col_id: u32, name: &str, is_unique: bool) -> StIndexRow { + StIndexRow { + index_id, + table_id, + cols: NonEmpty::new(col_id), + index_name: name.into(), + is_unique, + } + } + + fn table_row( + table_id: u32, + table_name: &str, + table_type: StTableType, + table_access: StAccess, + ) -> StTableRow { + StTableRow { + table_id, + table_name: table_name.into(), + table_type, + table_access, + } + } + + fn column_row( + table_id: u32, + col_id: u32, + col_name: &str, + col_type: AlgebraicType, + is_autoinc: bool, + ) -> StColumnRow { + StColumnRow { + table_id, + col_id, + col_name: col_name.into(), + col_type, + is_autoinc, + } + } + + fn column_schema(table_id: u32, id: u32, name: &str, ty: AlgebraicType, is_autoinc: bool) -> ColumnSchema { + ColumnSchema { + table_id, + col_id: id, + col_name: name.to_string(), + col_type: ty, + is_autoinc, + } + } + + fn index_schema(id: u32, table_id: u32, col_id: u32, name: &str, is_unique: bool) -> IndexSchema { + IndexSchema { + index_id: id, + table_id, + cols: NonEmpty::new(col_id), + index_name: name.to_string(), + is_unique, + } + } + fn basic_table_schema() -> TableDef { TableDef { table_name: "Foo".into(), @@ -2221,6 +2281,14 @@ mod tests { } } + fn setup_table() -> ResultTest<(Locking, MutTxId, TableId)> { + let datastore = get_datastore()?; + let mut tx = datastore.begin_mut_tx(); + let schema = basic_table_schema(); + let table_id = datastore.create_table_mut_tx(&mut tx, schema)?; + Ok((datastore, tx, table_id)) + } + #[test] fn test_bootstrapping_sets_up_tables() -> ResultTest<()> { let datastore = get_datastore()?; @@ -2234,12 +2302,12 @@ mod tests { assert_eq!( table_rows, vec![ - StTableRow { table_id: 0, table_name: "st_table".to_string(), table_type: StTableType::System, table_access: StAccess::Public }, - StTableRow { table_id: 1, table_name: "st_columns".to_string(), table_type: StTableType::System, table_access: StAccess::Public }, - StTableRow { table_id: 2, table_name: "st_sequence".to_string(), table_type: StTableType::System, table_access: StAccess::Public}, - StTableRow { table_id: 3, table_name: "st_indexes".to_string() , table_type: StTableType::System, table_access: StAccess::Public}, - StTableRow { table_id: 4, table_name: "st_constraints".to_string() , table_type: StTableType::System, table_access: StAccess::Public}, - StTableRow { table_id: 5, table_name: "st_module".to_string() , table_type: StTableType::System, table_access: StAccess::Public}, + table_row(0, "st_table", StTableType::System, StAccess::Public), + table_row(1, "st_columns", StTableType::System, StAccess::Public), + table_row(2, "st_sequence", StTableType::System, StAccess::Public), + table_row(3, "st_indexes", StTableType::System, StAccess::Public), + table_row(4, "st_constraints", StTableType::System, StAccess::Public), + table_row(5, "st_module", StTableType::System, StAccess::Public), ] ); let column_rows = datastore @@ -2251,42 +2319,42 @@ mod tests { assert_eq!( column_rows, vec![ - StColumnRow { table_id: 0, col_id: 0, col_name: "table_id".to_string(), col_type: AlgebraicType::U32, is_autoinc: true }, - StColumnRow { table_id: 0, col_id: 1, col_name: "table_name".to_string(), col_type: AlgebraicType::String, is_autoinc: false }, - StColumnRow { table_id: 0, col_id: 2, col_name: "table_type".to_string(), col_type: AlgebraicType::String, is_autoinc: false }, - StColumnRow { table_id: 0, col_id: 3, col_name: "table_access".to_string(), col_type: AlgebraicType::String, is_autoinc: false }, - - StColumnRow { table_id: 1, col_id: 0, col_name: "table_id".to_string(), col_type: AlgebraicType::U32, is_autoinc: false }, - StColumnRow { table_id: 1, col_id: 1, col_name: "col_id".to_string(), col_type: AlgebraicType::U32, is_autoinc: false }, - StColumnRow { table_id: 1, col_id: 2, col_name: "col_type".to_string(), col_type: AlgebraicType::array(AlgebraicType::U8), is_autoinc: false }, - StColumnRow { table_id: 1, col_id: 3, col_name: "col_name".to_string(), col_type: AlgebraicType::String, is_autoinc: false }, - StColumnRow { table_id: 1, col_id: 4, col_name: "is_autoinc".to_string(), col_type: AlgebraicType::Bool, is_autoinc: false }, - - StColumnRow { table_id: 2, col_id: 0, col_name: "sequence_id".to_string(), col_type: AlgebraicType::U32, is_autoinc: true }, - StColumnRow { table_id: 2, col_id: 1, col_name: "sequence_name".to_string(), col_type: AlgebraicType::String, is_autoinc: false }, - StColumnRow { table_id: 2, col_id: 2, col_name: "table_id".to_string(), col_type: AlgebraicType::U32, is_autoinc: false }, - StColumnRow { table_id: 2, col_id: 3, col_name: "col_id".to_string(), col_type: AlgebraicType::U32, is_autoinc: false }, - StColumnRow { table_id: 2, col_id: 4, col_name: "increment".to_string(), col_type: AlgebraicType::I128, is_autoinc: false }, - StColumnRow { table_id: 2, col_id: 5, col_name: "start".to_string(), col_type: AlgebraicType::I128, is_autoinc: false }, - StColumnRow { table_id: 2, col_id: 6, col_name: "min_value".to_string(), col_type: AlgebraicType::I128, is_autoinc: false }, - StColumnRow { table_id: 2, col_id: 7, col_name: "max_malue".to_string(), col_type: AlgebraicType::I128, is_autoinc: false }, - StColumnRow { table_id: 2, col_id: 8, col_name: "allocated".to_string(), col_type: AlgebraicType::I128, is_autoinc: false }, - - StColumnRow { table_id: 3, col_id: 0, col_name: "index_id".to_string(), col_type: AlgebraicType::U32, is_autoinc: true }, - StColumnRow { table_id: 3, col_id: 1, col_name: "table_id".to_string(), col_type: AlgebraicType::U32, is_autoinc: false }, - StColumnRow { table_id: 3, col_id: 2, col_name: "cols".to_string(), col_type: AlgebraicType::array(AlgebraicType::U32), is_autoinc: false }, - StColumnRow { table_id: 3, col_id: 3, col_name: "index_name".to_string(), col_type: AlgebraicType::String, is_autoinc: false }, - StColumnRow { table_id: 3, col_id: 4, col_name: "is_unique".to_string(), col_type: AlgebraicType::Bool, is_autoinc: false }, - - StColumnRow { table_id: 4, col_id: 0, col_name: "constraint_id".to_string(), col_type: AlgebraicType::U32, is_autoinc: true }, - StColumnRow { table_id: 4, col_id: 1, col_name: "constraint_name".to_string(), col_type: AlgebraicType::String, is_autoinc: false }, - StColumnRow { table_id: 4, col_id: 2, col_name: "kind".to_string(), col_type: AlgebraicType::U32, is_autoinc: false }, - StColumnRow { table_id: 4, col_id: 3, col_name: "table_id".to_string(), col_type: AlgebraicType::U32, is_autoinc: false }, - StColumnRow { table_id: 4, col_id: 4, col_name: "columns".to_string(), col_type: AlgebraicType::array(AlgebraicType::U32), is_autoinc: false }, - - StColumnRow { table_id: 5, col_id: 0, col_name: "program_hash".to_string(), col_type: AlgebraicType::array(AlgebraicType::U8), is_autoinc: false }, - StColumnRow { table_id: 5, col_id: 1, col_name: "kind".to_string(), col_type: AlgebraicType::U8, is_autoinc: false }, - StColumnRow { table_id: 5, col_id: 2, col_name: "epoch".to_string(), col_type: AlgebraicType::U128, is_autoinc: false }, + column_row(0, 0, "table_id", AlgebraicType::U32, true), + column_row(0, 1, "table_name", AlgebraicType::String, false), + column_row(0, 2, "table_type", AlgebraicType::String, false), + column_row(0, 3, "table_access", AlgebraicType::String, false), + + column_row(1, 0, "table_id", AlgebraicType::U32, false), + column_row(1, 1, "col_id", AlgebraicType::U32, false), + column_row(1, 2, "col_type", AlgebraicType::bytes(), false), + column_row(1, 3, "col_name", AlgebraicType::String, false), + column_row(1, 4, "is_autoinc", AlgebraicType::Bool, false), + + column_row(2, 0, "sequence_id", AlgebraicType::U32, true), + column_row(2, 1, "sequence_name", AlgebraicType::String, false), + column_row(2, 2, "table_id", AlgebraicType::U32, false), + column_row(2, 3, "col_id", AlgebraicType::U32, false), + column_row(2, 4, "increment", AlgebraicType::I128, false), + column_row(2, 5, "start", AlgebraicType::I128, false), + column_row(2, 6, "min_value", AlgebraicType::I128, false), + column_row(2, 7, "max_value", AlgebraicType::I128, false), + column_row(2, 8, "allocated", AlgebraicType::I128, false), + + column_row(3, 0, "index_id", AlgebraicType::U32, true), + column_row(3, 1, "table_id", AlgebraicType::U32, false), + column_row(3, 2, "cols", AlgebraicType::array(AlgebraicType::U32), false), + column_row(3, 3, "index_name", AlgebraicType::String, false), + column_row(3, 4, "is_unique", AlgebraicType::Bool, false), + + column_row(4, 0, "constraint_id", AlgebraicType::U32, true), + column_row(4, 1, "constraint_name", AlgebraicType::String, false), + column_row(4, 2, "kind", AlgebraicType::U32, false), + column_row(4, 3, "table_id", AlgebraicType::U32, false), + column_row(4, 4, "columns", AlgebraicType::array(AlgebraicType::U32), false), + + column_row(5, 0, "program_hash", AlgebraicType::array(AlgebraicType::U8), false), + column_row(5, 1, "kind", AlgebraicType::U8, false), + column_row(5, 2, "epoch", AlgebraicType::U128, false), ] ); let index_rows = datastore @@ -2298,12 +2366,12 @@ mod tests { assert_eq!( index_rows, vec![ - StIndexRow { index_id: 0, table_id: 0, cols: NonEmpty::new(0), index_name: "table_id_idx".to_string(), is_unique: true }, - StIndexRow { index_id: 1, table_id: 3, cols: NonEmpty::new(0), index_name: "index_id_idx".to_string(), is_unique: true }, - StIndexRow { index_id: 2, table_id: 2, cols: NonEmpty::new(0), index_name: "sequences_id_idx".to_string(), is_unique: true }, - StIndexRow { index_id: 3, table_id: 0, cols: NonEmpty::new(1), index_name: "table_name_idx".to_string(), is_unique: true }, - StIndexRow { index_id: 4, table_id: 4, cols: NonEmpty::new(0), index_name: "constraint_id_idx".to_string(), is_unique: true }, - StIndexRow { index_id: 5, table_id: 1, cols: NonEmpty::new(0), index_name: "idx_ct_columns_table_id".to_string(), is_unique: false } + index_row(0, 0, 0, "table_id_idx", true), + index_row(1, 3, 0, "index_id_idx", true), + index_row(2, 2, 0, "sequences_id_idx", true), + index_row(3, 0, 1, "table_name_idx", true), + index_row(4, 4, 0, "constraint_id_idx", true), + index_row(5, 1, 0, "idx_ct_columns_table_id", false), ] ); let sequence_rows = datastore @@ -2340,10 +2408,7 @@ mod tests { #[test] fn test_create_table_pre_commit() -> ResultTest<()> { - let datastore = get_datastore()?; - let mut tx = datastore.begin_mut_tx(); - let schema = basic_table_schema(); - let table_id = datastore.create_table_mut_tx(&mut tx, schema)?; + let (datastore, tx, table_id) = setup_table()?; let table_rows = datastore .iter_by_col_eq_mut_tx(&tx, ST_TABLES_ID, ColId(0), table_id.into())? .map(|x| StTableRow::try_from(x.view()).unwrap().to_owned()) @@ -2353,7 +2418,7 @@ mod tests { assert_eq!( table_rows, vec![ - StTableRow { table_id: 6, table_name: "Foo".to_string(), table_type: StTableType::User, table_access: StAccess::Public } + table_row(6, "Foo", StTableType::User, StAccess::Public) ] ); let column_rows = datastore @@ -2365,9 +2430,9 @@ mod tests { assert_eq!( column_rows, vec![ - StColumnRow { table_id: 6, col_id: 0, col_name: "id".to_string(), col_type: AlgebraicType::U32, is_autoinc: true }, - StColumnRow { table_id: 6, col_id: 1, col_name: "name".to_string(), col_type: AlgebraicType::String, is_autoinc: false }, - StColumnRow { table_id: 6, col_id: 2, col_name: "age".to_string(), col_type: AlgebraicType::U32, is_autoinc: false }, + column_row(6, 0, "id", AlgebraicType::U32, true), + column_row(6, 1, "name", AlgebraicType::String, false), + column_row(6, 2, "age", AlgebraicType::U32, false), ] ); Ok(()) @@ -2375,10 +2440,7 @@ mod tests { #[test] fn test_create_table_post_commit() -> ResultTest<()> { - let datastore = get_datastore()?; - let mut tx = datastore.begin_mut_tx(); - let schema = basic_table_schema(); - let table_id = datastore.create_table_mut_tx(&mut tx, schema)?; + let (datastore, tx, table_id) = setup_table()?; datastore.commit_mut_tx(tx)?; let tx = datastore.begin_mut_tx(); let table_rows = datastore @@ -2390,7 +2452,7 @@ mod tests { assert_eq!( table_rows, vec![ - StTableRow { table_id: 6, table_name: "Foo".to_string() , table_type: StTableType::User, table_access: StAccess::Public} + table_row(6, "Foo", StTableType::User, StAccess::Public) ] ); let column_rows = datastore @@ -2402,9 +2464,9 @@ mod tests { assert_eq!( column_rows, vec![ - StColumnRow { table_id: 6, col_id: 0, col_name: "id".to_string(), col_type: AlgebraicType::U32, is_autoinc: true }, - StColumnRow { table_id: 6, col_id: 1, col_name: "name".to_string(), col_type: AlgebraicType::String, is_autoinc: false }, - StColumnRow { table_id: 6, col_id: 2, col_name: "age".to_string(), col_type: AlgebraicType::U32, is_autoinc: false }, + column_row(6, 0, "id", AlgebraicType::U32, true), + column_row(6, 1, "name", AlgebraicType::String, false), + column_row(6, 2, "age", AlgebraicType::U32, false), ] ); Ok(()) @@ -2412,10 +2474,7 @@ mod tests { #[test] fn test_create_table_post_rollback() -> ResultTest<()> { - let datastore = get_datastore()?; - let mut tx = datastore.begin_mut_tx(); - let schema = basic_table_schema(); - let table_id = datastore.create_table_mut_tx(&mut tx, schema)?; + let (datastore, tx, table_id) = setup_table()?; datastore.rollback_mut_tx(tx); let tx = datastore.begin_mut_tx(); let table_rows = datastore @@ -2435,23 +2494,20 @@ mod tests { #[test] fn test_schema_for_table_pre_commit() -> ResultTest<()> { - let datastore = get_datastore()?; - let mut tx = datastore.begin_mut_tx(); - let schema = basic_table_schema(); - let table_id = datastore.create_table_mut_tx(&mut tx, schema)?; + let (datastore, tx, table_id) = setup_table()?; let schema = datastore.schema_for_table_mut_tx(&tx, table_id)?; #[rustfmt::skip] assert_eq!(schema, TableSchema { table_id: table_id.0, table_name: "Foo".into(), columns: vec![ - ColumnSchema { table_id: 6, col_id: 0, col_name: "id".to_string(), col_type: AlgebraicType::U32, is_autoinc: true }, - ColumnSchema { table_id: 6, col_id: 1, col_name: "name".to_string(), col_type: AlgebraicType::String, is_autoinc: false }, - ColumnSchema { table_id: 6, col_id: 2, col_name: "age".to_string(), col_type: AlgebraicType::U32, is_autoinc: false }, + column_schema(6, 0, "id", AlgebraicType::U32, true), + column_schema(6, 1, "name", AlgebraicType::String, false), + column_schema(6, 2, "age", AlgebraicType::U32, false), ], indexes: vec![ - IndexSchema { index_id: 6, table_id: 6, cols: NonEmpty::new(0), index_name: "id_idx".to_string(), is_unique: true }, - IndexSchema { index_id: 7, table_id: 6, cols: NonEmpty::new(1), index_name: "name_idx".to_string(), is_unique: true }, + index_schema(6, 6, 0, "id_idx", true), + index_schema(7, 6, 1, "name_idx", true), ], constraints: vec![], table_type: StTableType::User, @@ -2462,10 +2518,7 @@ mod tests { #[test] fn test_schema_for_table_post_commit() -> ResultTest<()> { - let datastore = get_datastore()?; - let mut tx = datastore.begin_mut_tx(); - let schema = basic_table_schema(); - let table_id = datastore.create_table_mut_tx(&mut tx, schema)?; + let (datastore, tx, table_id) = setup_table()?; datastore.commit_mut_tx(tx)?; let tx = datastore.begin_mut_tx(); let schema = datastore.schema_for_table_mut_tx(&tx, table_id)?; @@ -2474,13 +2527,13 @@ mod tests { table_id: table_id.0, table_name: "Foo".into(), columns: vec![ - ColumnSchema { table_id: 6, col_id: 0, col_name: "id".to_string(), col_type: AlgebraicType::U32, is_autoinc: true }, - ColumnSchema { table_id: 6, col_id: 1, col_name: "name".to_string(), col_type: AlgebraicType::String, is_autoinc: false }, - ColumnSchema { table_id: 6, col_id: 2, col_name: "age".to_string(), col_type: AlgebraicType::U32, is_autoinc: false }, + column_schema(6, 0, "id", AlgebraicType::U32, true), + column_schema(6, 1, "name", AlgebraicType::String, false), + column_schema(6, 2, "age", AlgebraicType::U32, false), ], indexes: vec![ - IndexSchema { index_id: 6, table_id: 6, cols: NonEmpty::new(0), index_name: "id_idx".to_string(), is_unique: true }, - IndexSchema { index_id: 7, table_id: 6, cols: NonEmpty::new(1), index_name: "name_idx".to_string(), is_unique: true }, + index_schema(6, 6, 0, "id_idx", true), + index_schema(7, 6, 1, "name_idx", true), ], constraints: vec![], table_type: StTableType::User, @@ -2491,10 +2544,7 @@ mod tests { #[test] fn test_schema_for_table_alter_indexes() -> ResultTest<()> { - let datastore = get_datastore()?; - let mut tx = datastore.begin_mut_tx(); - let schema = basic_table_schema(); - let table_id = datastore.create_table_mut_tx(&mut tx, schema)?; + let (datastore, tx, table_id) = setup_table()?; datastore.commit_mut_tx(tx)?; let mut tx = datastore.begin_mut_tx(); @@ -2525,13 +2575,7 @@ mod tests { }, )?; - let expected_indexes = vec![IndexSchema { - index_id: 8, - table_id: 6, - cols: NonEmpty::new(0), - index_name: "id_idx".into(), - is_unique: true, - }]; + let expected_indexes = vec![index_schema(8, 6, 0, "id_idx", true)]; assert_eq!( datastore.schema_for_table_mut_tx(&tx, table_id)?.indexes, expected_indexes, @@ -2554,10 +2598,7 @@ mod tests { #[test] fn test_schema_for_table_rollback() -> ResultTest<()> { - let datastore = get_datastore()?; - let mut tx = datastore.begin_mut_tx(); - let schema = basic_table_schema(); - let table_id = datastore.create_table_mut_tx(&mut tx, schema)?; + let (datastore, tx, table_id) = setup_table()?; datastore.rollback_mut_tx(tx); let tx = datastore.begin_mut_tx(); let schema = datastore.schema_for_table_mut_tx(&tx, table_id); @@ -2567,10 +2608,7 @@ mod tests { #[test] fn test_insert_pre_commit() -> ResultTest<()> { - let datastore = get_datastore()?; - let mut tx = datastore.begin_mut_tx(); - let schema = basic_table_schema(); - let table_id = datastore.create_table_mut_tx(&mut tx, schema)?; + let (datastore, mut tx, table_id) = setup_table()?; let row = u32_str_u32(0, "Foo", 18); // 0 will be ignored. datastore.insert_mut_tx(&mut tx, table_id, row)?; let rows = datastore @@ -2584,10 +2622,7 @@ mod tests { #[test] fn test_insert_wrong_schema_pre_commit() -> ResultTest<()> { - let datastore = get_datastore()?; - let mut tx = datastore.begin_mut_tx(); - let schema = basic_table_schema(); - let table_id = datastore.create_table_mut_tx(&mut tx, schema)?; + let (datastore, mut tx, table_id) = setup_table()?; let row = ProductValue::from_iter(vec![ AlgebraicValue::U32(0), // 0 will be ignored. AlgebraicValue::String("Foo".to_string()), @@ -2604,10 +2639,7 @@ mod tests { #[test] fn test_insert_post_commit() -> ResultTest<()> { - let datastore = get_datastore()?; - let mut tx = datastore.begin_mut_tx(); - let schema = basic_table_schema(); - let table_id = datastore.create_table_mut_tx(&mut tx, schema)?; + let (datastore, mut tx, table_id) = setup_table()?; // 0 will be ignored. datastore.insert_mut_tx(&mut tx, table_id, u32_str_u32(0, "Foo", 18))?; datastore.commit_mut_tx(tx)?; @@ -2623,10 +2655,7 @@ mod tests { #[test] fn test_insert_post_rollback() -> ResultTest<()> { - let datastore = get_datastore()?; - let mut tx = datastore.begin_mut_tx(); - let schema = basic_table_schema(); - let table_id = datastore.create_table_mut_tx(&mut tx, schema)?; + let (datastore, tx, table_id) = setup_table()?; let row = u32_str_u32(15, "Foo", 18); // 15 is ignored. datastore.commit_mut_tx(tx)?; let mut tx = datastore.begin_mut_tx(); @@ -2644,10 +2673,7 @@ mod tests { #[test] fn test_insert_commit_delete_insert() -> ResultTest<()> { - let datastore = get_datastore()?; - let mut tx = datastore.begin_mut_tx(); - let schema = basic_table_schema(); - let table_id = datastore.create_table_mut_tx(&mut tx, schema)?; + let (datastore, mut tx, table_id) = setup_table()?; let row = u32_str_u32(0, "Foo", 18); // 0 will be ignored. datastore.insert_mut_tx(&mut tx, table_id, row)?; datastore.commit_mut_tx(tx)?; @@ -2673,10 +2699,7 @@ mod tests { #[test] fn test_insert_delete_insert_delete_insert() -> ResultTest<()> { - let datastore = get_datastore()?; - let mut tx = datastore.begin_mut_tx(); - let schema = basic_table_schema(); - let table_id = datastore.create_table_mut_tx(&mut tx, schema)?; + let (datastore, mut tx, table_id) = setup_table()?; let row = u32_str_u32(0, "Foo", 18); // 0 will be ignored. datastore.insert_mut_tx(&mut tx, table_id, row)?; for _ in 0..2 { @@ -2701,10 +2724,7 @@ mod tests { #[test] fn test_unique_constraint_pre_commit() -> ResultTest<()> { - let datastore = get_datastore()?; - let mut tx = datastore.begin_mut_tx(); - let schema = basic_table_schema(); - let table_id = datastore.create_table_mut_tx(&mut tx, schema)?; + let (datastore, mut tx, table_id) = setup_table()?; let row = u32_str_u32(0, "Foo", 18); // 0 will be ignored. datastore.insert_mut_tx(&mut tx, table_id, row.clone())?; let result = datastore.insert_mut_tx(&mut tx, table_id, row); @@ -2728,10 +2748,7 @@ mod tests { #[test] fn test_unique_constraint_post_commit() -> ResultTest<()> { - let datastore = get_datastore()?; - let mut tx = datastore.begin_mut_tx(); - let schema = basic_table_schema(); - let table_id = datastore.create_table_mut_tx(&mut tx, schema)?; + let (datastore, mut tx, table_id) = setup_table()?; let row = u32_str_u32(0, "Foo", 18); // 0 will be ignored. datastore.insert_mut_tx(&mut tx, table_id, row.clone())?; datastore.commit_mut_tx(tx)?; @@ -2757,10 +2774,7 @@ mod tests { #[test] fn test_unique_constraint_post_rollback() -> ResultTest<()> { - let datastore = get_datastore()?; - let mut tx = datastore.begin_mut_tx(); - let schema = basic_table_schema(); - let table_id = datastore.create_table_mut_tx(&mut tx, schema)?; + let (datastore, tx, table_id) = setup_table()?; datastore.commit_mut_tx(tx)?; let mut tx = datastore.begin_mut_tx(); let row = u32_str_u32(0, "Foo", 18); // 0 will be ignored. @@ -2779,10 +2793,7 @@ mod tests { #[test] fn test_create_index_pre_commit() -> ResultTest<()> { - let datastore = get_datastore()?; - let mut tx = datastore.begin_mut_tx(); - let schema = basic_table_schema(); - let table_id = datastore.create_table_mut_tx(&mut tx, schema)?; + let (datastore, tx, table_id) = setup_table()?; datastore.commit_mut_tx(tx)?; let mut tx = datastore.begin_mut_tx(); let row = u32_str_u32(0, "Foo", 18); // 0 will be ignored. @@ -2803,21 +2814,17 @@ mod tests { .collect::>(); #[rustfmt::skip] assert_eq!(index_rows, vec![ - StIndexRow { index_id: 0, table_id: 0, cols: NonEmpty::new(0), index_name: "table_id_idx".to_string(), is_unique: true }, - StIndexRow { index_id: 1, table_id: 3, cols: NonEmpty::new(0), index_name: "index_id_idx".to_string(), is_unique: true }, - StIndexRow { index_id: 2, table_id: 2, cols: NonEmpty::new(0), index_name: "sequences_id_idx".to_string(), is_unique: true }, - StIndexRow { index_id: 3, table_id: 0, cols: NonEmpty::new(1), index_name: "table_name_idx".to_string(), is_unique: true }, - StIndexRow { index_id: 4, table_id: 4, cols: NonEmpty::new(0), index_name: "constraint_id_idx".to_string(), is_unique: true }, - StIndexRow { index_id: 5, table_id: 1, cols: NonEmpty::new(0), index_name: "idx_ct_columns_table_id".to_string(), is_unique: false }, - StIndexRow { index_id: 6, table_id: 6, cols: NonEmpty::new(0), index_name: "id_idx".to_string(), is_unique: true }, - StIndexRow { index_id: 7, table_id: 6, cols: NonEmpty::new(1), index_name: "name_idx".to_string(), is_unique: true }, - StIndexRow { index_id: 8, table_id: 6, cols: NonEmpty::new(2), index_name: "age_idx".to_string(), is_unique: true }, - ]); - let row = ProductValue::from_iter(vec![ - AlgebraicValue::U32(0), // 0 will be ignored. - AlgebraicValue::String("Bar".to_string()), - AlgebraicValue::U32(18), + index_row(0, 0, 0, "table_id_idx", true), + index_row(1, 3, 0, "index_id_idx", true), + index_row(2, 2, 0, "sequences_id_idx", true), + index_row(3, 0, 1, "table_name_idx", true), + index_row(4, 4, 0, "constraint_id_idx", true), + index_row(5, 1, 0, "idx_ct_columns_table_id", false), + index_row(6, 6, 0, "id_idx", true), + index_row(7, 6, 1, "name_idx", true), + index_row(8, 6, 2, "age_idx", true), ]); + let row = u32_str_u32(0, "Bar", 18); // 0 will be ignored. let result = datastore.insert_mut_tx(&mut tx, table_id, row); match result { Err(DBError::Index(IndexError::UniqueConstraintViolation { @@ -2839,10 +2846,7 @@ mod tests { #[test] fn test_create_index_post_commit() -> ResultTest<()> { - let datastore = get_datastore()?; - let mut tx = datastore.begin_mut_tx(); - let schema = basic_table_schema(); - let table_id = datastore.create_table_mut_tx(&mut tx, schema)?; + let (datastore, mut tx, table_id) = setup_table()?; let row = u32_str_u32(0, "Foo", 18); // 0 will be ignored. datastore.insert_mut_tx(&mut tx, table_id, row)?; datastore.commit_mut_tx(tx)?; @@ -2863,15 +2867,15 @@ mod tests { .collect::>(); #[rustfmt::skip] assert_eq!(index_rows, vec![ - StIndexRow { index_id: 0, table_id: 0, cols: NonEmpty::new(0), index_name: "table_id_idx".to_string(), is_unique: true }, - StIndexRow { index_id: 1, table_id: 3, cols: NonEmpty::new(0), index_name: "index_id_idx".to_string(), is_unique: true }, - StIndexRow { index_id: 2, table_id: 2, cols: NonEmpty::new(0), index_name: "sequences_id_idx".to_string(), is_unique: true }, - StIndexRow { index_id: 3, table_id: 0, cols: NonEmpty::new(1), index_name: "table_name_idx".to_string(), is_unique: true }, - StIndexRow { index_id: 4, table_id: 4, cols: NonEmpty::new(0), index_name: "constraint_id_idx".to_string(), is_unique: true }, - StIndexRow { index_id: 5, table_id: 1, cols: NonEmpty::new(0), index_name: "idx_ct_columns_table_id".to_string(), is_unique: false }, - StIndexRow { index_id: 6, table_id: 6, cols: NonEmpty::new(0), index_name: "id_idx".to_string(), is_unique: true }, - StIndexRow { index_id: 7, table_id: 6, cols: NonEmpty::new(1), index_name: "name_idx".to_string(), is_unique: true }, - StIndexRow { index_id: 8, table_id: 6, cols: NonEmpty::new(2), index_name: "age_idx".to_string(), is_unique: true }, + index_row(0, 0, 0, "table_id_idx", true), + index_row(1, 3, 0, "index_id_idx", true), + index_row(2, 2, 0, "sequences_id_idx", true), + index_row(3, 0, 1, "table_name_idx", true), + index_row(4, 4, 0, "constraint_id_idx", true), + index_row(5, 1, 0, "idx_ct_columns_table_id", false), + index_row(6, 6, 0, "id_idx", true), + index_row(7, 6, 1, "name_idx", true), + index_row(8, 6, 2, "age_idx", true), ]); let row = u32_str_u32(0, "Bar", 18); // 0 will be ignored. @@ -2896,10 +2900,7 @@ mod tests { #[test] fn test_create_index_post_rollback() -> ResultTest<()> { - let datastore = get_datastore()?; - let mut tx = datastore.begin_mut_tx(); - let schema = basic_table_schema(); - let table_id = datastore.create_table_mut_tx(&mut tx, schema)?; + let (datastore, mut tx, table_id) = setup_table()?; let row = u32_str_u32(0, "Foo", 18); // 0 will be ignored. datastore.insert_mut_tx(&mut tx, table_id, row)?; datastore.commit_mut_tx(tx)?; @@ -2920,20 +2921,16 @@ mod tests { .collect::>(); #[rustfmt::skip] assert_eq!(index_rows, vec![ - StIndexRow { index_id: 0, table_id: 0, cols: NonEmpty::new(0), index_name: "table_id_idx".to_string(), is_unique: true }, - StIndexRow { index_id: 1, table_id: 3, cols: NonEmpty::new(0), index_name: "index_id_idx".to_string(), is_unique: true }, - StIndexRow { index_id: 2, table_id: 2, cols: NonEmpty::new(0), index_name: "sequences_id_idx".to_string(), is_unique: true }, - StIndexRow { index_id: 3, table_id: 0, cols: NonEmpty::new(1), index_name: "table_name_idx".to_string(), is_unique: true }, - StIndexRow { index_id: 4, table_id: 4, cols: NonEmpty::new(0), index_name: "constraint_id_idx".to_string(), is_unique: true }, - StIndexRow { index_id: 5, table_id: 1, cols: NonEmpty::new(0), index_name: "idx_ct_columns_table_id".to_string(), is_unique: false }, - StIndexRow { index_id: 6, table_id: 6, cols: NonEmpty::new(0), index_name: "id_idx".to_string(), is_unique: true }, - StIndexRow { index_id: 7, table_id: 6, cols: NonEmpty::new(1), index_name: "name_idx".to_string(), is_unique: true }, - ]); - let row = ProductValue::from_iter(vec![ - AlgebraicValue::U32(0), // 0 will be ignored. - AlgebraicValue::String("Bar".to_string()), - AlgebraicValue::U32(18), + index_row(0, 0, 0, "table_id_idx", true), + index_row(1, 3, 0, "index_id_idx", true), + index_row(2, 2, 0, "sequences_id_idx", true), + index_row(3, 0, 1, "table_name_idx", true), + index_row(4, 4, 0, "constraint_id_idx", true), + index_row(5, 1, 0, "idx_ct_columns_table_id", false), + index_row(6, 6, 0, "id_idx", true), + index_row(7, 6, 1, "name_idx", true), ]); + let row = u32_str_u32(0, "Bar", 18); // 0 will be ignored. datastore.insert_mut_tx(&mut tx, table_id, row)?; let rows = datastore .iter_mut_tx(&tx, table_id)? @@ -2949,12 +2946,8 @@ mod tests { #[test] fn test_update_reinsert() -> ResultTest<()> { - let datastore = get_datastore()?; - + let (datastore, mut tx, table_id) = setup_table()?; // Insert a row and commit the tx. - let mut tx = datastore.begin_mut_tx(); - let schema = basic_table_schema(); - let table_id = datastore.create_table_mut_tx(&mut tx, schema)?; let row = u32_str_u32(0, "Foo", 18); // 0 will be ignored. // Because of autoinc columns, we will get a slightly different // value than the one we inserted. diff --git a/crates/core/src/db/datastore/system_tables.rs b/crates/core/src/db/datastore/system_tables.rs index 197510e250..f992089e06 100644 --- a/crates/core/src/db/datastore/system_tables.rs +++ b/crates/core/src/db/datastore/system_tables.rs @@ -484,7 +484,7 @@ pub(crate) fn st_sequences_schema() -> TableSchema { ColumnSchema { table_id: ST_SEQUENCES_ID.0, col_id: 7, - col_name: "max_malue".into(), + col_name: "max_value".into(), col_type: AlgebraicType::I128, is_autoinc: false, },