Dump plugins data with file and mod data

This commit is contained in:
2022-03-10 22:32:46 -05:00
parent 17f766da29
commit 7552573d00
6 changed files with 105 additions and 3 deletions

View File

@@ -0,0 +1,42 @@
use anyhow::Result;
use std::fs::{create_dir_all, File};
use std::io::Write;
use std::path::Path;
use crate::models::plugin;
// From: https://stackoverflow.com/a/50278316/6620612
fn format_radix(mut x: u64, radix: u32) -> String {
let mut result = vec![];
loop {
let m = x % radix as u64;
x = x / radix as u64;
// will panic if you use a bad radix (< 2 or > 36).
result.push(std::char::from_digit(m as u32, radix).unwrap());
if x == 0 {
break;
}
}
result.into_iter().rev().collect()
}
pub async fn dump_plugin_data(pool: &sqlx::Pool<sqlx::Postgres>, dir: &str) -> Result<()> {
let page_size = 20;
let mut last_id = None;
loop {
let plugins = plugin::batched_get_with_file_and_mod(&pool, page_size, last_id).await?;
if plugins.is_empty() {
break;
}
for plugin in plugins {
let path = Path::new(&dir);
create_dir_all(&path)?;
let path = path.join(format!("{}.json", format_radix(plugin.hash as u64, 36)));
let mut file = File::create(path)?;
write!(file, "{}", serde_json::to_string(&plugin)?)?;
last_id = Some(plugin.id);
}
}
return Ok(());
}

View File

@@ -3,6 +3,7 @@ pub mod dump_cell_data;
pub mod dump_cell_edit_counts;
pub mod dump_mod_data;
pub mod dump_mod_search_index;
pub mod dump_plugin_data;
pub mod update;
pub use download_tiles::download_tiles;
@@ -10,4 +11,5 @@ pub use dump_cell_data::dump_cell_data;
pub use dump_cell_edit_counts::dump_cell_edit_counts;
pub use dump_mod_data::dump_mod_data;
pub use dump_mod_search_index::dump_mod_search_index;
pub use dump_plugin_data::dump_plugin_data;
pub use update::update;