Owner-authorized recovery of accumulated source changes.
Includes accepted work and in-progress changes; NOT a release or acceptance.
Parent baseline 1b6db09bd1. Runtime data, build caches and local credentials excluded.
92 lines
3.8 KiB
Rust
92 lines
3.8 KiB
Rust
//! Архив Resource (Story 3.7 AC05): exact CAS и lifecycle-переход в одной транзакции.
|
|
//!
|
|
//! Вынесен из `postgres/resource.rs`, чтобы файл оставался в пределах
|
|
//! rust-code-health; поведение и SQL не менялись. Меняются только
|
|
//! `status`/`updated_at` строки `resources`: уже созданные publications, их owning
|
|
//! refs и блобы не трогаются, поэтому точные Published Version остаются читаемыми
|
|
//! и неизменными.
|
|
|
|
use crank_core::resource::{ResourceError, ResourceLifecycle, ResourceLifecycleAction};
|
|
use sqlx::Row;
|
|
|
|
use crate::model::resource::{ArchiveResourceRequest, ResourceDraftRecord};
|
|
|
|
use super::super::PostgresRegistry;
|
|
use super::{integrity, map_write_error, parse_status, read_draft, truncate_to_micros};
|
|
|
|
impl PostgresRegistry {
|
|
/// Архив Resource: меняется только lifecycle/ревизия карточки.
|
|
pub async fn archive_resource(
|
|
&self,
|
|
request: ArchiveResourceRequest,
|
|
) -> Result<ResourceDraftRecord, ResourceError> {
|
|
let mut transaction = self
|
|
.pool()
|
|
.begin()
|
|
.await
|
|
.map_err(|_| ResourceError::StorageUnavailable)?;
|
|
|
|
let row = sqlx::query(
|
|
"select status, updated_at
|
|
from resources
|
|
where workspace_id = $1 and resource_id = $2
|
|
for update",
|
|
)
|
|
.bind(request.workspace_id.as_str())
|
|
.bind(request.resource_id.as_str())
|
|
.fetch_optional(&mut *transaction)
|
|
.await
|
|
.map_err(|_| ResourceError::StorageUnavailable)?
|
|
.ok_or(ResourceError::NotFound)?;
|
|
|
|
let recorded_updated_at: time::OffsetDateTime = row
|
|
.try_get("updated_at")
|
|
.map_err(|_| ResourceError::ArtifactIntegrity)?;
|
|
// F: точный precondition. Старый CAS обязан победить lifecycle-отказ: повтор
|
|
// архива прежней карточкой — stale (428), а не «уже архивирован».
|
|
if recorded_updated_at != request.expected_updated_at {
|
|
return Err(ResourceError::Stale);
|
|
}
|
|
let status = parse_status(&row.try_get::<String, _>("status").map_err(integrity)?)?;
|
|
// Archived -> Archived недопустим: актуальный CAS на уже архивном Resource
|
|
// даёт sanitized lifecycle-отказ (409), без 500 и без mutation.
|
|
ResourceLifecycle::transition(status, ResourceLifecycleAction::Archive)?;
|
|
|
|
let archived_at = truncate_to_micros(request.archived_at);
|
|
let next_updated_at = if archived_at > recorded_updated_at {
|
|
archived_at
|
|
} else {
|
|
recorded_updated_at + time::Duration::microseconds(1)
|
|
};
|
|
let updated = sqlx::query(
|
|
"update resources
|
|
set status = 'archived', updated_at = $3
|
|
where workspace_id = $1 and resource_id = $2 and updated_at = $4",
|
|
)
|
|
.bind(request.workspace_id.as_str())
|
|
.bind(request.resource_id.as_str())
|
|
.bind(next_updated_at)
|
|
.bind(recorded_updated_at)
|
|
.execute(&mut *transaction)
|
|
.await
|
|
.map_err(map_write_error)?
|
|
.rows_affected();
|
|
if updated != 1 {
|
|
return Err(ResourceError::Stale);
|
|
}
|
|
|
|
let record = read_draft(
|
|
&mut transaction,
|
|
&request.workspace_id,
|
|
&request.resource_id,
|
|
)
|
|
.await?
|
|
.ok_or(ResourceError::ArtifactIntegrity)?;
|
|
transaction
|
|
.commit()
|
|
.await
|
|
.map_err(|_| ResourceError::StorageUnavailable)?;
|
|
Ok(record)
|
|
}
|
|
}
|