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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
use std::io::Read;
use std::path::{Path, PathBuf};
use std::env;
use std::fs;
use std::collections::HashSet;

use toml;
use url;
use chrono::{self, TimeZone};

use drivers;
use {
    Migratable, encode, prompt, open_file_in_fg, write_to_path, invalid_tag,
    FULL_TAG_RE, DT_FORMAT, CONFIG_FILE,
    PG_CONFIG_TEMPLATE, SQLITE_CONFIG_TEMPLATE,
};
use errors::*;


#[derive(Debug, Clone)]
/// Project configuration/settings builder to initialize a new config file
pub struct ConfigInitializer {
    dir: PathBuf,
    database_type: Option<String>,
    interactive: bool,

}
impl ConfigInitializer {
    /// Start a new `ConfigInitializer`
    pub fn new(dir: &Path) -> Self {
        Self {
            dir: dir.to_owned(),
            database_type: None,
            interactive: true,
        }
    }

    /// Specify the database_type, checks whether the type is supported
    pub fn for_database(mut self, db_type: Option<&str>) -> Result<Self> {
        match db_type {
            None => self.database_type = None,
            Some(db_type) => {
                match db_type {
                    "postgres" | "sqlite" => (),
                    e => bail_fmt!(ErrorKind::Config, "unsupported database type: {}", e),
                };
                self.database_type = Some(db_type.to_owned());
            }
        }
        Ok(self)
    }

    /// Set interactive prompts, default is `true`
    pub fn interactive(mut self, b: bool) -> Self {
        self.interactive = b;
        self
    }

    /// Determines whether new .migrant file location should be in
    /// the given directory or a user specified path
    fn confirm_new_config_location(dir: &Path) -> Result<PathBuf> {
        println!(" A new `{}` config file will be created at the following location: ", CONFIG_FILE);
        println!("   {:?}", dir.display());
        let ans = prompt(" Is this ok? [Y/n] ")?;
        if ans.is_empty() || ans.to_lowercase() == "y" {
            return Ok(dir.to_owned());
        }

        println!(" You can specify the absolute location now, or nothing to exit");
        let ans = prompt(" >> ")?;
        if ans.is_empty() {
            bail_fmt!(ErrorKind::Config, "No `{}` path provided", CONFIG_FILE)
        }

        let path = PathBuf::from(ans);
        if !path.is_absolute() || path.file_name().unwrap() != CONFIG_FILE {
            bail_fmt!(ErrorKind::Config, "Invalid absolute path: {}, must end in `{}`", path.display(), CONFIG_FILE);
        }
        Ok(path)
    }

    /// Generate a template config file using provided parameters or prompting the user.
    /// If running interactively, the file will be opened for editing and `Config::setup`
    /// will be run automatically.
    pub fn initialize(self) -> Result<()> {
        let config_path = self.dir.join(CONFIG_FILE);
        let config_path = if !self.interactive {
            config_path
        } else {
            ConfigInitializer::confirm_new_config_location(&config_path)
                .map_err(|e| format_err!(ErrorKind::Config, "unable to create a `{}` config -> {}", CONFIG_FILE, e))?
        };

        let db_type = if let Some(db_type) = self.database_type.as_ref() {
            db_type.to_owned()
        } else {
            if !self.interactive {
                bail_fmt!(ErrorKind::Config, "database type must be specified if running non-interactively")
            }
            println!("\n ** Gathering database information...");
            let db_type = prompt(" database type (sqlite|postgres) >> ")?;
            match db_type.as_ref() {
                "postgres" | "sqlite" => (),
                e => bail_fmt!(ErrorKind::Config, "unsupported database type: {}", e),
            };
            db_type
        };

        println!("\n ** Writing {} config template to {:?}", db_type, config_path);
        match db_type.as_ref() {
            "postgres" => {
                write_to_path(&config_path, PG_CONFIG_TEMPLATE.as_bytes())?;
            }
            "sqlite" => {
                let content = SQLITE_CONFIG_TEMPLATE.replace("__CONFIG_DIR__", config_path.parent().unwrap().to_str().unwrap());
                write_to_path(&config_path, content.as_bytes())?;
            }
            _ => unreachable!(),
        };

        println!("\n ** Please update `{}` with your database credentials and run `setup`", CONFIG_FILE);

        if self.interactive {
            let editor = env::var("EDITOR").unwrap_or_else(|_| "vim".to_string());
            let file_path = config_path.to_str().unwrap();
            let command = format!("{} {}", editor, file_path);
            println!(" -- Your config file will be opened with the following command: `{}`", &command);
            println!(" -- After editing, the `setup` command will be run for you");
            let _ = prompt(&format!(" -- Press [ENTER] to open now or [CTRL+C] to exit and edit manually"))?;
            open_file_in_fg(&editor, file_path)
                .map_err(|e| format_err!(ErrorKind::Config, "Error editing config file: {}", e))?;

            println!();
            let config = Config::load_file_only(&config_path)?;
            let _setup = config.setup()?;
        }
        Ok(())
    }
}


