Upgrade skyrim-cell-dump and handle Cow<str>s

This commit is contained in:
Tyler Hallada 2021-07-23 23:40:05 -04:00
parent 19126f4981
commit 39ae7703b0
4 changed files with 36 additions and 45 deletions

5
Cargo.lock generated
View File

@ -1792,12 +1792,13 @@ checksum = "cbce6d4507c7e4a3962091436e56e95290cb71fa302d0d270e32130b75fbff27"
[[package]] [[package]]
name = "skyrim-cell-dump" name = "skyrim-cell-dump"
version = "0.2.1" version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23e018bdbd3339262da78d4e5b08e24fd028e8c6f811a7dada7156dd03a6ef16" checksum = "5a0abb8da2a8271d3f260136a73c57ade0914a0aeba5c0e653f9500db5b4bf3d"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"bitflags", "bitflags",
"encoding_rs",
"flate2", "flate2",
"nom", "nom",
"serde", "serde",

View File

@ -22,7 +22,7 @@ seahash = "4.1"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
sqlx = { version = "0.5", features = ["runtime-tokio-native-tls", "postgres", "migrate", "chrono"] } sqlx = { version = "0.5", features = ["runtime-tokio-native-tls", "postgres", "migrate", "chrono"] }
skyrim-cell-dump = "0.2.1" skyrim-cell-dump = "0.3.0"
tempfile = "3.2" tempfile = "3.2"
tokio = { version = "1.5.0", features = ["full"] } tokio = { version = "1.5.0", features = ["full"] }
tokio-util = { version = "0.6", features = ["compat"] } tokio-util = { version = "0.6", features = ["compat"] }

View File

@ -5,6 +5,7 @@ use dotenv::dotenv;
use reqwest::StatusCode; use reqwest::StatusCode;
use skyrim_cell_dump::parse_plugin; use skyrim_cell_dump::parse_plugin;
use sqlx::postgres::PgPoolOptions; use sqlx::postgres::PgPoolOptions;
use std::borrow::Borrow;
use std::convert::TryInto; use std::convert::TryInto;
use std::env; use std::env;
use std::fs::OpenOptions; use std::fs::OpenOptions;
@ -86,6 +87,9 @@ where
.file_name() .file_name()
.expect("plugin path ends in a valid file_name") .expect("plugin path ends in a valid file_name")
.to_string_lossy(); .to_string_lossy();
let author = plugin.header.author.as_deref();
let description = plugin.header.description.as_deref();
let masters: Vec<&str> = plugin.header.masters.iter().map(|s| s.borrow()).collect();
let plugin_row = plugin::insert( let plugin_row = plugin::insert(
&pool, &pool,
&db_file.name, &db_file.name,
@ -93,14 +97,9 @@ where
db_file.id, db_file.id,
plugin.header.version as f64, plugin.header.version as f64,
plugin_buf.len() as i64, plugin_buf.len() as i64,
plugin.header.author, author,
plugin.header.description, description,
&plugin &masters,
.header
.masters
.iter()
.map(|s| s.to_string())
.collect::<Vec<String>>(),
&file_name, &file_name,
file_path, file_path,
) )
@ -110,12 +109,9 @@ where
.worlds .worlds
.iter() .iter()
.map(|world| { .map(|world| {
let (form_id, master) = get_local_form_id_and_master( let (form_id, master) =
world.form_id, get_local_form_id_and_master(world.form_id, &masters, &file_name)
&plugin.header.masters, .expect("form_id to be a valid i32");
&file_name,
)
.expect("form_id to be a valid i32");
UnsavedWorld { form_id, master } UnsavedWorld { form_id, master }
}) })
.collect(); .collect();
@ -136,12 +132,9 @@ where
.iter() .iter()
.map(|cell| { .map(|cell| {
let world_id = if let Some(world_form_id) = cell.world_form_id { let world_id = if let Some(world_form_id) = cell.world_form_id {
let (form_id, master) = get_local_form_id_and_master( let (form_id, master) =
world_form_id, get_local_form_id_and_master(world_form_id, &masters, &file_name)
&plugin.header.masters, .expect("form_id to be valid i32");
&file_name,
)
.expect("form_id to be valid i32");
Some( Some(
db_worlds db_worlds
.iter() .iter()
@ -152,12 +145,9 @@ where
} else { } else {
None None
}; };
let (form_id, master) = get_local_form_id_and_master( let (form_id, master) =
cell.form_id, get_local_form_id_and_master(cell.form_id, &masters, &file_name)
&plugin.header.masters, .expect("form_id is a valid i32");
&file_name,
)
.expect("form_id is a valid i32");
UnsavedCell { UnsavedCell {
form_id, form_id,
master, master,

View File

@ -1,9 +1,10 @@
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use chrono::NaiveDateTime; use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use tracing::instrument; use tracing::instrument;
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize, FromRow)]
pub struct Plugin { pub struct Plugin {
pub id: i32, pub id: i32,
pub name: String, pub name: String,
@ -30,30 +31,29 @@ pub async fn insert(
size: i64, size: i64,
author: Option<&str>, author: Option<&str>,
description: Option<&str>, description: Option<&str>,
masters: &[String], masters: &[&str],
file_name: &str, file_name: &str,
file_path: &str, file_path: &str,
) -> Result<Plugin> { ) -> Result<Plugin> {
sqlx::query_as!( // sqlx doesn't understand slices of &str with the query_as! macro: https://github.com/launchbadge/sqlx/issues/280
Plugin, sqlx::query_as(
"INSERT INTO plugins r#"INSERT INTO plugins
(name, hash, file_id, version, size, author, description, masters, file_name, file_path, created_at, updated_at) (name, hash, file_id, version, size, author, description, masters, file_name, file_path, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, now(), now()) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, now(), now())
ON CONFLICT (file_id, file_path) DO UPDATE ON CONFLICT (file_id, file_path) DO UPDATE
SET (name, hash, version, author, description, masters, file_name, updated_at) = SET (name, hash, version, author, description, masters, file_name, updated_at) =
(EXCLUDED.name, EXCLUDED.hash, EXCLUDED.version, EXCLUDED.author, EXCLUDED.description, EXCLUDED.masters, EXCLUDED.file_name, now()) (EXCLUDED.name, EXCLUDED.hash, EXCLUDED.version, EXCLUDED.author, EXCLUDED.description, EXCLUDED.masters, EXCLUDED.file_name, now())
RETURNING *", RETURNING *"#,
name,
hash,
file_id,
version,
size,
author,
description,
masters,
file_name,
file_path
) )
.bind(name)
.bind(hash)
.bind(file_id)
.bind(version)
.bind(size)
.bind(author)
.bind(description)
.bind(masters)
.bind(file_name)
.fetch_one(pool) .fetch_one(pool)
.await .await
.context("Failed to insert plugin") .context("Failed to insert plugin")