aboutsummaryrefslogtreecommitdiff
path: root/src/db/migration/mod.rs
blob: 61cdcf98428414a46dce2bf195ae2a9d657f4d75 (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
use diesel::{result::Error as DieselError, Connection as _};

use crate::db::backend::Connection;

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(&self, app_name: &str, connection: &Connection) {
        // TODO prevent double-applying
        connection
            .transaction::<_, DieselError, _>(|| {
                println!("{}:\tapplying {} (id {})", app_name, self.name, self.id);
                for change in self.changes {
                    change.apply(app_name, connection);
                }

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