add general maintenance script to always run in prod

This commit is contained in:
41666 2023-01-23 08:53:06 -05:00
parent 9940e9dd90
commit 679b49ff88
2 changed files with 25 additions and 1 deletions

View file

@ -66,6 +66,18 @@ async fn main() {
cmd_prune().await;
tokio::time::sleep(tokio::time::Duration::from_secs(60 * 5)).await;
},
"maintenance" => {
println!("Running maintenance tasks...");
println!("Checking if DB is migrated...");
if !migrations::is_migrated().await {
println!("DB is not migrated, running migrations...");
cmd_migrate().await;
}
println!("Running prune...");
cmd_prune().await;
println!("Done!");
}
"migrate" => cmd_migrate().await,
_ => {
println!("Unknown command: {}", command);

View file

@ -1,5 +1,5 @@
use crate::PG;
use sqlx::query;
use sqlx::{query, Row};
pub async fn cmd_migrate() {
println!("Migrating database...");
@ -173,3 +173,15 @@ async fn migrate_analytics() {
println!("ANALYTICS => done!");
}
pub async fn is_migrated() -> bool {
let pool = PG.get().await;
let tables: i64 = query("SELECT count(1) FROM pg_tables WHERE schemaname = 'public' AND tablename IN ('players', 'vehicles', 'classes', 'analytics');")
.fetch_one(pool)
.await
.unwrap()
.get(0);
tables == 4
}