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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#![recursion_limit = "1024"]
#[macro_use]
extern crate error_chain;
extern crate rpassword;
extern crate rustc_serialize;
extern crate chrono;
extern crate walkdir;

pub mod errors {
    error_chain! { }
}

use errors::*;
use std::collections::HashMap;
use std::process::Command;
use std::io::{self, Write};
use std::path::PathBuf;
use std::fs;

use rpassword::read_password;
use rustc_serialize::json;
use walkdir::WalkDir;
use chrono::TimeZone;


static META_FILE: &'static str = ".migrant";
static DT_FORMAT: &'static str = "%Y%m%d-%H%M%S";


#[derive(RustcEncodable, RustcDecodable, Debug, Clone)]
/// Settings that are serialized and saved in a project `.migrant` file
pub struct Settings {
    database_type: String,
    database_name: String,
    db_user: String,
    password: String,
    migration_folder: String,
    applied: Vec<String>,
    down: Vec<String>,
}
impl Settings {
    fn new(db_type: String, db_name: String, db_user: String, password: String) -> Settings {
        Settings {
            database_type: db_type,
            database_name: db_name,
            db_user: db_user,
            password: password,
            migration_folder: "resources/migrations".to_string(),
            applied: vec![],
            down: vec![],
        }
    }
}


#[derive(Debug)]
/// Migration meta data
struct Migration {
    stamp: chrono::DateTime<chrono::UTC>,
    up: PathBuf,
    down: PathBuf,
}
impl Migration {
    fn new(up: PathBuf, down: PathBuf, stamp: chrono::DateTime<chrono::UTC>) -> Migration {
        Migration {
            stamp: stamp,
            up: up,
            down: down,
        }
    }
}


fn runner(settings: &Settings, meta_path: &PathBuf, filename: &str) -> Result<std::process::Output> {
    Ok(match settings.database_type.as_ref() {
        "sqlite" => {
            let mut db_path = meta_path.clone();
            db_path.pop();
            db_path.push(&settings.database_name);
            Command::new("sqlite3")
                    .arg(db_path.to_str().unwrap())
                    .arg(&format!(".read {}", filename))
                    .output()
                    .chain_err(|| "failed to run migration command")?
        }
        "pg" => {
            Command::new("psql")
                    .arg("-U").arg(&settings.db_user)
                    .arg("-d").arg(&settings.database_name)
                    .arg("-h").arg("localhost")
                    .arg("-f").arg(&filename)
                    .output()
                    .chain_err(|| "failed to run migration command")?
        }
        _ => unreachable!(),
    })
}


/// Display a prompt and return the entered response
fn prompt(msg: &str, secure: bool) -> String {
    print!("{}", msg);
    let _ = io::stdout().flush();

    if secure {
        read_password().unwrap()
    } else {
        let stdin = io::stdin();
        let mut resp = String::new();
        let _ = stdin.read_line(&mut resp);
        resp.trim().to_string()
    }
}


/// Search for a .migrant file in the current directory,
/// looking up the parent path until it finds one.
pub fn search_for_meta(dir: &PathBuf, parents: u32) -> Option<PathBuf> {
    let mut dir = dir.clone();
    let mut count = 0;
    loop {
        for path in fs::read_dir(&dir).unwrap() {
            let path = path.unwrap().path();
            if let Some(file) = path.file_name() {
                if file == META_FILE { return Some(path.clone()); }
            }
        }
        if dir.parent().is_some() {
            dir.pop();
        } else {
            return None;
        }
        if count == parents { break; }
        count += 1;
    }
    None
}


