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
use std; use std::path::{Path, PathBuf}; use chrono::{DateTime, Utc}; use drivers; use migratable::Migratable; use config::Config; use connection::DbConn; use {DbKind, invalid_tag, Direction, DT_FORMAT}; use errors::*; #[derive(Clone, Debug)] pub struct FileMigration { pub tag: String, pub up: Option<PathBuf>, pub down: Option<PathBuf>, pub stamp: Option<DateTime<Utc>>, } impl FileMigration { pub fn with_tag(tag: &str) -> Result<Self> { if invalid_tag(tag) { bail_fmt!(ErrorKind::Migration, "Invalid tag `{}`. Tags can contain [a-z0-9-]", tag); } Ok(Self { tag: tag.to_owned(), up: None, down: None, stamp: None, }) } fn check_path(path: &Path) -> Result<()> { if !path.exists() { bail_fmt!(ErrorKind::MigrationNotFound, "Migration file not found: {:?}", path) } Ok(()) } pub fn up<T: AsRef<Path>>(&mut self, up_file: T) -> Result<&mut Self> { let path = up_file.as_ref(); Self::check_path(path)?; self.up = Some(path.to_owned()); Ok(self) } pub fn down<T: AsRef<Path>>(&mut self, down_file: T) -> Result<&mut Self> { let path = down_file.as_ref(); Self::check_path(path)?; self.down = Some(path.to_owned()); Ok(self) } pub fn boxed(&self) -> Box<Migratable> { Box::new(self.clone()) } } impl Migratable for FileMigration { fn apply_up(&self, db_kind: DbKind, config: &Config) -> std::result::Result<(), Box<std::error::Error>> { if let Some(ref up) = self.up { match db_kind { DbKind::Sqlite => { let db_path = config.database_path()?; drivers::sqlite::run_migration(&db_path, up)?; } DbKind::Postgres => { let conn_str = config.connect_string()?; drivers::pg::run_migration(&conn_str, up)?; } } } else { print_flush!("(empty) ..."); } Ok(()) } fn apply_down(&self, db_kind: DbKind, config: &Config) -> std::result::Result<(), Box<std::error::Error>> { if let Some(ref down) = self.down { match db_kind { DbKind::Sqlite => { let db_path = config.database_path()?; drivers::sqlite::run_migration(&db_path, down)?; } DbKind::Postgres => { let conn_str = config.connect_string()?; drivers::pg::run_migration(&conn_str, down)?; } } } else { print_flush!("(empty) ..."); } Ok(()) } fn tag(&self) -> String { match self.stamp.as_ref() { Some(dt) => { let dt_string = dt.format(DT_FORMAT).to_string(); format!("{}_{}", dt_string, self.tag) } None => self.tag.to_owned(), } } fn description(&self, direction: &Direction) -> String { match *direction { Direction::Up => self.up.as_ref().map(|p| format!("{:?}", p)).unwrap_or_else(|| self.tag()), Direction::Down => self.down.as_ref().map(|p| format!("{:?}", p)).unwrap_or_else(|| self.tag()), } } } #[derive(Clone, Debug)] pub struct FnMigration<T, U> { pub tag: String, pub up: Option<T>, pub down: Option<U>, } impl<T, U> FnMigration<T, U> where T: 'static + Clone + Fn(DbConn) -> std::result::Result<(), Box<std::error::Error>>, U: 'static + Clone + Fn(DbConn) -> std::result::Result<(), Box<std::error::Error>> { pub fn with_tag(tag: &str) -> Result<Self> { if invalid_tag(tag) { bail_fmt!(ErrorKind::Migration, "Invalid tag `{}`. Tags can contain [a-z0-9-]", tag); } Ok(Self { tag: tag.to_owned(), up: None, down: None, }) } pub fn up(&mut self, f_up: T) -> &mut Self { self.up = Some(f_up); self } pub fn down(&mut self, f_down: U) -> &mut Self { self.down = Some(f_down); self } pub fn boxed(&self) -> Box<Migratable> { Box::new(self.clone()) } } impl<T, U> Migratable for FnMigration<T, U> where T: 'static + Clone + Fn(DbConn) -> std::result::Result<(), Box<std::error::Error>>, U: 'static + Clone + Fn(DbConn) -> std::result::Result<(), Box<std::error::Error>> { fn apply_up(&self, _: DbKind, config: &Config) -> std::result::Result<(), Box<::std::error::Error>> { if let Some(ref up) = self.up { up(DbConn::new(config))?; } else { print_flush!("(empty) ..."); } Ok(()) } fn apply_down(&self, _: DbKind, config: &Config) -> std::result::Result<(), Box<::std::error::Error>> { if let Some(ref down) = self.down { down(DbConn::new(config))?; } else { print_flush!("(empty) ..."); } Ok(()) } fn tag(&self) -> String { self.tag.to_owned() } fn description(&self, _: &Direction) -> String { self.tag() } }