Skip to content

Commit

Permalink
fix: update db calls to not use dates in pks & reduce dupe key failur…
Browse files Browse the repository at this point in the history
…es. closes #18
  • Loading branch information
akarras committed Sep 18, 2023
1 parent 215d267 commit 8e39d24
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 28 deletions.
2 changes: 2 additions & 0 deletions migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod m20221112_211028_add_final_fantasy_character_relation_to_verification;
mod m20221227_164853_price_alert;
mod m20230311_230232_cleanup_fks;
mod m20230312_184157_add_last_updated_table;
mod m20230918_033709_remove_junk_pk_from_listing;

pub struct Migrator;

Expand Down Expand Up @@ -47,6 +48,7 @@ impl MigratorTrait for Migrator {
Box::new(m20221227_164853_price_alert::Migration),
Box::new(m20230311_230232_cleanup_fks::Migration),
Box::new(m20230312_184157_add_last_updated_table::Migration),
Box::new(m20230918_033709_remove_junk_pk_from_listing::Migration),
]
}
}
55 changes: 55 additions & 0 deletions migration/src/m20230918_033709_remove_junk_pk_from_listing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use sea_orm_migration::prelude::*;

use crate::m20220101_000001_create_table::{ActiveListing, SaleHistory};

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// alter table sale_history
// drop constraint sale_history_pkey;

// alter table sale_history
// add primary key (id);
manager
.drop_foreign_key(
ForeignKeyDropStatement::new()
.table(SaleHistory::Table)
.name("sale_history_pkey")
.to_owned(),
)
.await?;
manager
.alter_table(
TableAlterStatement::new()
.modify_column(ColumnDef::new(SaleHistory::Id).primary_key())
.table(SaleHistory::Table)
.to_owned(),
)
.await?;
manager
.drop_foreign_key(
ForeignKeyDropStatement::new()
.table(ActiveListing::Table)
.name("active_listing_pkey")
.to_owned(),
)
.await?;
manager
.alter_table(
TableAlterStatement::new()
.modify_column(ColumnDef::new(ActiveListing::Id).primary_key())
.table(ActiveListing::Table)
.to_owned(),
)
.await?;
Ok(())
}

async fn down(&self, _manager: &SchemaManager) -> Result<(), DbErr> {
// Replace the sample below with your own migration scripts
todo!();
}
}
1 change: 0 additions & 1 deletion ultros-db/src/entity/active_listing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ pub struct Model {
pub price_per_unit: i32,
pub quantity: i32,
pub hq: bool,
#[sea_orm(primary_key, auto_increment = false)]
pub timestamp: DateTime,
}

Expand Down
2 changes: 1 addition & 1 deletion ultros-db/src/entity/ffxiv_character_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct Model {
pub discord_user_id: i64,
pub ffxiv_character_id: i32,
pub challenge: String,
#[sea_orm(primary_key)]
#[sea_orm(primary_key, auto_increment = false)]
pub id: i32,
}

