File tree Expand file tree Collapse file tree 3 files changed +64
-6
lines changed Expand file tree Collapse file tree 3 files changed +64
-6
lines changed Original file line number Diff line number Diff line change 1
1
use crate :: database:: Database ;
2
2
use crate :: database:: migrations:: denormalize_ip_identities:: DenormalizeIpIdentitiesMigration ;
3
+ use crate :: database:: migrations:: reset_stats:: ResetStatsMigration ;
3
4
4
5
pub mod denormalize_ip_identities;
6
+ mod reset_stats;
5
7
6
8
#[ async_trait]
7
9
pub trait DatabaseMigration {
@@ -17,17 +19,26 @@ impl MigrationExecutor {
17
19
pub fn new ( ) -> Self {
18
20
let denormalize_ip_identities_migration =
19
21
Box :: new ( DenormalizeIpIdentitiesMigration { } ) ;
22
+ let reset_stats_migration =
23
+ Box :: new ( ResetStatsMigration { } ) ;
20
24
Self {
21
- migrations : vec ! [ denormalize_ip_identities_migration]
25
+ migrations : vec ! [
26
+ denormalize_ip_identities_migration,
27
+ reset_stats_migration
28
+ ]
22
29
}
23
30
}
24
31
25
- pub async fn execute_migration_by_name ( & self , database : & Database , name : String ) {
32
+ pub async fn execute_migration_by_name ( & self , database : & Database , name : String ) -> bool {
26
33
match self . migrations . iter ( ) . find ( |migration| migration. get_id ( ) == name) {
27
34
Some ( migration) => {
28
- migration. perform ( database) . await
35
+ migration. perform ( database) . await ;
36
+ true
37
+ }
38
+ None => {
39
+ warn ! ( "Could not find migration '{}'" , name) ;
40
+ false
29
41
}
30
- None => { }
31
42
}
32
43
}
33
44
}
Original file line number Diff line number Diff line change
1
+ use mongodb:: bson:: doc;
2
+ use mongodb:: options:: UpdateOptions ;
3
+ use crate :: database:: Database ;
4
+ use crate :: database:: migrations:: DatabaseMigration ;
5
+
6
+ pub struct ResetStatsMigration { }
7
+
8
+ // resets stats for players, in specific, set stats objects to empty objects
9
+ #[ async_trait]
10
+ impl DatabaseMigration for ResetStatsMigration {
11
+ fn get_id ( & self ) -> String {
12
+ String :: from ( "reset_stats" )
13
+ }
14
+
15
+ async fn perform ( & self , database : & Database ) {
16
+ info ! ( "Resetting all player stats..." ) ;
17
+ let update_result = database. players . update_many (
18
+ doc ! { } ,
19
+ doc ! { "$set" : { "stats" : { } } } ,
20
+ None
21
+ ) . await ;
22
+ match update_result {
23
+ Ok ( result) => {
24
+ info ! (
25
+ "Successfully reset player statistics, {} documents were modified" ,
26
+ result. modified_count
27
+ ) ;
28
+ }
29
+ Err ( err) => {
30
+ warn ! ( "Could not reset stats: {}" , err) ;
31
+ }
32
+ } ;
33
+ }
34
+ }
Original file line number Diff line number Diff line change @@ -168,8 +168,21 @@ async fn main() -> Result<(), String> {
168
168
leaderboards
169
169
} ;
170
170
171
- // let migration_executor = MigrationExecutor::new();
172
- // migration_executor.execute_migration_by_name(&*state.database, String::from("denormalize_ip_identities")).await;
171
+
172
+ if env:: var ( "MARS_DATABASE_MIGRATION" ) . is_ok ( ) {
173
+ let migration = env:: var ( "MARS_DATABASE_MIGRATION" ) . unwrap_or ( "NONE" . to_owned ( ) ) ;
174
+ info ! ( "API will not run, migration is set" ) ;
175
+ info ! ( "Executing migration '{}'..." , migration. to_owned( ) ) ;
176
+ let migration_executor = MigrationExecutor :: new ( ) ;
177
+ let migration_found = migration_executor. execute_migration_by_name (
178
+ & * state. database ,
179
+ migration. to_owned ( )
180
+ ) . await ;
181
+ if !migration_found {
182
+ warn ! ( "Did not find migration '{}'" , & migration) ;
183
+ }
184
+ return Ok ( ( ) ) ;
185
+ } ;
173
186
174
187
let ws_port = env:: var ( "MARS_WS_PORT" ) . unwrap_or ( "7000" . to_owned ( ) ) . parse :: < u32 > ( ) . unwrap_or ( 7000 ) ;
175
188
let res = tokio:: try_join!(
You can’t perform that action at this time.
0 commit comments