Skip to main content

fluxpro_engine/db_models/
process_def_db.rs

1//! Stored definition rows and conversion back to workflow model types.
2
3use crate::models::process_def::ProcessDefinition;
4use crate::models::process_def_error::CreateProcessError;
5use chrono::{DateTime, Utc};
6use log::error;
7use serde_json::Value;
8use sqlx::FromRow;
9use uuid::Uuid;
10
11/// Database row containing a compiled definition and version metadata.
12#[derive(Debug, FromRow, PartialEq)]
13pub struct ProcessDefDb {
14    /// Database row identity.
15    pub uuid: Uuid,
16    /// UTC time when the row was created.
17    pub created_at: DateTime<Utc>,
18    /// Process definition key stored under the SQL column name.
19    pub key_: String,
20    /// Normalized process definition version.
21    pub version: String,
22    /// Insertion-order index for versions of the same process key.
23    pub index_id: i32,
24    /// Stored lifecycle status of this definition version.
25    pub status: String,
26    /// UTC time from which this definition can be selected.
27    pub effective_from: DateTime<Utc>,
28    /// UTC time after which this definition is excluded from runtime selection.
29    pub deprecated_at: Option<DateTime<Utc>>,
30    /// Human-readable notes supplied by the version metadata.
31    pub version_comment: Option<String>,
32    /// Compiled workflow definition as JSON.
33    pub definition: Value,
34}
35
36impl TryInto<ProcessDefinition> for ProcessDefDb {
37    type Error = CreateProcessError;
38
39    fn try_into(self) -> Result<ProcessDefinition, Self::Error> {
40        let full_def: ProcessDefinition = serde_json::from_value(self.definition).map_err(|e| {
41            error!(
42                "Error while converting definition to process definition: {}",
43                e
44            );
45            CreateProcessError::FullDefinitionNotValid
46        })?;
47        let with_uuid = ProcessDefinition {
48            uuid: Some(self.uuid),
49            ..full_def
50        };
51        Ok(with_uuid)
52    }
53}