1
1
use super :: relational_db:: RelationalDB ;
2
2
use crate :: database_logger:: SystemLogger ;
3
3
use crate :: sql:: parser:: RowLevelExpr ;
4
- use crate :: subscription:: module_subscription_actor:: ModuleSubscriptions ;
5
4
use spacetimedb_data_structures:: map:: HashMap ;
6
5
use spacetimedb_datastore:: locking_tx_datastore:: MutTxId ;
7
6
use spacetimedb_lib:: db:: auth:: StTableType ;
@@ -24,6 +23,13 @@ impl UpdateLogger for SystemLogger {
24
23
}
25
24
}
26
25
26
+ /// The result of a database update.
27
+ /// Indicates whether clients should be disconnected when the update is complete.
28
+ pub enum UpdateResult {
29
+ Success ,
30
+ RequiresClientDisconnect ,
31
+ }
32
+
27
33
/// Update the database according to the migration plan.
28
34
///
29
35
/// The update is performed within the transactional context `tx`.
@@ -36,12 +42,11 @@ impl UpdateLogger for SystemLogger {
36
42
// drop_* become transactional.
37
43
pub fn update_database (
38
44
stdb : & RelationalDB ,
39
- subscriptions : & ModuleSubscriptions ,
40
45
tx : & mut MutTxId ,
41
46
auth_ctx : AuthCtx ,
42
47
plan : MigratePlan ,
43
48
logger : & dyn UpdateLogger ,
44
- ) -> anyhow:: Result < ( ) > {
49
+ ) -> anyhow:: Result < UpdateResult > {
45
50
let existing_tables = stdb. get_all_tables_mut ( tx) ?;
46
51
47
52
// TODO: consider using `ErrorStream` here.
@@ -59,9 +64,7 @@ pub fn update_database(
59
64
60
65
match plan {
61
66
MigratePlan :: Manual ( plan) => manual_migrate_database ( stdb, tx, plan, logger, existing_tables) ,
62
- MigratePlan :: Auto ( plan) => {
63
- auto_migrate_database ( stdb, subscriptions, tx, auth_ctx, plan, logger, existing_tables)
64
- }
67
+ MigratePlan :: Auto ( plan) => auto_migrate_database ( stdb, tx, auth_ctx, plan, logger, existing_tables) ,
65
68
}
66
69
}
67
70
@@ -72,7 +75,7 @@ fn manual_migrate_database(
72
75
_plan : ManualMigratePlan ,
73
76
_logger : & dyn UpdateLogger ,
74
77
_existing_tables : Vec < Arc < TableSchema > > ,
75
- ) -> anyhow:: Result < ( ) > {
78
+ ) -> anyhow:: Result < UpdateResult > {
76
79
unimplemented ! ( "Manual database migrations are not yet implemented" )
77
80
}
78
81
@@ -87,13 +90,12 @@ macro_rules! log {
87
90
/// Automatically migrate a database.
88
91
fn auto_migrate_database (
89
92
stdb : & RelationalDB ,
90
- subscriptions : & ModuleSubscriptions ,
91
93
tx : & mut MutTxId ,
92
94
auth_ctx : AuthCtx ,
93
95
plan : AutoMigratePlan ,
94
96
logger : & dyn UpdateLogger ,
95
97
existing_tables : Vec < Arc < TableSchema > > ,
96
- ) -> anyhow:: Result < ( ) > {
98
+ ) -> anyhow:: Result < UpdateResult > {
97
99
// We have already checked in `migrate_database` that `existing_tables` are compatible with the `old` definition in `plan`.
98
100
// So we can look up tables in there using unwrap.
99
101
@@ -132,6 +134,7 @@ fn auto_migrate_database(
132
134
}
133
135
134
136
log:: info!( "Running database update steps: {}" , stdb. database_identity( ) ) ;
137
+ let mut res = UpdateResult :: Success ;
135
138
136
139
for step in plan. steps {
137
140
match step {
@@ -270,16 +273,16 @@ fn auto_migrate_database(
270
273
stdb. add_columns_to_table ( tx, table_id, column_schemas, default_values) ?;
271
274
}
272
275
spacetimedb_schema:: auto_migrate:: AutoMigrateStep :: DisconnectAllUsers => {
273
- // Disconnect all clients from subscriptions.
274
- // Any dangling clients will be handled during the launch of module hosts ,
275
- // which invokes `ModuleHost::call_identity_disconnected`.
276
- subscriptions . remove_all_subscribers ( ) ;
276
+ log ! ( logger , "Disconnecting all users" ) ;
277
+ // It does disconnect clients right away ,
278
+ // but send response indicated that caller should drop clients
279
+ res = UpdateResult :: RequiresClientDisconnect ;
277
280
}
278
281
}
279
282
}
280
283
281
284
log:: info!( "Database update complete" ) ;
282
- Ok ( ( ) )
285
+ Ok ( res )
283
286
}
284
287
285
288
#[ cfg( test) ]
@@ -359,14 +362,7 @@ mod test {
359
362
// Try to update the db.
360
363
let mut tx = begin_mut_tx ( & stdb) ;
361
364
let plan = ponder_migrate ( & old, & new) ?;
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
- ) ?;
365
+ update_database ( & stdb, & mut tx, auth_ctx, plan, & TestLogger ) ?;
370
366
371
367
// Expect the schema change.
372
368
let idx_b_id = stdb
0 commit comments