zeebe_rs/process_instance/
migrate.rs

1use crate::Client;
2use crate::ClientError;
3use crate::proto;
4
5#[derive(Debug, Clone)]
6pub struct Initial;
7
8#[derive(Debug, Clone)]
9pub struct WithProcessInstance;
10
11#[derive(Debug, Clone)]
12pub struct WithVariables;
13
14pub trait MigrateProcessInstanceState {}
15impl MigrateProcessInstanceState for Initial {}
16impl MigrateProcessInstanceState for WithProcessInstance {}
17impl MigrateProcessInstanceState for WithVariables {}
18
19/// Instructions for mapping elements between process definitions
20#[derive(Debug, Clone)]
21pub struct MappingInstruction {
22    source_element_id: String,
23    target_element_id: String,
24}
25
26/// Plan defining how to migrate a process instance
27#[derive(Debug, Clone)]
28pub struct MigrationPlan {
29    target_process_definition_key: i64,
30    mapping_instructions: Vec<MappingInstruction>,
31}
32
33/// Request to migrate a process instance to a different process definition
34/// # Examples
35/// ```ignore
36/// client
37///     .migrate_process_instance()
38///     .with_process_instance_key(12356)
39///     .without_migration_plan()
40///     .send()
41///     .await?;
42/// ```
43#[derive(Debug, Clone)]
44pub struct MigrateProcessInstanceRequest<T: MigrateProcessInstanceState> {
45    client: Client,
46    process_instance_key: i64,
47    migration_plan: Option<MigrationPlan>,
48    operation_reference: Option<u64>,
49    _state: std::marker::PhantomData<T>,
50}
51
52impl<T: MigrateProcessInstanceState> MigrateProcessInstanceRequest<T> {
53    pub(crate) fn new(client: Client) -> MigrateProcessInstanceRequest<Initial> {
54        MigrateProcessInstanceRequest {
55            client,
56            process_instance_key: 0,
57            migration_plan: None,
58            operation_reference: None,
59            _state: std::marker::PhantomData,
60        }
61    }
62
63    fn transition<NewState: MigrateProcessInstanceState>(
64        self,
65    ) -> MigrateProcessInstanceRequest<NewState> {
66        MigrateProcessInstanceRequest {
67            client: self.client,
68            process_instance_key: self.process_instance_key,
69            migration_plan: self.migration_plan,
70            operation_reference: self.operation_reference,
71            _state: std::marker::PhantomData,
72        }
73    }
74}
75
76impl MigrateProcessInstanceRequest<Initial> {
77    /// Sets the process instance key identifying which instance to migrate
78    ///
79    /// # Arguments
80    ///
81    /// * `process_instance_key` - The key of the process instance to migrate
82    ///
83    /// # Returns
84    ///
85    /// The updated MigrateProcessInstanceRequest in the WithProcessInstance state
86    pub fn with_process_instance_key(
87        mut self,
88        process_instance_key: i64,
89    ) -> MigrateProcessInstanceRequest<WithProcessInstance> {
90        self.process_instance_key = process_instance_key;
91        self.transition()
92    }
93}
94
95impl MigrateProcessInstanceRequest<WithProcessInstance> {
96    /// Sets the migration plan with target process and element mappings
97    ///
98    /// # Arguments
99    ///
100    /// * `target_process_definition_key` - The key of the target process definition
101    /// * `mapping_instructions` - The instructions for mapping elements between processes
102    ///
103    /// # Returns
104    ///
105    /// The updated MigrateProcessInstanceRequest in the WithVariables state
106    pub fn with_migration_plan(
107        mut self,
108        target_process_definition_key: i64,
109        mapping_instructions: Vec<MappingInstruction>,
110    ) -> MigrateProcessInstanceRequest<WithVariables> {
111        self.migration_plan = Some(MigrationPlan {
112            target_process_definition_key,
113            mapping_instructions,
114        });
115
116        self.transition()
117    }
118
119    /// Continues without specifying a migration plan
120    ///
121    /// # Returns
122    ///
123    /// The updated MigrateProcessInstanceRequest in the WithVariables state
124    pub fn without_migration_plan(self) -> MigrateProcessInstanceRequest<WithVariables> {
125        self.transition()
126    }
127}
128
129impl MigrateProcessInstanceRequest<WithVariables> {
130    /// Sends the process instance migration request to the Zeebe workflow engine
131    ///
132    /// # Returns
133    ///
134    /// A Result containing the MigrateProcessInstanceResponse or a ClientError
135    pub async fn send(mut self) -> Result<MigrateProcessInstanceResponse, ClientError> {
136        let res = self
137            .client
138            .gateway_client
139            .migrate_process_instance(proto::MigrateProcessInstanceRequest {
140                process_instance_key: self.process_instance_key,
141                migration_plan: self.migration_plan.map(|p| {
142                    proto::migrate_process_instance_request::MigrationPlan {
143                        target_process_definition_key: p.target_process_definition_key,
144                        mapping_instructions: p
145                            .mapping_instructions
146                            .into_iter()
147                            .map(
148                                |i| proto::migrate_process_instance_request::MappingInstruction {
149                                    source_element_id: i.source_element_id,
150                                    target_element_id: i.target_element_id,
151                                },
152                            )
153                            .collect(),
154                    }
155                }),
156                operation_reference: self.operation_reference,
157            })
158            .await?;
159
160        Ok(res.into_inner().into())
161    }
162
163    /// Sets a reference key for tracking this operation
164    ///
165    /// # Arguments
166    ///
167    /// * `operation_reference` - The reference key for tracking the operation
168    ///
169    /// # Returns
170    ///
171    /// The updated MigrateProcessInstanceRequest with the operation reference set
172    pub fn with_operation_reference(mut self, operation_reference: u64) -> Self {
173        self.operation_reference = Some(operation_reference);
174        self
175    }
176}
177
178/// Response from migrating a process instance
179#[derive(Debug, Clone)]
180pub struct MigrateProcessInstanceResponse {}
181
182impl From<proto::MigrateProcessInstanceResponse> for MigrateProcessInstanceResponse {
183    fn from(_value: proto::MigrateProcessInstanceResponse) -> MigrateProcessInstanceResponse {
184        MigrateProcessInstanceResponse {}
185    }
186}