11use super :: relational_db:: RelationalDB ;
22use crate :: database_logger:: SystemLogger ;
33use crate :: sql:: parser:: RowLevelExpr ;
4+ use crate :: subscription:: module_subscription_actor:: ModuleSubscriptions ;
45use spacetimedb_data_structures:: map:: HashMap ;
56use spacetimedb_datastore:: locking_tx_datastore:: MutTxId ;
67use spacetimedb_lib:: db:: auth:: StTableType ;
@@ -35,6 +36,7 @@ impl UpdateLogger for SystemLogger {
3536// drop_* become transactional.
3637pub fn update_database (
3738 stdb : & RelationalDB ,
39+ subscriptions : & ModuleSubscriptions ,
3840 tx : & mut MutTxId ,
3941 auth_ctx : AuthCtx ,
4042 plan : MigratePlan ,
@@ -57,7 +59,9 @@ pub fn update_database(
5759
5860 match plan {
5961 MigratePlan :: Manual ( plan) => manual_migrate_database ( stdb, tx, plan, logger, existing_tables) ,
60- MigratePlan :: Auto ( plan) => auto_migrate_database ( stdb, tx, auth_ctx, plan, logger, existing_tables) ,
62+ MigratePlan :: Auto ( plan) => {
63+ auto_migrate_database ( stdb, subscriptions, tx, auth_ctx, plan, logger, existing_tables)
64+ }
6165 }
6266}
6367
@@ -83,6 +87,7 @@ macro_rules! log {
8387/// Automatically migrate a database.
8488fn auto_migrate_database (
8589 stdb : & RelationalDB ,
90+ subscriptions : & ModuleSubscriptions ,
8691 tx : & mut MutTxId ,
8792 auth_ctx : AuthCtx ,
8893 plan : AutoMigratePlan ,
@@ -264,7 +269,12 @@ fn auto_migrate_database(
264269 . collect ( ) ;
265270 stdb. add_columns_to_table ( tx, table_id, column_schemas, default_values) ?;
266271 }
267- _ => anyhow:: bail!( "migration step not implemented: {step:?}" ) ,
272+ spacetimedb_schema:: auto_migrate:: AutoMigrateStep :: DisconnectAllUsers => {
273+ // Disconnect all clients from subscriptions.
274+ // Any dangling clients will be handled by new modules launch logic
275+ // which invokes `ModuleHost::call_identity_disconnected`.
276+ subscriptions. remove_all_subscribers ( ) ;
277+ }
268278 }
269279 }
270280
@@ -349,7 +359,14 @@ mod test {
349359 // Try to update the db.
350360 let mut tx = begin_mut_tx ( & stdb) ;
351361 let plan = ponder_migrate ( & old, & new) ?;
352- update_database ( & stdb, & mut tx, auth_ctx, plan, & TestLogger ) ?;
362+ update_database (
363+ & stdb,
364+ & ModuleSubscriptions :: for_test_new_runtime ( Arc :: new ( stdb. db . clone ( ) ) ) . 0 ,
365+ & mut tx,
366+ auth_ctx,
367+ plan,
368+ & TestLogger ,
369+ ) ?;
353370
354371 // Expect the schema change.
355372 let idx_b_id = stdb
0 commit comments