Skip to content

Commit 4215686

Browse files
committed
Migrations behind env variable & stats reset migration
1 parent 889f1f5 commit 4215686

File tree

3 files changed

+64
-6
lines changed

3 files changed

+64
-6
lines changed

src/database/migrations/mod.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use crate::database::Database;
22
use crate::database::migrations::denormalize_ip_identities::DenormalizeIpIdentitiesMigration;
3+
use crate::database::migrations::reset_stats::ResetStatsMigration;
34

45
pub mod denormalize_ip_identities;
6+
mod reset_stats;
57

68
#[async_trait]
79
pub trait DatabaseMigration {
@@ -17,17 +19,26 @@ impl MigrationExecutor {
1719
pub fn new() -> Self {
1820
let denormalize_ip_identities_migration =
1921
Box::new(DenormalizeIpIdentitiesMigration {});
22+
let reset_stats_migration =
23+
Box::new(ResetStatsMigration {});
2024
Self {
21-
migrations: vec![denormalize_ip_identities_migration]
25+
migrations: vec![
26+
denormalize_ip_identities_migration,
27+
reset_stats_migration
28+
]
2229
}
2330
}
2431

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 {
2633
match self.migrations.iter().find(|migration| migration.get_id() == name) {
2734
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
2941
}
30-
None => {}
3142
}
3243
}
3344
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
}

src/main.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,21 @@ async fn main() -> Result<(), String> {
168168
leaderboards
169169
};
170170

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+
};
173186

174187
let ws_port = env::var("MARS_WS_PORT").unwrap_or("7000".to_owned()).parse::<u32>().unwrap_or(7000);
175188
let res = tokio::try_join!(

0 commit comments

Comments
 (0)