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
#![allow(clippy::type_complexity)]

use std::borrow::Cow;
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::fs::{File, ReadDir};
use std::io::{BufRead, BufReader, Write};
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};
use std::{error, fmt, fs, io};

pub const MIGRATION_FILE_UP: &str = "up.cql";
pub const MIGRATION_FILE_DOWN: &str = "down.cql";

const COMMENT_LENGTH: usize = 2;
const COMMENT_LINE_TYPE_1: &str = "--";
const COMMENT_LINE_TYPE_2: &str = "//";
const QUERIES_SEPARATOR: char = ';';

#[derive(Debug)]
pub enum Error {
    ParseMigrationFile(String),
    Store(Box<dyn error::Error>),
    Io(io::Error),
}

impl error::Error for Error {}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::ParseMigrationFile(ref err) => f.write_str(err),
            Error::Store(ref e) => e.fmt(f),
            Error::Io(ref e) => e.fmt(f),
        }
    }
}

impl From<io::Error> for Error {
    fn from(err: io::Error) -> Self {
        Error::Io(err)
    }
}

pub type Result<T> = std::result::Result<T, Error>;

pub trait MigrationRow {
    fn id(&self) -> u64;
    fn is_up(&self) -> bool;
}

pub trait Store {
    type Row: MigrationRow;
    type Error: std::error::Error + 'static;

    fn get_all(&self) -> std::result::Result<Option<Vec<Self::Row>>, Self::Error>;
    fn add(&self, id: u64, up: bool) -> std::result::Result<(), Self::Error>;
    fn exec(&self, q: &str) -> std::result::Result<(), Self::Error>;
}

pub fn create_migration<P, Q>(
    name: &str,
    migrations_dir: P,
    q_up: Q,
    q_down: Q,
) -> std::io::Result<PathBuf>
where
    P: AsRef<Path>,
    Q: AsRef<[u8]>,
{
    let unix_timestamp = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .expect("get unix timestamp");
    let migration_path =
        migrations_dir
            .as_ref()
            .join(format!("{}_{}", unix_timestamp.as_secs(), name));
    fs::create_dir_all(&migration_path)?;
    create_migration_file(migration_path.join(MIGRATION_FILE_UP), Some(q_up.as_ref()))?;
    create_migration_file(
        migration_path.join(MIGRATION_FILE_DOWN),
        Some(q_down.as_ref()),
    )?;
    Ok(migration_path)
}

fn create_migration_file(path: PathBuf, q: Option<&[u8]>) -> std::io::Result<()> {
    let mut f = fs::File::create(path)?;
    if let Some(bytes) = q {
        f.write_all(bytes)?;
    }
    f.sync_all()?;
    Ok(())
}

pub struct Migrator<'a, S> {
    path: Cow<'a, Path>,
    store: S,
}