#[derive(Serialize, Deserialize, Debug, Clone)]
/// Settings that are serialized and saved in a project `.migrant.toml` config file
pub(crate) struct Settings {
    pub(crate) database_type: String,
    pub(crate) database_name: String,
    pub(crate) database_host: Option<String>,
    pub(crate) database_port: Option<String>,
    pub(crate) database_user: Option<String>,
    pub(crate) database_password: Option<String>,
    pub(crate) migration_location: String,
    pub(crate) database_params: Option<toml::value::Table>,
}


#[derive(Debug, Clone)]
/// Project configuration/settings
pub struct Config {
    pub path: PathBuf,
    pub(crate) settings: Settings,
    pub(crate) applied: Vec<String>,
    pub(crate) migrations: Option<Vec<Box<Migratable>>>,
}
impl Config {
    /// Define an explicit set of `Migratable` migrations to use.
    ///
    /// When using explicit migrations, make sure they are defined on the `Config`
    /// instance before applied migrations are loaded from the database. This is
    /// required because tag format requirements are stricter for implicit
    /// (file-system based) migrations are stricter, requiring a timestamp to
    /// maintain a deterministic order.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// let mut config = Config::load_file_only(&p)?;
    /// config.use_migrations(vec![
    ///     EmbeddedMigration::with_tag("initial")?
    ///         .up(include_str!("../migrations/initial/up.sql"))
    ///         .down(include_str!("../migrations/initial/down.sql"))
    ///         .boxed(),
    ///     FileMigration::with_tag("second")?
    ///         .up("migrations/second/up.sql")?
    ///         .down("migrations/second/down.sql")?
    ///         .boxed(),
    ///     FnMigration::with_tag("custom")?
    ///         .up(migrations::Custom::up)
    ///         .down(migrations::Custom::down)
    ///         .boxed(),
    /// ])?;
    /// let config = config.reload()?;
    /// ```
    pub fn use_migrations(&mut self, migrations: Vec<Box<Migratable>>) -> Result<&mut Self> {
        let mut set = HashSet::new();
        for mig in &migrations {
            let tag = mig.tag();
            if set.contains(&tag) {
                bail_fmt!(ErrorKind::TagError, "Tags must be unique. Found duplicate: {}", tag)
            }
            set.insert(tag);
        }
        self.migrations = Some(migrations);
        Ok(self)
    }

    /// Migrations are explicitly defined
    pub fn is_explicit(&self) -> bool {
        self.migrations.is_some()
    }

    /// Check that migration tags conform to naming requirements.
    /// If migrations are explicitly defined (with `use_migrations`), then
    /// tags may only contain [a-z0-9-]. If migrations are managed by `migrant`,
    /// not specified with `use_migrations` and instead created by `migrant_lib::new`,
    /// then they must follow [0-9]{14}_[a-z0-9-] (<timestamp>_<name>).
    fn check_saved_tag(&self, tag: &str) -> Result<()> {
        if self.is_explicit() {
            if invalid_tag(tag) {
                bail_fmt!(ErrorKind::Migration, "Found a non-conforming tag in the database: `{}`. \
                                                 Managed tags may contain [a-z0-9-]", tag)
            }
        } else if !FULL_TAG_RE.is_match(&tag) {
            bail_fmt!(ErrorKind::Migration, "Found a non-conforming tag in the database: `{}`. \
                                             Generated tags must follow [0-9]{{14}}_[a-z0-9-]", tag)
        }
        Ok(())
    }

    /// Do a full reload of the configuration file, keeping track of
    /// manually specified `migrations`
    pub fn reload(&self) -> Result<Config> {
        let mut config = Config::load_file_only(&self.path)?;
        config.migrations = self.migrations.clone();
        let applied = config.load_applied()?;
        config.applied = applied;
        Ok(config)
    }

