database repair tool
This commit is contained in:
76
src/main.rs
76
src/main.rs
@ -15,10 +15,18 @@ struct Cli{
|
|||||||
|
|
||||||
#[derive(Subcommand)]
|
#[derive(Subcommand)]
|
||||||
enum Commands{
|
enum Commands{
|
||||||
|
RepairPolicies(RepairPoliciesCommand),
|
||||||
Review(ReviewCommand),
|
Review(ReviewCommand),
|
||||||
UploadScripts(UploadScriptsCommand),
|
UploadScripts(UploadScriptsCommand),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Args)]
|
||||||
|
struct RepairPoliciesCommand{
|
||||||
|
#[arg(long)]
|
||||||
|
session_id_file:PathBuf,
|
||||||
|
#[arg(long)]
|
||||||
|
api_url:String,
|
||||||
|
}
|
||||||
#[derive(Args)]
|
#[derive(Args)]
|
||||||
struct ReviewCommand{
|
struct ReviewCommand{
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
@ -38,6 +46,10 @@ struct UploadScriptsCommand{
|
|||||||
async fn main(){
|
async fn main(){
|
||||||
let cli=Cli::parse();
|
let cli=Cli::parse();
|
||||||
match cli.command{
|
match cli.command{
|
||||||
|
Commands::RepairPolicies(command)=>repair(RepairPoliciesConfig{
|
||||||
|
session_id:std::fs::read_to_string(command.session_id_file).unwrap(),
|
||||||
|
api_url:command.api_url,
|
||||||
|
}).await.unwrap(),
|
||||||
Commands::Review(command)=>review(ReviewConfig{
|
Commands::Review(command)=>review(ReviewConfig{
|
||||||
session_id:std::fs::read_to_string(command.session_id_file).unwrap(),
|
session_id:std::fs::read_to_string(command.session_id_file).unwrap(),
|
||||||
api_url:command.api_url,
|
api_url:command.api_url,
|
||||||
@ -419,3 +431,67 @@ async fn upload_scripts(config:UploadConfig)->Result<(),ScriptUploadError>{
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum RepairPoliciesError{
|
||||||
|
Cookie(submissions_api::CookieError),
|
||||||
|
Reqwest(submissions_api::ReqwestError),
|
||||||
|
GetPolicies(submissions_api::Error),
|
||||||
|
GetScripts(submissions_api::types::SingleItemError),
|
||||||
|
UpdateScriptPolicy(submissions_api::Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
struct RepairPoliciesConfig{
|
||||||
|
session_id:String,
|
||||||
|
api_url:String,
|
||||||
|
}
|
||||||
|
async fn repair(config:RepairPoliciesConfig)->Result<(),RepairPoliciesError>{
|
||||||
|
let cookie=submissions_api::Cookie::new(&config.session_id).map_err(RepairPoliciesError::Cookie)?;
|
||||||
|
let api=&submissions_api::external::Context::new(config.api_url,cookie).map_err(RepairPoliciesError::Reqwest)?;
|
||||||
|
|
||||||
|
const LIMIT:u32=100;
|
||||||
|
let mut page=1;
|
||||||
|
loop{
|
||||||
|
println!("Downloading page {page}...");
|
||||||
|
let policies=api.get_script_policies(submissions_api::types::GetScriptPoliciesRequest{
|
||||||
|
Page:page,
|
||||||
|
Limit:LIMIT,
|
||||||
|
FromScriptHash:None,
|
||||||
|
ToScriptID:None,
|
||||||
|
Policy:Some(submissions_api::types::Policy::Replace),
|
||||||
|
}).await.map_err(RepairPoliciesError::GetPolicies)?;
|
||||||
|
|
||||||
|
futures::stream::iter(policies.iter().map(Ok)).try_for_each_concurrent(REMOTE_CONCURRENCY,async|policy|{
|
||||||
|
let from_script=api.get_script_from_hash(submissions_api::types::HashRequest{
|
||||||
|
hash:policy.FromScriptHash.as_str(),
|
||||||
|
}).await.map_err(RepairPoliciesError::GetScripts)?;
|
||||||
|
|
||||||
|
if let Some(from_script)=from_script{
|
||||||
|
if policy.ToScriptID==from_script.ID{
|
||||||
|
// invalid policy. Reset the policy to None
|
||||||
|
api.update_script_policy(submissions_api::types::UpdateScriptPolicyRequest{
|
||||||
|
ID:policy.ID,
|
||||||
|
FromScriptID:None,
|
||||||
|
ToScriptID:None,
|
||||||
|
Policy:Some(submissions_api::types::Policy::None),
|
||||||
|
}).await.map_err(RepairPoliciesError::UpdateScriptPolicy)?;
|
||||||
|
println!("Policy updated! {:?}",policy.ID);
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
println!("Script did not exist! hash={}",policy.FromScriptHash);
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}).await?;
|
||||||
|
|
||||||
|
if policies.len()<LIMIT as usize{
|
||||||
|
// We scanned all policies
|
||||||
|
println!("Done!");
|
||||||
|
break;
|
||||||
|
}else{
|
||||||
|
page+=1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user