From e1f1548e7e1a850d61e954b8ba100ade20fb4179 Mon Sep 17 00:00:00 2001 From: alemi Date: Thu, 13 Jun 2024 17:20:50 +0200 Subject: [PATCH] fix: use .udpate method, internal unchanged --- upub/cli/src/fix.rs | 24 ++++++++---------------- upub/cli/src/update.rs | 6 ++---- upub/core/src/traits/process.rs | 10 +++------- 3 files changed, 13 insertions(+), 27 deletions(-) diff --git a/upub/cli/src/fix.rs b/upub/cli/src/fix.rs index d7b8394a..5a1b5c82 100644 --- a/upub/cli/src/fix.rs +++ b/upub/cli/src/fix.rs @@ -1,4 +1,4 @@ -use sea_orm::EntityTrait; +use sea_orm::{ActiveModelTrait, EntityTrait}; pub async fn fix(ctx: upub::Context, likes: bool, shares: bool, replies: bool) -> Result<(), sea_orm::DbErr> { use futures::TryStreamExt; @@ -16,14 +16,11 @@ pub async fn fix(ctx: upub::Context, likes: bool, shares: bool, replies: bool) - for (k, v) in store { let m = upub::model::object::ActiveModel { - internal: sea_orm::Set(k), + internal: sea_orm::Unchanged(k), likes: sea_orm::Set(v), ..Default::default() }; - if let Err(e) = upub::model::object::Entity::update(m) - .exec(db) - .await - { + if let Err(e) = m.update(db).await { tracing::warn!("record not updated ({k}): {e}"); } } @@ -41,14 +38,11 @@ pub async fn fix(ctx: upub::Context, likes: bool, shares: bool, replies: bool) - for (k, v) in store { let m = upub::model::object::ActiveModel { - internal: sea_orm::Set(k), + internal: sea_orm::Unchanged(k), announces: sea_orm::Set(v), ..Default::default() }; - if let Err(e) = upub::model::object::Entity::update(m) - .exec(db) - .await - { + if let Err(e) = m.update(db).await { tracing::warn!("record not updated ({k}): {e}"); } } @@ -69,14 +63,12 @@ pub async fn fix(ctx: upub::Context, likes: bool, shares: bool, replies: bool) - for (k, v) in store { let m = upub::model::object::ActiveModel { - id: sea_orm::Set(k.clone()), + id: sea_orm::Unchanged(k.clone()), replies: sea_orm::Set(v), ..Default::default() }; - if let Err(e) = upub::model::object::Entity::update(m) - .exec(db) - .await - { + // TODO will update work with non-primary-key field?? + if let Err(e) = m.update(db).await { tracing::warn!("record not updated ({k}): {e}"); } } diff --git a/upub/cli/src/update.rs b/upub/cli/src/update.rs index d0225860..a682af68 100644 --- a/upub/cli/src/update.rs +++ b/upub/cli/src/update.rs @@ -1,5 +1,5 @@ use futures::TryStreamExt; -use sea_orm::{ActiveValue::{Unchanged, Set}, ColumnTrait, EntityTrait, QueryFilter}; +use sea_orm::{ActiveModelTrait, ActiveValue::{Set, Unchanged}, ColumnTrait, EntityTrait, QueryFilter}; use upub::traits::Fetcher; pub async fn update_users(ctx: upub::Context, days: i64) -> Result<(), sea_orm::DbErr> { @@ -33,9 +33,7 @@ pub async fn update_users(ctx: upub::Context, days: i64) -> Result<(), sea_orm:: for (uid, user_model) in insertions { tracing::info!("updating user {}", uid); - upub::model::actor::Entity::update(user_model) - .exec(ctx.db()) - .await?; + user_model.update(ctx.db()).await?; } tracing::info!("updated {count} users"); diff --git a/upub/core/src/traits/process.rs b/upub/core/src/traits/process.rs index 6f21e5a4..3de14a51 100644 --- a/upub/core/src/traits/process.rs +++ b/upub/core/src/traits/process.rs @@ -1,5 +1,5 @@ use apb::{target::Addressed, Activity, Base, Object}; -use sea_orm::{sea_query::Expr, ActiveValue::{NotSet, Set, Unchanged}, ColumnTrait, Condition, DatabaseTransaction, EntityTrait, QueryFilter, QuerySelect, SelectColumns}; +use sea_orm::{sea_query::Expr, ActiveModelTrait, ActiveValue::{NotSet, Set, Unchanged}, ColumnTrait, Condition, DatabaseTransaction, EntityTrait, QueryFilter, QuerySelect, SelectColumns}; use crate::{ext::{AnyQuery, LoggableError}, model, traits::{fetch::Pull, Fetcher, Normalizer}}; #[derive(Debug, thiserror::Error)] @@ -268,9 +268,7 @@ pub async fn update(ctx: &crate::Context, activity: impl apb::Activity, tx: &Dat let mut actor_model = crate::AP::actor_q(object_node.as_actor()?)?; actor_model.internal = Unchanged(internal_uid); actor_model.updated = Set(chrono::Utc::now()); - crate::model::actor::Entity::update(actor_model) - .exec(tx) - .await?; + actor_model.update(tx).await?; }, apb::ObjectType::Note | apb::ObjectType::Document(apb::DocumentType::Page) => { let internal_oid = crate::model::object::Entity::ap_to_internal(&oid, tx) @@ -279,9 +277,7 @@ pub async fn update(ctx: &crate::Context, activity: impl apb::Activity, tx: &Dat let mut object_model = crate::AP::object_q(&object_node)?; object_model.internal = Unchanged(internal_oid); object_model.updated = Set(chrono::Utc::now()); - crate::model::object::Entity::update(object_model) - .exec(tx) - .await?; + object_model.update(tx).await?; }, _ => return Err(ProcessorError::Unprocessable(activity.id()?.to_string())), }