    /// Load config file from the given path without querying the database
    /// to check for applied migrations
    pub fn load_file_only<T: AsRef<Path>>(path: T) -> Result<Config> {
        let path = path.as_ref();
        let mut file = fs::File::open(path)?;
        let mut content = String::new();
        file.read_to_string(&mut content)?;
        let settings = toml::from_str::<Settings>(&content)?;
        Ok(Config {
            path: path.to_owned(),
            settings: settings,
            applied: vec![],
            migrations: None,
        })
    }

    /// Load config file from the given path and query the database to load up applied migrations
    pub fn load<T: AsRef<Path>>(path: T) -> Result<Config> {
        let path = path.as_ref();
        let mut config = Config::load_file_only(path)?;
        let applied = config.load_applied()?;
        config.applied = applied;
        Ok(config)
    }

    /// Load the applied migrations from the database migration table
    pub(crate) fn load_applied(&self) -> Result<Vec<String>> {
        if !self.migration_table_exists()? {
            bail_fmt!(ErrorKind::Migration, "`__migrant_migrations` table is missing, maybe try re-setting-up? -> `setup`")
        }

        let applied = match self.settings.database_type.as_ref() {
            "sqlite"    => drivers::sqlite::select_migrations(&self.settings.database_name)?,
            "postgres"  => drivers::pg::select_migrations(&self.connect_string()?)?,
            _ => unreachable!(),
        };
        let mut tags = vec![];
        for tag in applied.into_iter() {
            self.check_saved_tag(&tag)?;
            tags.push(tag);
        }
        let tags = if self.is_explicit() { tags } else {
            let mut stamped = tags.into_iter().map(|tag| {
                let stamp = tag.split('_').next()
                    .ok_or_else(|| format_err!(ErrorKind::TagError, "Invalid tag format: {:?}", tag))?;
                let stamp = chrono::Utc.datetime_from_str(stamp, DT_FORMAT)?;
                Ok((stamp, tag.clone()))
            }).collect::<Result<Vec<_>>>()?;
            stamped.sort_by(|a, b| a.0.cmp(&b.0));
            stamped.into_iter().map(|tup| tup.1).collect::<Vec<_>>()
        };
        Ok(tags)
    }


    /// Check if a __migrant_migrations table exists
    pub(crate) fn migration_table_exists(&self) -> Result<bool> {
        match self.settings.database_type.as_ref() {
            "sqlite"    => drivers::sqlite::migration_table_exists(self.settings.database_name.as_str()),
            "postgres"  => drivers::pg::migration_table_exists(&self.connect_string()?),
            _ => unreachable!()
        }
    }

    /// Insert given tag into database migration table
    pub(crate) fn insert_migration_tag(&self, tag: &str) -> Result<()> {
        match self.settings.database_type.as_ref() {
            "sqlite"    => drivers::sqlite::insert_migration_tag(&self.settings.database_name, tag)?,
            "postgres"  => drivers::pg::insert_migration_tag(&self.connect_string()?, tag)?,
            _ => unreachable!(),
        };
        Ok(())
    }

    /// Remove a given tag from the database migration table
    pub(crate) fn delete_migration_tag(&self, tag: &str) -> Result<()> {
        match self.settings.database_type.as_ref() {
            "sqlite"    => drivers::sqlite::remove_migration_tag(&self.settings.database_name, tag)?,
            "postgres"  => drivers::pg::remove_migration_tag(&self.connect_string()?, tag)?,
            _ => unreachable!(),
        };
        Ok(())
    }

    /// Start a config initializer in the given directory
    pub fn init_in(dir: &Path) -> ConfigInitializer {
        ConfigInitializer::new(dir)
    }

