1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
//! Implementation of storing migration state in an [AWS DynamoDB database table][dynamodb].
//!
//! This provides implementations of traits defined in [`migrate_state`]
//!
//! See [`DdbStateLock`] docs for more details.
//!
//! The following cargo features of the crate are exposed:
//!
//! - `native-tls` (enabled by default) - enables `native-tls` feature in dependent `rusoto` crates
//! - `rustls` - enables `rustls` feature in dependent `rusoto` crates
//!
//! [dynamodb]: https://aws.amazon.com/dynamodb/

#![warn(missing_docs, unreachable_pub, rust_2018_idioms)]
// Makes rustc abort compilation if there are any unsafe blocks in the crate.
// Presence of this annotation is picked up by tools such as cargo-geiger
// and lets them ensure that there is indeed no unsafe code as opposed to
// something they couldn't detect (e.g. unsafe added via macro expansion, etc).
#![forbid(unsafe_code)]

use async_trait::async_trait;
use migrate_state::{Result, StateClient, StateGuard, StateLock};
use rusoto_dynamodb::DynamoDb;
use std::{collections::HashMap, iter};

/// Builder for [`DdbStateLock`] object, see its methods for available configurations.
/// To finish building the object call [`build()`](DdbStateLockBuilder::build) method.
pub struct DdbStateLockBuilder(DdbStateCtx);

impl DdbStateLockBuilder {
    /// Override partition key attribute name used for the stored migration state record.
    ///
    /// Default: `"partition_key"`
    pub fn partition_key_attr_name(&mut self, name: impl Into<String>) -> &mut Self {
        self.0.partition_key_attr.name = name.into();
        self
    }

    /// Override partition key attribute value used for stored migration state record.
    ///
    /// Default: `"migrate-state"` (string DynamoDB type)
    pub fn partition_key_attr_val(&mut self, val: rusoto_dynamodb::AttributeValue) -> &mut Self {
        self.0.partition_key_attr.value = val;
        self
    }

    /// Override sort key attribute name used for stored migration state record.
    ///
    /// Default: no sort key attribute is added to the record.
    /// If [`sort_key_attr_val`](Self::sort_key_attr_val) was not set, then
    /// it's value will be set to `"migrate-state"` (string DynamoDB type)
    pub fn sort_key_attr_name(&mut self, name: impl Into<String>) -> &mut Self {
        match &mut self.0.sort_key_attr {
            Some(it) => it.name = name.into(),
            None => self.0.sort_key_attr = Some(AttrNameVal::new(name, default_key_attr_value())),
        }
        self
    }

    /// Override sort key attribute value used for stored migration state record.
    ///
    /// Default: no sort key attribute is added to the record.
    /// If [`sort_key_attr_name`](Self::sort_key_attr_name) was not set, then
    /// it's value will be set to `"sort_key"`
    pub fn sort_key_attr_val(&mut self, val: rusoto_dynamodb::AttributeValue) -> &mut Self {
        match &mut self.0.sort_key_attr {
            Some(it) => it.value = val,
            None => self.0.sort_key_attr = Some(AttrNameVal::new("sort_key", val)),
        }
        self
    }

    /// Override payload attribute name used for stored migration state record.
    ///
    /// Default: `"payload"`
    pub fn payload_attr_name(&mut self, name: impl Into<String>) -> &mut Self {
        self.0.payload_attr_name = name.into();
        self
    }

    /// Consume the builder and return final configured [`DdbStateLock`] object
    pub fn build(self) -> DdbStateLock {
        DdbStateLock(self.0)
    }
}

fn default_key_attr_value() -> rusoto_dynamodb::AttributeValue {
    rusoto_dynamodb::AttributeValue {
        s: Some("migrate-state".into()),
        ..Default::default()
    }
}

