-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use anyhow::{anyhow, Ok, Result}; | ||
use aot_backend::schema::{artifact, map_layout, map_spaces, user}; | ||
use aot_backend::util; | ||
use diesel::dsl::sum; | ||
use diesel::prelude::*; | ||
use diesel::QueryDsl; | ||
|
||
fn main() -> Result<()> { | ||
let pool = util::get_pg_conn_pool(); | ||
let mut conn = pool.get().expect("Could not retrieve connection from pool"); | ||
|
||
let users = user::table | ||
.select(user::id) | ||
.load::<i32>(&mut conn) | ||
.expect("Could not get user ids"); | ||
|
||
let _ = Ok(users.into_iter().try_for_each(|user_id| { | ||
let total_artifacts_in_user_base: Option<i64> = map_spaces::table | ||
.inner_join(artifact::table) | ||
.inner_join(map_layout::table) | ||
.filter(map_layout::player.eq(user_id)) | ||
.select(sum(artifact::count)) | ||
.first(&mut conn) | ||
.map_err(|err| anyhow!("Error getting sum of artifacts for user: {}", err))?; | ||
|
||
diesel::update(user::table.filter(user::id.eq(user_id))) | ||
.set(user::artifacts.eq(total_artifacts_in_user_base.unwrap_or(0) as i32)) | ||
.execute(&mut conn) | ||
.map(|_| ()) | ||
.map_err(|err| anyhow!("Error updating user artifacts: {}", err)) | ||
}))?; | ||
|
||
println!("Adjusted artifacts for all users"); | ||
|
||
Ok(()) | ||
} |