aboutsummaryrefslogtreecommitdiff
path: root/src/db/migration/mod.rs
blob: 839608ff9198d1b7cb0262a9ef796d187a2616d0 (plain)
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
pub use tosin_macros::gather_migrations as gather;

use diesel::{result::Error as DieselError, Connection};

use crate::db::backend::Connectable;

mod change;

pub use change::*;

pub struct Migration {
    pub id: usize,
    pub name: &'static str,
    pub prereqs: &'static [(&'static str, usize)],
    pub changes: &'static [DatabaseChange],
}

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);
                }

                Ok(())
            })
            .unwrap();
    }
}