/// Implements [`StateLock`] storing migration state in an [AWS DynamoDB database table][dynamodb].
///
/// <pre class="compile_fail" style="white-space:normal;font:inherit;">
///
/// ⚠️ Warning! State locking is not yet implemented, but it is planned to be implemented.
/// Thus, you have to manually ensure you don't run migrations in parallel in the meantime...
///
/// </pre>
///
/// You can configure how and where migration state is stored via [`DdbStateLockBuilder`]
/// which is created via [`DdbStateLock::with_builder()`] (or lower-level [`DdbStateLock::builder()`]).
///
/// In general migration state is stored as a single record with a partition key,
/// an optional sort key and payload attribute of binary array type (payload
/// contains migration state itself).
///
/// Example usage:
///
/// ```no_run
/// use migrate_state_dynamodb::DdbStateLock;
/// use migrate_core::Plan;
///
/// let ddb_client = rusoto_dynamodb::DynamoDbClient::new(rusoto_core::Region::default());
///
/// let mut state_lock = DdbStateLock::with_builder("ddb-table-name", ddb_client, |it| {
///     // Available configurations.
///     // In this example we pass values that are already set by default just to demo
///     it.partition_key_attr_name("partition_key")
///         .sort_key_attr_name("sort_key")
///         .payload_attr_name("payload")
///         // yeah, `rusoto_dynamodb::AttributeValue` API is a bit ugly...
///         .partition_key_attr_val(rusoto_dynamodb::AttributeValue {
///             s: Some("migrate-state".to_owned()),
///             ..Default::default()
///         })
///         .sort_key_attr_val(rusoto_dynamodb::AttributeValue {
///             s: Some("migrate-state".to_owned()),
///             ..Default::default()
///         })
/// });
///
/// let plan = Plan::builder(state_lock);
/// ```
///
/// [dynamodb]: https://aws.amazon.com/dynamodb/
pub struct DdbStateLock(DdbStateCtx);

impl DdbStateLock {
    /// Returns [`DdbStateLockBuilder`] to configure and create an instance of [`DdbStateLock`].
    ///
    /// Takes two required arguments:
    ///
    /// - `table_name` - Name of the DynamoDB table to store state in
    /// - `ddb` - [`DynamoDb`] client implementation to use for all DynamoDB API calls
    pub fn builder(
        table_name: impl Into<String>,
        ddb: impl DynamoDb + Send + Sync + 'static,
    ) -> DdbStateLockBuilder {
        DdbStateLockBuilder(DdbStateCtx {
            partition_key_attr: AttrNameVal::new("partition_key", default_key_attr_value()),
            sort_key_attr: None,
            payload_attr_name: "payload".to_owned(),
            table_name: table_name.into(),
            ddb: Box::new(ddb),
        })
    }

    /// Same as [`DdbStateLock::builder()`], but accepts third argument, which
    /// is a clousure that takes builder to configure it in a single method call chain.
    /// Method exists only for convenience of creating [`DdbStateLock`] in one expression.
    ///
    /// The return value of the closure is ignored, it is intended only for a single
    /// simple method call chain. Use [`DdbStateLock::builder()`] method to implement
    /// more advanced configuration flow.
    ///
    /// ```
    /// # fn run(ddb_client: rusoto_dynamodb::DynamoDbClient) {
    /// use migrate_state_dynamodb::DdbStateLock;
    ///
    /// let state_lock = DdbStateLock::with_builder("table-name", ddb_client, |it| {
    ///     it.partition_key_attr_name("pk")
    ///         .sort_key_attr_name("sk")
    /// });
    /// # }
    /// ```
    ///
    pub fn with_builder(
        table_name: impl Into<String>,
        ddb: impl DynamoDb + Send + Sync + 'static,
        configure: impl FnOnce(&mut DdbStateLockBuilder) -> &mut DdbStateLockBuilder,
    ) -> Self {
        let mut builder = Self::builder(table_name, ddb);
        let _ = configure(&mut builder);
        builder.build()
    }
}

#[async_trait]
impl StateLock for DdbStateLock {
    async fn lock(self: Box<Self>, _force: bool) -> Result<Box<dyn StateGuard>> {
        // FIXME: acquire the distributed lock here

        Ok(Box::new(DdbStateGuard(DdbStateClient(self.0))))
    }
}

struct DdbStateGuard(DdbStateClient);

#[async_trait]
impl StateGuard for DdbStateGuard {
    fn client(&mut self) -> &mut dyn StateClient {
        &mut self.0
    }

    async fn unlock(mut self: Box<Self>) -> Result<()> {
        // FIXME: release the distributed lock here
        // but be cautios not to corrupt the lock if some other
        // subject has acquired it with `force_lock()`.
        // If that is the case, we should just issue a warning
        // and return successfully
        Ok(())
    }
}