Expand Down
21 changes: 9 additions & 12 deletions ultros-db/src/entity/retainer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ pub struct Model {

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::active_listing::Entity")]
ActiveListing,
#[sea_orm(has_many = "super::owned_retainers::Entity")]
OwnedRetainers,
#[sea_orm(has_many = "super::active_listing::Entity")]
ActiveListings,
#[sea_orm(
belongs_to = "super::retainer_city::Entity",
from = "Column::RetainerCityId",
Expand All @@ -37,6 +37,12 @@ pub enum Relation {
World,
}

impl Related<super::active_listing::Entity> for Entity {
fn to() -> RelationDef {
Relation::ActiveListing.def()
}
}

impl Related<super::owned_retainers::Entity> for Entity {
fn to() -> RelationDef {
Relation::OwnedRetainers.def()
Expand All @@ -51,16 +57,7 @@ impl Related<super::retainer_city::Entity> for Entity {

impl Related<super::world::Entity> for Entity {
fn to() -> RelationDef {
super::active_listing::Relation::World.def()
}
fn via() -> Option<RelationDef> {
Some(super::active_listing::Relation::Retainer.def().rev())
}
}

impl Related<super::active_listing::Entity> for Entity {
fn to() -> RelationDef {
Relation::ActiveListings.def()
Relation::World.def()
}
}

Expand Down
19 changes: 12 additions & 7 deletions ultros-db/src/entity/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub struct Model {

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::active_listing::Entity")]
ActiveListing,
#[sea_orm(
belongs_to = "super::datacenter::Entity",
from = "Column::DatacenterId",
Expand All @@ -33,6 +35,12 @@ pub enum Relation {
SaleHistory,
}

impl Related<super::active_listing::Entity> for Entity {
fn to() -> RelationDef {
Relation::ActiveListing.def()
}
}

impl Related<super::datacenter::Entity> for Entity {
fn to() -> RelationDef {
Relation::Datacenter.def()
Expand All @@ -51,18 +59,15 @@ impl Related<super::list::Entity> for Entity {
}
}

impl Related<super::sale_history::Entity> for Entity {
impl Related<super::retainer::Entity> for Entity {
fn to() -> RelationDef {
Relation::SaleHistory.def()
Relation::Retainer.def()
}
}

impl Related<super::retainer::Entity> for Entity {
impl Related<super::sale_history::Entity> for Entity {
fn to() -> RelationDef {
super::active_listing::Relation::Retainer.def()
}
fn via() -> Option<RelationDef> {
Some(super::active_listing::Relation::World.def().rev())
Relation::SaleHistory.def()
}
}

Expand Down
9 changes: 5 additions & 4 deletions ultros-db/src/listings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl UltrosDb {
_ => None,
})
.map(|listing| async move {
active_listing::Entity::delete_by_id((listing.id, listing.timestamp))
active_listing::Entity::delete_by_id(listing.id)
.exec(&self.db)
.await
.map(|_| listing)
Expand Down Expand Up @@ -292,9 +292,10 @@ impl UltrosDb {
});
let (added, _removed_result) =
futures::future::join(futures::future::join_all(added), async move {
let remove_result = futures::future::try_join_all(remove_iter.map(|(l, _)| {
active_listing::Entity::delete_by_id((l.id, l.timestamp)).exec(&self.db)
}))
let remove_result = futures::future::try_join_all(
remove_iter
.map(|(l, _)| active_listing::Entity::delete_by_id(l.id).exec(&self.db)),
)
.await?;
Result::<usize>::Ok(remove_result.len())
})
Expand Down
8 changes: 7 additions & 1 deletion ultros-db/src/recently_updated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ impl UltrosDb {
match updated {
Ok(_updated) => {}
Err(_e) => {
model.insert(&self.db).await?;
match model.clone().insert(&self.db).await {
Ok(ok) => {}
Err(e) => {
// ok now can we update?
model.clone().update(&self.db).await?;
}
}
}
}
Ok(())
Expand Down
13 changes: 11 additions & 2 deletions ultros-db/src/sales.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,21 @@ impl UltrosDb {
let buyer = match buyer {
Some(buyer) => buyer,
None => {
unknown_final_fantasy_character::ActiveModel {
let result = unknown_final_fantasy_character::ActiveModel {
name: ActiveValue::Set(name.to_string()),
..Default::default()
}
.insert(&self.db)
.await?
.await;
match result {
Ok(m) => m,
// the most common error here is a duplicate key, in this case we can just look them up now.
Err(e) => unknown_final_fantasy_character::Entity::find()
.filter(unknown_final_fantasy_character::Column::Name.eq(name))
.one(&self.db)
.await?
.ok_or(e)?,
}
}
};
Ok::<_, anyhow::Error>((buyer.name.clone(), buyer))
Expand Down

0 comments on commit 8e39d24

Please sign in to comment.