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]]
name = "skyrim-cell-dump"
version = "0.2.1"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23e018bdbd3339262da78d4e5b08e24fd028e8c6f811a7dada7156dd03a6ef16"
checksum = "5a0abb8da2a8271d3f260136a73c57ade0914a0aeba5c0e653f9500db5b4bf3d"
dependencies = [
"anyhow",
"bitflags",
"encoding_rs",
"flate2",
"nom",
"serde",

View File

@ -22,7 +22,7 @@ seahash = "4.1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
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"
tokio = { version = "1.5.0", features = ["full"] }
tokio-util = { version = "0.6", features = ["compat"] }

View File

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

View File

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