Include cells with plugin data

This commit is contained in:
Tyler Hallada 2022-03-10 23:27:26 -05:00
parent 7552573d00
commit c9dcd3b7c5
2 changed files with 18 additions and 9 deletions

View File

@ -25,7 +25,8 @@ pub async fn dump_plugin_data(pool: &sqlx::Pool<sqlx::Postgres>, dir: &str) -> R
let page_size = 20; let page_size = 20;
let mut last_id = None; let mut last_id = None;
loop { loop {
let plugins = plugin::batched_get_with_file_and_mod(&pool, page_size, last_id).await?; let plugins =
plugin::batched_get_with_data(&pool, page_size, last_id, "Skyrim.esm", 1).await?;
if plugins.is_empty() { if plugins.is_empty() {
break; break;
} }

View File

@ -38,7 +38,7 @@ pub struct UnsavedPlugin<'a> {
} }
#[derive(Debug, Serialize, Deserialize, FromRow)] #[derive(Debug, Serialize, Deserialize, FromRow)]
pub struct PluginWithFileAndMod { pub struct PluginWithData {
pub id: i32, pub id: i32,
pub name: String, pub name: String,
pub hash: i64, pub hash: i64,
@ -55,6 +55,7 @@ pub struct PluginWithFileAndMod {
pub created_at: NaiveDateTime, pub created_at: NaiveDateTime,
pub file: Option<serde_json::Value>, pub file: Option<serde_json::Value>,
pub r#mod: Option<serde_json::Value>, pub r#mod: Option<serde_json::Value>,
pub cells: Option<serde_json::Value>,
} }
#[instrument(level = "debug", skip(pool))] #[instrument(level = "debug", skip(pool))]
@ -89,29 +90,36 @@ pub async fn insert<'a>(
} }
#[instrument(level = "debug", skip(pool))] #[instrument(level = "debug", skip(pool))]
pub async fn batched_get_with_file_and_mod( pub async fn batched_get_with_data(
pool: &sqlx::Pool<sqlx::Postgres>, pool: &sqlx::Pool<sqlx::Postgres>,
page_size: i64, page_size: i64,
last_id: Option<i32>, last_id: Option<i32>,
) -> Result<Vec<PluginWithFileAndMod>> { master: &str,
world_id: i32,
) -> Result<Vec<PluginWithData>> {
let last_id = last_id.unwrap_or(0); let last_id = last_id.unwrap_or(0);
sqlx::query_as!( sqlx::query_as!(
PluginWithFileAndMod, PluginWithData,
"SELECT "SELECT
plugins.*, plugins.*,
json_agg(files.*) as file, json_agg(DISTINCT files.*) as file,
json_agg(mods.*) as mod json_agg(DISTINCT mods.*) as mod,
COALESCE(json_agg(DISTINCT jsonb_build_object('x', cells.x, 'y', cells.y)) FILTER (WHERE cells.x IS NOT NULL AND cells.y IS NOT NULL AND cells.master = $3 AND cells.world_id = $4), '[]') AS cells
FROM plugins FROM plugins
LEFT OUTER JOIN files ON files.id = plugins.file_id LEFT OUTER JOIN files ON files.id = plugins.file_id
LEFT OUTER JOIN mods ON mods.id = files.mod_id LEFT OUTER JOIN mods ON mods.id = files.mod_id
LEFT OUTER JOIN plugin_cells ON plugin_cells.plugin_id = plugins.id
LEFT OUTER JOIN cells ON cells.id = plugin_cells.cell_id
WHERE plugins.id > $2 WHERE plugins.id > $2
GROUP BY plugins.id GROUP BY plugins.id
ORDER BY plugins.id ASC ORDER BY plugins.id ASC
LIMIT $1", LIMIT $1",
page_size, page_size,
last_id last_id,
master,
world_id
) )
.fetch_all(pool) .fetch_all(pool)
.await .await
.context("Failed to batch get with cells") .context("Failed to batch get with data")
} }