struct DdbStateClient(DdbStateCtx);

#[async_trait]
impl StateClient for DdbStateClient {
    async fn fetch(&mut self) -> Result<Vec<u8>> {
        // FIXME: add retries with exponential backoff
        let item = self
            .0
            .ddb
            .get_item(rusoto_dynamodb::GetItemInput {
                key: self.0.to_primary_key(),
                projection_expression: Some(self.0.payload_attr_name.clone()),
                table_name: self.0.table_name.clone(),
                ..Default::default()
            })
            .await
            .map_err(|source| Error::GetItem { source })?
            .item;

        let mut item = match item {
            Some(it) => it,
            None => return Ok(vec![]),
        };

        let mut payload =
            item.remove(&self.0.payload_attr_name)
                .ok_or_else(|| Error::PayloadAttrNotFound {
                    payload_attr_name: self.0.payload_attr_name.clone(),
                })?;

        let payload = payload.b.take().ok_or(Error::UnexpectedPayloadType {
            actual_value: payload,
        })?;

        Ok(payload.to_vec())
    }

    async fn update(&mut self, state: Vec<u8>) -> Result<()> {
        let state = rusoto_dynamodb::AttributeValue {
            b: Some(state.into()),
            ..Default::default()
        };
        let update_expression = "SET #p = :p";
        let attr_names = iter::once(("#p".to_owned(), self.0.payload_attr_name.clone()));
        let attr_values = iter::once((":p".to_owned(), state));

        self.0
            .ddb
            .update_item(rusoto_dynamodb::UpdateItemInput {
                expression_attribute_names: Some(attr_names.collect()),
                expression_attribute_values: Some(attr_values.collect()),
                key: self.0.to_primary_key(),
                table_name: self.0.table_name.clone(),
                update_expression: Some(update_expression.to_owned()),
                ..Default::default()
            })
            .await
            .map_err(|source| Error::UpdateItem { source })?;

        Ok(())
    }
}

#[derive(Clone)]
struct AttrNameVal {
    name: String,
    value: rusoto_dynamodb::AttributeValue,
}

impl AttrNameVal {
    fn new(name: impl Into<String>, value: rusoto_dynamodb::AttributeValue) -> Self {
        Self {
            name: name.into(),
            value,
        }
    }
}

struct DdbStateCtx {
    partition_key_attr: AttrNameVal,
    sort_key_attr: Option<AttrNameVal>,
    payload_attr_name: String,
    table_name: String,
    ddb: Box<dyn DynamoDb + Send + Sync>,
}

impl DdbStateCtx {
    fn to_primary_key(&self) -> HashMap<String, rusoto_dynamodb::AttributeValue> {
        let partition_key = (
            self.partition_key_attr.name.clone(),
            self.partition_key_attr.value.clone(),
        );
        let sort_key = self
            .sort_key_attr
            .clone()
            .map(|attr| (attr.name, attr.value));

        iter::once(partition_key).chain(sort_key).collect()
    }
}

#[derive(Debug, thiserror::Error)]
enum Error {
    #[error("dynamodb update_item operation failed when updating migration state")]
    UpdateItem {
        source: rusoto_core::RusotoError<rusoto_dynamodb::UpdateItemError>,
    },

    #[error("dynamodb get_item operation failed when fetching migration state")]
    GetItem {
        source: rusoto_core::RusotoError<rusoto_dynamodb::GetItemError>,
    },

    #[error(
        "the returned migration state item doesn't contain \
        payload attribute `{payload_attr_name}`"
    )]
    PayloadAttrNotFound { payload_attr_name: String },

    #[error(
        "the returned migration state item's payload is not \
        binary array type, actual value: {actual_value:?}"
    )]
    UnexpectedPayloadType {
        actual_value: rusoto_dynamodb::AttributeValue,
    },
}

#[cfg(test)]
mod tests {
    use super::*;

    // TODO: spin localstack or local dynamodb docker container to test this crate
    #[tokio::test]
    #[ignore]
    async fn smoke_test() {
        let lock = DdbStateLock::builder(
            "veetaha-sandbox",
            rusoto_dynamodb::DynamoDbClient::new(Default::default()),
        )
        .build();

        migrate_state_test::storage(Box::new(lock)).await;
    }
}