Move interior refs list to interior_refs table

Still WIP, need to actually save the data to the table.
This commit is contained in:
Tyler Hallada 2020-07-22 22:42:17 -04:00
parent 65e6ba1f8a
commit 6eb58935e3
3 changed files with 14 additions and 81 deletions

View File

@ -26,7 +26,6 @@ pub fn migration() -> String {
t.add_column("sell_buy_list_id", types::integer().default(0));
t.add_column("vendor_id", types::integer());
t.add_column("vendor_gold", types::integer());
t.add_column("interior_refs", types::custom("jsonb"));
t.add_column("created_at", types::custom("timestamp(3)"));
t.add_column("updated_at", types::custom("timestamp(3)"));
t.add_index(
@ -61,19 +60,14 @@ pub fn migration() -> String {
t.add_column("created_at", types::custom("timestamp(3)"));
});
// TODO: rename to interior_reflist or similar?
// TODO: index shop_id col
m.create_table("interior_refs", |t| {
t.add_column("id", types::primary().indexed(true));
t.add_column("shop_id", types::foreign("shops", "id"));
t.add_column("mod_name", types::varchar(255));
t.add_column("local_form_id", types::integer());
t.add_column("position_x", types::float());
t.add_column("position_y", types::float());
t.add_column("position_z", types::float());
t.add_column("angle_x", types::float());
t.add_column("angle_y", types::float());
t.add_column("angle_z", types::float());
t.add_column("scale", types::float());
t.add_column("references", types::custom("jsonb"));
t.add_column("created_at", types::custom("timestamp(3)"));
t.add_column("updated_at", types::custom("timestamp(3)"));
});
m.make::<Pg>()

View File

@ -12,16 +12,9 @@ use super::Model;
pub struct InteriorRef {
pub id: Option<i32>,
pub shop_id: i32,
pub mod_name: String,
pub local_form_id: i32,
pub position_x: f64,
pub position_y: f64,
pub position_z: f64,
pub angle_x: f64,
pub angle_y: f64,
pub angle_z: f64,
pub scale: f64,
pub references: serde_json::value::Value,
pub created_at: Option<NaiveDateTime>,
pub updated_at: Option<NaiveDateTime>,
}
#[async_trait]
@ -46,23 +39,17 @@ impl Model for InteriorRef {
async fn save(self, db: &PgPool) -> Result<Self> {
let timer = std::time::Instant::now();
// TODO:
// * Actually save the references list to the jsonb column
// * Decide if I'll need to make the same changes to merchandise and transactions
// - answer depends on how many rows of each I expect to insert in one go
let result = sqlx::query_as!(
Self,
"INSERT INTO interior_refs
(shop_id, mod_name, local_form_id, position_x, position_y, position_z, angle_x,
angle_y, angle_z, scale, created_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, now())
(shop_id, created_at, updated_at)
VALUES ($1, now(), now())
RETURNING *",
self.shop_id,
self.mod_name,
self.local_form_id,
self.position_x,
self.position_y,
self.position_z,
self.angle_x,
self.angle_y,
self.angle_z,
self.scale,
)
.fetch_one(db)
.await?;
@ -102,50 +89,4 @@ impl Model for InteriorRef {
debug!("SELECT * FROM interior_refs ... {:.3?}", elapsed);
Ok(result)
}
// TODO: figure out a way bulk insert in a single query
// see: https://github.com/launchbadge/sqlx/issues/294
async fn bulk_save(db: &PgPool, interior_refs: Vec<Self>) -> Result<()> {
let timer = std::time::Instant::now();
// Testing whether setting a jsonb column with an array of 200 items is faster than
// inserting 200 rows. Answer: it is a hell of a lot faster!
// TODO:
// 1. remove interior_refs column from shops
// 2. replace all columns in interior_refs table with single `refs` jsonb column and
// shops_id foreign_key
// 3. This function will now create the row in that table
// 4. Decide if I'll need to make the same changes to merchandise and transactions
// - answer depends on how many rows of each I expect to insert in one go
sqlx::query!(
"UPDATE shops SET interior_refs = $1::jsonb",
serde_json::to_value(&interior_refs)?,
)
.execute(db)
.await?;
// let mut transaction = db.begin().await?;
// for interior_ref in interior_refs {
// sqlx::query!(
// "INSERT INTO interior_refs
// (shop_id, mod_name, local_form_id, position_x, position_y, position_z, angle_x,
// angle_y, angle_z, scale, created_at)
// VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, now())",
// interior_ref.shop_id,
// interior_ref.mod_name,
// interior_ref.local_form_id,
// interior_ref.position_x,
// interior_ref.position_y,
// interior_ref.position_z,
// interior_ref.angle_x,
// interior_ref.angle_y,
// interior_ref.angle_z,
// interior_ref.scale,
// )
// .execute(&mut transaction)
// .await?;
// }
// transaction.commit().await?;
let elapsed = timer.elapsed();
debug!("INSERT INTO interior_refs ... {:.3?}", elapsed);
Ok(())
}
}

View File

@ -18,7 +18,6 @@ pub struct Shop {
pub sell_buy_list_id: i32,
pub vendor_id: i32,
pub vendor_gold: i32,
pub interior_refs: serde_json::value::Value,
pub created_at: Option<NaiveDateTime>,
pub updated_at: Option<NaiveDateTime>,
}
@ -49,8 +48,8 @@ impl Model for Shop {
Self,
"INSERT INTO shops
(name, owner_id, description, is_not_sell_buy, sell_buy_list_id, vendor_id,
vendor_gold, interior_refs, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, now(), now())
vendor_gold, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, now(), now())
RETURNING *",
self.name,
self.owner_id,
@ -59,7 +58,6 @@ impl Model for Shop {
self.sell_buy_list_id,
self.vendor_id,
self.vendor_gold,
self.interior_refs,
)
.fetch_one(db)
.await?;