Skip to content

Commit 93ef187

Browse files
committed
harden tests for drop/create table transactionality
1 parent 2c68e1f commit 93ef187

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

crates/core/src/db/datastore/locking_tx_datastore/datastore.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,7 @@ fn metadata_from_row(row: RowRef<'_>) -> Result<Metadata> {
10381038
#[cfg(test)]
10391039
mod tests {
10401040
use super::*;
1041+
use crate::db::datastore::locking_tx_datastore::tx_state::PendingSchemaChange;
10411042
use crate::db::datastore::system_tables::{
10421043
system_tables, StColumnRow, StConstraintData, StConstraintFields, StConstraintRow, StIndexAlgorithm,
10431044
StIndexFields, StIndexRow, StRowLevelSecurityFields, StScheduledFields, StSequenceFields, StSequenceRow,
@@ -2902,11 +2903,25 @@ mod tests {
29022903

29032904
// Create a transaction and drop the table and roll back.
29042905
let mut tx = begin_mut_tx(&datastore);
2906+
assert_eq!(&*tx.tx_state.pending_schema_changes, []);
29052907
assert!(datastore.drop_table_mut_tx(&mut tx, table_id).is_ok());
2908+
assert_matches!(
2909+
&*tx.tx_state.pending_schema_changes,
2910+
[
2911+
PendingSchemaChange::IndexRemoved(..),
2912+
PendingSchemaChange::IndexRemoved(..),
2913+
PendingSchemaChange::SequenceRemoved(..),
2914+
PendingSchemaChange::ConstraintRemoved(..),
2915+
PendingSchemaChange::ConstraintRemoved(..),
2916+
PendingSchemaChange::TableRemoved(removed_table_id, _)
2917+
]
2918+
if *removed_table_id == table_id
2919+
);
29062920
datastore.rollback_mut_tx(tx);
29072921

29082922
// Ensure the table still exists in the next transaction.
29092923
let tx = begin_mut_tx(&datastore);
2924+
assert_eq!(&*tx.tx_state.pending_schema_changes, []);
29102925
assert!(
29112926
datastore.table_id_exists_mut_tx(&tx, &table_id),
29122927
"Table should still exist",
@@ -2919,10 +2934,23 @@ mod tests {
29192934
fn test_create_table_is_transactional() -> ResultTest<()> {
29202935
// Create a table in a failed transaction.
29212936
let (datastore, tx, table_id) = setup_table()?;
2937+
assert_matches!(
2938+
&*tx.tx_state.pending_schema_changes,
2939+
[
2940+
PendingSchemaChange::TableAdded(added_table_id),
2941+
PendingSchemaChange::IndexAdded(..),
2942+
PendingSchemaChange::IndexAdded(..),
2943+
PendingSchemaChange::ConstraintAdded(..),
2944+
PendingSchemaChange::ConstraintAdded(..),
2945+
PendingSchemaChange::SequenceAdded(..),
2946+
]
2947+
if *added_table_id == table_id
2948+
);
29222949
datastore.rollback_mut_tx(tx);
29232950

29242951
// Nothing should have happened.
29252952
let tx = begin_mut_tx(&datastore);
2953+
assert_eq!(&*tx.tx_state.pending_schema_changes, []);
29262954
assert!(
29272955
!datastore.table_id_exists_mut_tx(&tx, &table_id),
29282956
"Table should not exist"

crates/core/src/db/datastore/locking_tx_datastore/sequence.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use spacetimedb_primitives::SequenceId;
33
use spacetimedb_schema::schema::SequenceSchema;
44
use spacetimedb_table::MemoryUsage;
55

6+
#[derive(Debug, PartialEq)]
67
pub(super) struct Sequence {
78
schema: SequenceSchema,
89
pub(super) value: i128,

crates/core/src/db/datastore/locking_tx_datastore/tx_state.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ pub(super) struct TxState {
8686
/// The places that do need to care about changes are those that make them, and merge/rollback.
8787
/// Architecting this way should benefit performance both during transactions and merge.
8888
/// On rollback, it should be fairly cheap to e.g., just re-add an index or drop it on the floor.
89+
#[derive(Debug, PartialEq)]
8990
pub(super) enum PendingSchemaChange {
9091
/// The [`TableIndex`] / [`IndexSchema`] with `IndexId`
9192
/// was removed from the table with [`TableId`].

0 commit comments

Comments
 (0)