fix: use .udpate method, internal unchanged

This commit is contained in:
əlemi 2024-06-13 17:20:50 +02:00
parent 37fa1df9ab
commit e1f1548e7e
Signed by: alemi
GPG key ID: A4895B84D311642C
3 changed files with 13 additions and 27 deletions

View file

@ -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> { pub async fn fix(ctx: upub::Context, likes: bool, shares: bool, replies: bool) -> Result<(), sea_orm::DbErr> {
use futures::TryStreamExt; 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 { for (k, v) in store {
let m = upub::model::object::ActiveModel { let m = upub::model::object::ActiveModel {
internal: sea_orm::Set(k), internal: sea_orm::Unchanged(k),
likes: sea_orm::Set(v), likes: sea_orm::Set(v),
..Default::default() ..Default::default()
}; };
if let Err(e) = upub::model::object::Entity::update(m) if let Err(e) = m.update(db).await {
.exec(db)
.await
{
tracing::warn!("record not updated ({k}): {e}"); 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 { for (k, v) in store {
let m = upub::model::object::ActiveModel { let m = upub::model::object::ActiveModel {
internal: sea_orm::Set(k), internal: sea_orm::Unchanged(k),
announces: sea_orm::Set(v), announces: sea_orm::Set(v),
..Default::default() ..Default::default()
}; };
if let Err(e) = upub::model::object::Entity::update(m) if let Err(e) = m.update(db).await {
.exec(db)
.await
{
tracing::warn!("record not updated ({k}): {e}"); 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 { for (k, v) in store {
let m = upub::model::object::ActiveModel { let m = upub::model::object::ActiveModel {
id: sea_orm::Set(k.clone()), id: sea_orm::Unchanged(k.clone()),
replies: sea_orm::Set(v), replies: sea_orm::Set(v),
..Default::default() ..Default::default()
}; };
if let Err(e) = upub::model::object::Entity::update(m) // TODO will update work with non-primary-key field??
.exec(db) if let Err(e) = m.update(db).await {
.await
{
tracing::warn!("record not updated ({k}): {e}"); tracing::warn!("record not updated ({k}): {e}");
} }
} }

View file

@ -1,5 +1,5 @@
use futures::TryStreamExt; 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; use upub::traits::Fetcher;
pub async fn update_users(ctx: upub::Context, days: i64) -> Result<(), sea_orm::DbErr> { 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 { for (uid, user_model) in insertions {
tracing::info!("updating user {}", uid); tracing::info!("updating user {}", uid);
upub::model::actor::Entity::update(user_model) user_model.update(ctx.db()).await?;
.exec(ctx.db())
.await?;
} }
tracing::info!("updated {count} users"); tracing::info!("updated {count} users");

View file

@ -1,5 +1,5 @@
use apb::{target::Addressed, Activity, Base, Object}; 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}}; use crate::{ext::{AnyQuery, LoggableError}, model, traits::{fetch::Pull, Fetcher, Normalizer}};
#[derive(Debug, thiserror::Error)] #[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()?)?; let mut actor_model = crate::AP::actor_q(object_node.as_actor()?)?;
actor_model.internal = Unchanged(internal_uid); actor_model.internal = Unchanged(internal_uid);
actor_model.updated = Set(chrono::Utc::now()); actor_model.updated = Set(chrono::Utc::now());
crate::model::actor::Entity::update(actor_model) actor_model.update(tx).await?;
.exec(tx)
.await?;
}, },
apb::ObjectType::Note | apb::ObjectType::Document(apb::DocumentType::Page) => { apb::ObjectType::Note | apb::ObjectType::Document(apb::DocumentType::Page) => {
let internal_oid = crate::model::object::Entity::ap_to_internal(&oid, tx) 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)?; let mut object_model = crate::AP::object_q(&object_node)?;
object_model.internal = Unchanged(internal_oid); object_model.internal = Unchanged(internal_oid);
object_model.updated = Set(chrono::Utc::now()); object_model.updated = Set(chrono::Utc::now());
crate::model::object::Entity::update(object_model) object_model.update(tx).await?;
.exec(tx)
.await?;
}, },
_ => return Err(ProcessorError::Unprocessable(activity.id()?.to_string())), _ => return Err(ProcessorError::Unprocessable(activity.id()?.to_string())),
} }