    /// - Confirm the database can be accessed
    /// - Setup the database migrations table if it doesn't exist yet
    pub fn setup(&self) -> Result<bool> {
        println!(" ** Confirming database credentials...");
        match self.settings.database_type.as_ref() {
            "sqlite" => {
                if self.settings.database_name.is_empty() {
                    bail_fmt!(ErrorKind::Config, "`database_name` cannot be blank!")
                }
                let db_path = self.path.parent().unwrap().join(&self.settings.database_name);
                let created = drivers::sqlite::create_file_if_missing(&db_path)?;
                println!("    - checking if db file already exists...");
                if created {
                    println!("    - db not found... creating now... ✓")
                } else {
                    println!("    - db already exists ✓");
                }
            }
            "postgres" => {
                let conn_str = self.connect_string()?;
                let can_connect = drivers::pg::can_connect(&conn_str)?;
                if !can_connect {
                    println!(" ERROR: Unable to connect to {}", conn_str);
                    println!("        Please initialize your database and user and then run `setup`");
                    println!("\n  ex) sudo -u postgres createdb {}", &self.settings.database_name);
                    println!("      sudo -u postgres createuser {}", self.settings.database_user.as_ref().unwrap());
                    println!("      sudo -u postgres psql -c \"alter user {} with password '****'\"", self.settings.database_user.as_ref().unwrap());
                    println!();
                    bail_fmt!(ErrorKind::Config, "Cannot connect to postgres database");
                } else {
                    println!("    - Connection confirmed ✓");
                }
            }
            _ => unreachable!(),
        }

        println!("\n ** Setting up migrations table");
        let table_created = match self.settings.database_type.as_ref() {
            "sqlite" => {
                let db_path = self.path.parent().unwrap().join(&self.settings.database_name);
                drivers::sqlite::migration_setup(&db_path)?
            }
            "postgres" => {
                let conn_str = self.connect_string()?;
                drivers::pg::migration_setup(&conn_str)?
            }
            _ => unreachable!(),
        };

        if table_created {
            println!("    - migrations table missing");
            println!("    - `__migrant_migrations` table created ✓");
            Ok(true)
        } else {
            println!("    - `__migrant_migrations` table already exists ✓");
            Ok(false)
        }
    }

    /// Return the absolute path to the directory containing migration folders
    pub fn migration_dir(&self) -> Result<PathBuf> {
        Ok(self.path.parent()
            .map(|p| p.join(&self.settings.migration_location))
            .ok_or_else(|| format_err!(ErrorKind::PathError, "Error generating PathBuf to migration_location"))?)
    }

    /// Return the database type
    pub fn database_type(&self) -> Result<String> {
        Ok(self.settings.database_type.to_owned())
    }

    /// Return the absolute path to the database file. This is intended for
    /// sqlite3 databases only
    pub fn database_path(&self) -> Result<PathBuf> {
        match self.settings.database_type.as_ref() {
            "sqlite" => (),
            db_t => bail_fmt!(ErrorKind::Config, "Cannot retrieve database-path for database-type: {}", db_t),
        };

        Ok(self.path.parent()
            .map(|p| p.join(&self.settings.database_name))
            .ok_or_else(|| format_err!(ErrorKind::PathError, "Error generating PathBuf to database-path"))?)
    }

    /// Generate a database connection string.
    /// Not intended for file-based databases (sqlite)
    pub fn connect_string(&self) -> Result<String> {
        match self.settings.database_type.as_ref() {
            "postgres" => (),
            db_t => bail_fmt!(ErrorKind::Config, "Cannot generate connect-string for database-type: {}", db_t),
        };

        let pass = match self.settings.database_password {
            Some(ref pass) => {
                let p = encode(pass);
                format!(":{}", p)
            },
            None => "".into(),
        };
        let user = match self.settings.database_user.as_ref().and_then(|s| if s.is_empty() { None } else { Some(s) }) {
            Some(ref user) => encode(user),
            None => bail_fmt!(ErrorKind::Config, "'database_user' not specified"),
        };

        let db_name = if self.settings.database_name.is_empty() {
            bail_fmt!(ErrorKind::Config, "`database_name` not specified");
        } else {
            encode(&self.settings.database_name)
        };

        let host = self.settings.database_host.clone().unwrap_or_else(|| "localhost".to_string());
        let host = if host.is_empty() { "localhost".to_string() } else { host };
        let host = encode(&host);

        let port = self.settings.database_port.clone().unwrap_or_else(|| "5432".to_string());
        let port = if host.is_empty() { "5432".to_string() } else { port };
        let port = encode(&port);

        let s = format!("{db_type}://{user}{pass}@{host}:{port}/{db_name}",
                db_type=self.settings.database_type,
                user=user,
                pass=pass,
                host=host,
                port=port,
                db_name=db_name);

        let mut url = url::Url::parse(&s)?;

        if let Some(ref params) = self.settings.database_params {
            let mut pairs = vec![];
            for (k, v) in params.iter() {
                let k = encode(k);
                let v = match *v {
                    toml::value::Value::String(ref s) => encode(s),
                    ref v => bail_fmt!(ErrorKind::Config, "Database params can only be strings, found `{}={}`", k, v),
                };
                pairs.push((k, v));
            }
            if !pairs.is_empty() {
                let mut url = url.query_pairs_mut();
                for &(ref k, ref v) in &pairs {
                    url.append_pair(k, v);
                }
            }
        }

        Ok(url.into_string())
    }
}