diff options
author | Melody Horn <melody@boringcactus.com> | 2021-06-28 17:12:33 -0600 |
---|---|---|
committer | Melody Horn <melody@boringcactus.com> | 2021-06-28 17:12:33 -0600 |
commit | 42d14c943eb31753489b39ef58803c543c023bee (patch) | |
tree | e5c7d9e3784192ebdc390dfa481631eaad07f0fd /src/db | |
parent | b1ec80c918dd9e156c049a500983fb15147c6e14 (diff) | |
download | tosin-42d14c943eb31753489b39ef58803c543c023bee.tar.gz tosin-42d14c943eb31753489b39ef58803c543c023bee.zip |
fmt
Diffstat (limited to 'src/db')
-rw-r--r-- | src/db/migration/change.rs | 43 | ||||
-rw-r--r-- | src/db/migration/mod.rs | 16 | ||||
-rw-r--r-- | src/db/mod.rs | 2 | ||||
-rw-r--r-- | src/db/models/mod.rs | 2 |
4 files changed, 35 insertions, 28 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(); } } diff --git a/src/db/mod.rs b/src/db/mod.rs index e04fe06..dd21406 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -2,5 +2,5 @@ pub mod backend; pub mod migration; pub mod models; -pub use diesel::*; pub use diesel::backend as diesel_backend; +pub use diesel::*; diff --git a/src/db/models/mod.rs b/src/db/models/mod.rs index 3f39ccd..4133cf2 100644 --- a/src/db/models/mod.rs +++ b/src/db/models/mod.rs @@ -1,4 +1,4 @@ -pub use tosin_macros::{Model, gather_models as gather}; +pub use tosin_macros::{gather_models as gather, Model}; mod meta; pub use meta::*; |