yggdrasil/src/dbtool/dump.rs

88 lines
2.7 KiB
Rust

/*
* Yggdrasil: Minecraft authentication server
* Copyright (C) 2023 0xf8.dev@proton.me
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use anyhow::{bail, Result};
use log::info;
use yggdrasil::*;
use yggdrasil::structs::*;
use crate::dbtool::Args;
pub struct Dump {}
impl Dump {
async fn dump_accounts(db: &Database) -> Result<()> {
let r = sqlx::query_as!(account::AccountRaw, "SELECT * FROM accounts")
.fetch_all(&db.pool)
.await?;
info!("[ Got {} records ]", r.len());
Ok(for a in r {
info!("{:#?}", a.complete(db).await)
})
}
async fn dump_profiles(db: &Database) -> Result<()> {
let r = sqlx::query_as!(profile::ProfileRaw, "SELECT * FROM profiles")
.fetch_all(&db.pool)
.await?;
info!("[ Got {} records ]", r.len());
Ok(for p in r {
info!("{:#?}", p.complete(db).await.to_simple())
})
}
async fn dump_sessions(db: &Database) -> Result<()> {
let r = sqlx::query_as!(session::SessionRaw, "SELECT * FROM sessions")
.fetch_all(&db.pool)
.await?;
info!("[ Got {} records ]", r.len());
Ok(for s in r {
info!("{:#?}", s.complete(db).await)
})
}
async fn dump_tokens(db: &Database) -> Result<()> {
let r = sqlx::query_as!(token::TokenRaw, "SELECT * FROM tokens")
.fetch_all(&db.pool)
.await?;
info!("[ Got {} records ]", r.len());
Ok(for t in r {
info!("{:#?}", t.complete(db).await)
})
}
pub async fn exec(args: Args, db: &Database) -> Result<()> {
if args.arguments.len() < 1 { bail!("Not enough arguments. dump <table>") }
let table = args.arguments.get(0).unwrap().to_lowercase();
match table.as_str() {
"accounts" => Self::dump_accounts(db).await?,
"profiles" => Self::dump_profiles(db).await?,
"sessions" => Self::dump_sessions(db).await?,
"tokens" => Self::dump_tokens(db).await?,
_ => bail!("Invalid table \"{table}\". Tables: accounts, profiles, sessions, tokens")
}
Ok(())
}
}