diff options
Diffstat (limited to 'src/db/migration')
-rw-r--r-- | src/db/migration/change.rs | 43 | ||||
-rw-r--r-- | src/db/migration/mod.rs | 16 |
2 files changed, 33 insertions, 26 deletions
diff --git a/src/db/migration/change.rs b/src/db/migration/change.rs index b5d0dd5..8bb8bf6 100644 --- a/src/db/migration/change.rs +++ b/src/db/migration/change.rs @@ -7,34 +7,39 @@ pub enum DatabaseChange { CreateModel { name: &'static str, fields: &'static [Field], - options: &'static [CreateModelOption] + options: &'static [CreateModelOption], }, } impl DatabaseChange { pub fn apply<C: Connectable>(&self, app_name: &str, connection: &C::Connection) { - use barrel::{Migration, Table, types}; + use barrel::{types, Migration, Table}; match self { - DatabaseChange::CreateModel { name, fields, options } => { + DatabaseChange::CreateModel { + name, + fields, + options, + } => { let mut m = Migration::new(); - let columns: Vec<(&'static str, _)> = fields.iter().map(|field| match field { - Field::CharField { name, max_length } => { - let name = *name; - let _type = match max_length { - None => types::text(), - Some(max_length) => types::varchar(*max_length), - }; - (name, _type) - } - Field::DateTimeField { name } => { - (*name, types::text()) // TODO do smart things on non-sqlite - } - Field::IntField { name } => { - (*name, types::integer()) - } - }).collect(); + let columns: Vec<(&'static str, _)> = fields + .iter() + .map(|field| match field { + Field::CharField { name, max_length } => { + let name = *name; + let _type = match max_length { + None => types::text(), + Some(max_length) => types::varchar(*max_length), + }; + (name, _type) + } + Field::DateTimeField { name } => { + (*name, types::text()) // TODO do smart things on non-sqlite + } + Field::IntField { name } => (*name, types::integer()), + }) + .collect(); let callback = move |t: &mut Table| { for (name, _type) in &columns { diff --git a/src/db/migration/mod.rs b/src/db/migration/mod.rs index d511f88..839608f 100644 --- a/src/db/migration/mod.rs +++ b/src/db/migration/mod.rs @@ -1,6 +1,6 @@ pub use tosin_macros::gather_migrations as gather; -use diesel::{Connection, result::Error as DieselError}; +use diesel::{result::Error as DieselError, Connection}; use crate::db::backend::Connectable; @@ -18,12 +18,14 @@ pub struct Migration { impl Migration { pub fn apply<C: Connectable>(&self, app_name: &str, connection: &C::Connection) { // TODO prevent double-applying - connection.transaction::<_, DieselError, _>(|| { - for change in self.changes { - change.apply::<C>(app_name, connection); - } + connection + .transaction::<_, DieselError, _>(|| { + for change in self.changes { + change.apply::<C>(app_name, connection); + } - Ok(()) - }).unwrap(); + Ok(()) + }) + .unwrap(); } } |