impl<'a, S> Migrator<'a, S>
where
    S: Store,
{
    pub fn with_store<P>(path: P, store: S) -> Self
    where
        P: Into<Cow<'a, Path>>,
    {
        Migrator {
            path: path.into(),
            store,
        }
    }

    #[inline]
    fn migrate_n(&self, up: bool, n: Option<usize>) -> Result<Option<u64>> {
        // Try to read migrations dir first
        let dir = fs::read_dir(&self.path)?;

        let migration_history = self.get_migration_history()?;
        match self.filter_migrations(dir, migration_history, up)? {
            Some(migrations_to_execute) => self.execute_migrations(migrations_to_execute, up, n),
            None => Ok(None),
        }
    }

    /// Migrates up,
    /// returns None if database is already up to date.
    pub fn migrate_up(&self) -> Result<Option<u64>> {
        self.migrate_n(true, None)
    }

    /// Migrates down,
    /// returns None if database is already up to date.
    pub fn migrate_down(&self) -> Result<Option<u64>> {
        self.migrate_n(false, None)
    }

    /// Migrates up `n` times or less,
    /// returns None if database is already up to date.
    pub fn migrate_up_n(&self, n: usize) -> Result<Option<u64>> {
        self.migrate_n(true, Some(n))
    }

    /// Migrates down `n` times or less,
    /// returns None if database is already up to date.
    pub fn migrate_down_n(&self, n: usize) -> Result<Option<u64>> {
        self.migrate_n(false, Some(n))
    }

    fn get_migration_history(&self) -> Result<HashMap<u64, isize>> {
        let res: HashMap<u64, isize> = match self
            .store
            .get_all()
            .map_err(|err| Error::Store(Box::new(err)))?
        {
            Some(migrations) => migrations.into_iter().fold(HashMap::new(), |mut acc, m| {
                let increment = if m.is_up() { 1 } else { -1 };
                match acc.entry(m.id()) {
                    Entry::Occupied(o) => {
                        *o.into_mut() += increment;
                    }
                    Entry::Vacant(v) => {
                        v.insert(increment);
                    }
                }
                acc
            }),
            None => HashMap::new(),
        };
        Ok(res)
    }

    fn parse_cql_file(path: PathBuf) -> Result<Option<Vec<String>>> {
        let file = File::open(path)?;

        let mut queries = Vec::new();
        let mut reader = BufReader::new(file);
        let mut bytes_count: usize;
        let mut buf = String::new();
        let mut is_new_query = false;
        loop {
            bytes_count = reader.read_line(&mut buf)?;
            if bytes_count == 0 {
                break;
            }

            let trimmed = buf.trim();
            if !trimmed.is_empty() && !is_cql_comment_line(trimmed) {
                if is_new_query {
                    queries.push(String::new());
                }
                if trimmed.chars().last().unwrap() == QUERIES_SEPARATOR {
                    is_new_query = true
                } else {
                    is_new_query = false
                }

                if queries.is_empty() {
                    queries.push(trimmed.to_string());
                } else {
                    queries.last_mut().unwrap().push_str(trimmed);
                }
            }

            buf.clear();
        }

        if queries.is_empty() {
            return Ok(None);
        }
        Ok(Some(queries))
    }

    fn filter_migrations(
        &self,
        dir: ReadDir,
        history: HashMap<u64, isize>,
        up: bool,
    ) -> Result<Option<Vec<(u64, Vec<String>)>>> {
        let mut res: Vec<(u64, Vec<String>)> = dir
            .map(|r| r.unwrap())
            .filter(|elem| elem.metadata().unwrap().is_dir())
            .filter_map(
                |elem| match elem.file_name().to_str().unwrap().splitn(2, '_').next() {
                    Some(timestamp_prefix) => match timestamp_prefix.parse::<u64>() {
                        Ok(timestamp) => {
                            let counter = *history.get(&timestamp).unwrap_or(&0);
                            if up && counter == 0 || (!up && counter == 1) {
                                let mut up_path = elem.path();
                                if up {
                                    up_path.push(MIGRATION_FILE_UP);
                                } else {
                                    up_path.push(MIGRATION_FILE_DOWN);
                                }
                                Some((timestamp, up_path))
                            } else {
                                None
                            }
                        }
                        Err(_) => None,
                    },
                    None => None,
                },
            )
            .map(|m| {
                let queries = match Self::parse_cql_file(m.1.clone())? {
                    Some(v) => v,
                    None => {
                        return Err(Error::ParseMigrationFile(format!(
                            "no CQL found in {}",
                            m.1.display()
                        )))
                    }
                };

                Ok((m.0, queries))
            })
            .collect::<Result<Vec<(u64, Vec<String>)>>>()?;
        if res.is_empty() {
            return Ok(None);
        }
        if up {
            res.sort_by(|(a_timestamp, _), (b_timestamp, _)| a_timestamp.cmp(&b_timestamp));
        } else {
            res.sort_by(|(a_timestamp, _), (b_timestamp, _)| b_timestamp.cmp(&a_timestamp));
        }
        Ok(Some(res))
    }

    fn migrate_one(
        &self,
        timestamp: u64,
        queries: Vec<String>,
        up: bool,
        add_history: bool,
    ) -> Result<()> {
        for query in queries {
            self.store
                .exec(&query)
                .map_err(|err| Error::Store(Box::new(err)))?;
        }

        if add_history {
            return self
                .store
                .add(timestamp, up)
                .map_err(|err| Error::Store(Box::new(err)));
        }
        Ok(())
    }

    pub fn execute_migrations(
        &self,
        migration_to_execute: Vec<(u64, Vec<String>)>,
        up: bool,
        n: Option<usize>,
    ) -> Result<Option<u64>> {
        let (last_id, take_n) = match n {
            Some(v) => {
                if migration_to_execute.len() > v {
                    (migration_to_execute.get(v).unwrap().0, v)
                } else {
                    (
                        migration_to_execute.last().unwrap().0,
                        migration_to_execute.len(),
                    )
                }
            }
            None => (
                migration_to_execute.last().unwrap().0,
                migration_to_execute.len(),
            ),
        };

        let add_history = up || take_n != migration_to_execute.len();
        for (timestamp, queries) in migration_to_execute.into_iter().take(take_n) {
            self.migrate_one(timestamp, queries, up, add_history)?;
        }

        Ok(Some(last_id))
    }
}

fn is_cql_comment_line(line: &str) -> bool {
    let comment_slice = &line[..COMMENT_LENGTH];
    comment_slice == COMMENT_LINE_TYPE_1 || comment_slice == COMMENT_LINE_TYPE_2
}