/// Search for available migrations
fn search_for_migrations(mig_root: &PathBuf) -> Vec<Migration> {
    // collect any .sql files into a Map<`stamp-tag`, Vec<up&down files>>
    let mut files = HashMap::new();
    for dir in WalkDir::new(mig_root).into_iter() {
        let e = dir.unwrap();
        let path = e.path();
        if let Some(ext) = path.extension() {
            if ext.is_empty() || ext != "sql" { continue; }
            let parent = path.parent().unwrap();
            let key = format!("{}", parent.display());
            let entry = files.entry(key).or_insert(vec![]);
            entry.push(path.to_path_buf());
        }
    }
    // transform up&down files into a Vec<Migration>
    let mut migrations = vec![];
    for (path, migs) in files.iter() {
        let mut up = PathBuf::from(path);
        let mut down = PathBuf::from(path);
        let stamp_string = chrono::UTC.ymd(2000, 1, 1).and_hms(0, 0, 0).format(DT_FORMAT).to_string();
        let mut stamp = stamp_string.as_str();
        for mig in migs.iter() {
            let mut file_name = mig.file_name().and_then(|p| p.to_str()).unwrap().split('.');
            stamp = file_name.next().unwrap();
            let up_down = file_name.skip(1).next().unwrap();
            match up_down {
                "up" => up = mig.clone(),
                "down" => down = mig.clone(),
                _ => (),
            };
        }
        let migration = Migration::new(up, down,
                                       chrono::UTC.datetime_from_str(stamp, DT_FORMAT).unwrap());
        migrations.push(migration);
    }
    // sort, earliest migrations first
    migrations.sort_by(|a, b| a.stamp.cmp(&b.stamp));
    migrations
}


/// Determines whether new .migrant file location should be in
/// the given directory or a user specified path
fn new_meta_location(mut dir: PathBuf) -> Result<PathBuf> {
    dir.push(".migrant");
    println!(" $ --- No `.migrant` file found in parent path ---");
    println!(" $ A new `.migrant` file will be created at the following location: ");
    println!(" $  {:?}", dir.display());
    let ans = prompt(" $ Is this ok? (y/n) >> ", false);
    if ans.trim().to_lowercase() == "y" {
        return Ok(dir);
    }
    println!(" $ You can specify the absolute location now, or nothing to exit");
    let ans = prompt(" $ >> ", false);
    if ans.trim().is_empty() { bail!("No `.migrant` path provided"); }
    let path = PathBuf::from(ans);
    if !path.is_absolute() || path.file_name().unwrap() != ".migrant" {
        return bail!(format!("Invalid absolute path: {:?}", path.display()));
    }
    Ok(path)
}


/// Initialize the current directory
pub fn init(dir: &PathBuf) -> Result<()> {
    let meta = new_meta_location(dir.clone())
                    .chain_err(|| "unable to create a .migrant file")?;
    let db_type = prompt(" db-type (sqlite|pg) >> ", false);
    let db_name;
    let mut db_user = "".into();
    let mut password = "".into();
    let mut db_pref = None;
    match db_type.as_ref() {
        "pg" => db_pref = Some("postgres"),
        "sqlite" => (),
        _ => bail!("unsupported database type"),
    }
    if let Some(pref) = db_pref {
        db_name = prompt(" $ database name >> ", false);
        db_user = prompt(&format!(" $ {} database user >> ", pref), false);
        password = prompt(&format!(" $ {} user password >> ", pref), true);
    } else {
        db_name = prompt(" $ relative path to database (from .migrant file) >> ", false);
    }
    let settings = Settings::new(db_type, db_name, db_user, password);
    let json = format!("{}", json::as_pretty_json(&settings));
    let mut file = fs::File::create(meta)
                    .chain_err(|| "unable to create new file")?;
    file.write_all(json.as_bytes())
        .chain_err(|| "unable to write file contents")?;
    Ok(())
}


/// List the currently applied and available migrations under settings.migration_folder
pub fn list(mig_dir: &PathBuf, settings: &Settings) -> Result<()> {
    let available = search_for_migrations(mig_dir);
    println!("Current Migration Status:");
    for mig in available.iter() {
        let file = mig.up.file_name().unwrap();
        let mig_path = mig.up.to_str().map(String::from).unwrap();
        let x = settings.applied.contains(&mig_path);
        println!(" -> [{x}] {name}", x=if x { 'x' } else { ' ' }, name=file.to_str().unwrap());
    }
    Ok(())
}


/// Return the next available and unapplied migration
fn next_available(mig_dir: &PathBuf, applied: &[String]) -> Option<(PathBuf, PathBuf)> {
    let available = search_for_migrations(mig_dir);
    for mig in available.iter() {
        if !applied.contains(&mig.up.to_str().map(String::from).unwrap()) {
            return Some(
                (mig.up.clone(), mig.down.clone())
                );
        }
    }
    None
}


