(
store: &Store,
object_type: &str,
name: &str,
settings: &StoredSettings,
)
| 3766 | } |
| 3767 | |
| 3768 | async fn save_settings_record( |
| 3769 | store: &Store, |
| 3770 | object_type: &str, |
| 3771 | name: &str, |
| 3772 | settings: &StoredSettings, |
| 3773 | ) -> Result<(), Status> { |
| 3774 | use crate::persistence::WriteCondition; |
| 3775 | |
| 3776 | let payload = serde_json::to_vec(settings) |
| 3777 | .map_err(|e| Status::internal(format!("encode settings payload failed: {e}")))?; |
| 3778 | |
| 3779 | let (id, condition) = if settings.resource_version == 0 { |
| 3780 | // Create new settings (resource_version 0 means never persisted) |
| 3781 | (uuid::Uuid::new_v4().to_string(), WriteCondition::MustCreate) |
| 3782 | } else { |
| 3783 | // Update existing with CAS on the version from when it was loaded |
| 3784 | // Fetch the record to get the stable ID |
| 3785 | let existing = store |
| 3786 | .get_by_name(object_type, name) |
| 3787 | .await |
| 3788 | .map_err(|e| Status::internal(format!("fetch settings for CAS failed: {e}")))? |
| 3789 | .ok_or_else(|| Status::not_found("settings disappeared since load"))?; |
| 3790 | |
| 3791 | ( |
| 3792 | existing.id, |
| 3793 | WriteCondition::MatchResourceVersion(settings.resource_version), |
| 3794 | ) |
| 3795 | }; |
| 3796 | |
| 3797 | // Single-attempt CAS write |
| 3798 | store |
| 3799 | .put_if(object_type, &id, name, &payload, None, condition) |
| 3800 | .await |
| 3801 | .map_err(|e| match e { |
| 3802 | crate::persistence::PersistenceError::Conflict { .. } => { |
| 3803 | Status::aborted("settings were modified concurrently; please retry") |
| 3804 | } |
| 3805 | crate::persistence::PersistenceError::UniqueViolation { .. } => { |
| 3806 | Status::aborted("settings were created concurrently; please retry") |
| 3807 | } |
| 3808 | other => super::persistence_error_to_status(other, "persist settings"), |
| 3809 | })?; |
| 3810 | |
| 3811 | Ok(()) |
| 3812 | } |
| 3813 | |
| 3814 | fn decode_policy_from_global_settings( |
| 3815 | global: &StoredSettings, |
no test coverage detected