use serde_json::Value;
use tokio_postgres::GenericClient;
use crate::error::ForceSyncError;
use super::PgStore;
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Debug, Clone, PartialEq)]
pub struct SyncConflict {
pub tenant: String,
pub object_name: String,
pub external_id: String,
pub field_name: String,
pub left_value: Value,
pub right_value: Value,
pub resolution: Option<String>,
}
async fn insert_conflict_query<C>(
client: &C,
conflict: &SyncConflict,
) -> Result<i64, ForceSyncError>
where
C: GenericClient + Sync + ?Sized,
{
let row = client
.query_one(
"insert into sync_conflict (
tenant,
object_name,
external_id,
field_name,
left_value,
right_value,
resolution
) values ($1, $2, $3, $4, $5::jsonb, $6::jsonb, $7)
returning conflict_id",
&[
&conflict.tenant,
&conflict.object_name,
&conflict.external_id,
&conflict.field_name,
&conflict.left_value,
&conflict.right_value,
&conflict.resolution,
],
)
.await?;
Ok(row.get(0))
}
impl PgStore {
pub async fn insert_conflict(&self, conflict: &SyncConflict) -> Result<i64, ForceSyncError> {
let client = self.pool().get().await?;
insert_conflict_query(&**client, conflict).await
}
}