/// Move up one migration.
/// If `force`, record the migration as a success regardless of the outcome.
/// If `fake`, only update the settings file as if the migration was successful.
pub fn up(mig_dir: &PathBuf, meta_path: &PathBuf, settings: &mut Settings, force: bool, fake: bool) -> Result<()> {
    if let Some((next_available, down)) = next_available(mig_dir, settings.applied.as_slice()) {
        println!("Applying: {:?}", next_available);

        let mut stdout = String::new();
        if !fake {
            let out = runner(&settings, &meta_path, next_available.to_str().unwrap()).chain_err(|| "failed 'up'")?;
            let success = out.stderr.is_empty();
            if !success {
                let info = format!("psql --up stderr: {}",
                      String::from_utf8(out.stderr)
                             .chain_err(|| "Error getting stderr string")?);
                if force {
                    println!("{}", info);
                } else {
                    bail!("{}", info);
                }
            }
            stdout = String::from_utf8(out.stdout).chain_err(|| "Error getting stdout string")?;
        }

        println!("psql --up stdout: {}", stdout);
        settings.applied.push(next_available.to_str().unwrap().to_string());
        settings.down.push(down.to_str().unwrap().to_string());
        let json = format!("{}", json::as_pretty_json(settings));
        let mut file = fs::File::create(meta_path)
                        .chain_err(|| "unable to create file")?;
        file.write_all(json.as_bytes())
            .chain_err(|| "unable to write settings file")?;
    }
    Ok(())
}


/// Move down one migration.
/// If `force`, record the migration as a success regardless of the outcome.
/// If `fake`, only update the settings file as if the migration was successful.
pub fn down(meta_path: &PathBuf, settings: &mut Settings, force: bool, fake: bool) -> Result<()> {
    if let Some(last) = settings.down.pop() {
        println!("Onto database: {}", settings.database_name);
        println!("Applying: {}", last);

        let mut stdout = String::new();
        if !fake {
            let out = runner(&settings, &meta_path, &last).chain_err(|| "failed 'down'")?;
            let success = out.stderr.is_empty();
            if !success {
                let info = format!("--down stderr: {}",
                      String::from_utf8(out.stderr)
                             .chain_err(|| "Error getting stderr string")?);
                if force {
                    println!("{}", info);
                } else {
                    bail!("{}", info);
                }
            }
            stdout = String::from_utf8(out.stdout).chain_err(|| "Error getting stdout string")?;
        }

        println!("psql --down stdout: {}", stdout);
        settings.applied.pop();
        let json = format!("{}", json::as_pretty_json(settings));
        let mut file = fs::File::create(meta_path)
                         .chain_err(|| "unable to create file")?;
        file.write_all(json.as_bytes())
            .chain_err(|| "unable to write settings file")?;
    }
    Ok(())
}


/// Create a new migration with the given tag
pub fn new(mig_dir: &mut PathBuf, settings: &mut Settings, tag: &str) -> Result<()> {
    let now = chrono::UTC::now();
    let dt_string = now.format(DT_FORMAT).to_string();
    let folder = format!("{}.{}", dt_string, tag);
    mig_dir.push(&settings.migration_folder);
    mig_dir.push(folder);
    let _ = fs::create_dir_all(&mig_dir);

    let up = format!("{}.{}.{}.sql", dt_string, tag, "up");
    let down = format!("{}.{}.{}.sql", dt_string, tag, "down");
    let migs = vec![up, down];
    for mig in migs.iter() {
        let mut p = mig_dir.clone();
        p.push(mig);
        let _ = fs::File::create(&p).chain_err(|| "Failed to create file")?;
        println!("Created: {:?}", p);
    }
    Ok(())
}


/// Open a repl connection to the specified database connection
pub fn shell(meta_path: &PathBuf, settings: Settings) -> Result<()> {
    Ok(match settings.database_type.as_ref() {
        "sqlite" => {
            let mut db_path = meta_path.clone();
            db_path.pop();
            db_path.push(&settings.database_name);
            let _ = Command::new("sqlite3")
                    .arg(db_path.to_str().unwrap())
                    .spawn().unwrap().wait()
                    .chain_err(|| "failed to run migration command")?;
        }
        "pg" => {
            Command::new("psql")
                    .arg("-U").arg(&settings.db_user)
                    .arg("-d").arg(&settings.database_name)
                    .arg("-h").arg("localhost")
                    .spawn().unwrap().wait()
                    .chain_err(|| "failed to run shell")?;
        }
        _ => unreachable!(),
    })
}