aboutsummaryrefslogtreecommitdiff
path: root/src/cli/migrate.rs
blob: a6efbb5245e7700bf56feef94a53faa272473753 (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use structopt::StructOpt;

use crate::{Settings, UrlMap};
use crate::db::backend::Connectable;
use crate::db::migration::{DatabaseChange, CreateModelOption};
use crate::db::models::Field;

#[derive(StructOpt)]
/// Perform database migrations
pub struct Migrate {
}

const CREATE_MIGRATION_TABLE: DatabaseChange = DatabaseChange::CreateModel {
    name: "Migration",
    fields: &[
        Field::CharField {
            name: "app_name",
            max_length: None,
        },
        Field::IntField {
            name: "migration_id",
        },
        Field::CharField {
            name: "migration_name",
            max_length: None,
        },
        Field::DateTimeField {
            name: "applied_at",
        }
    ],
    options: &[CreateModelOption::IfNotExist],
};

impl Migrate {
    pub fn execute<C: Connectable>(self, _urls: UrlMap, settings: Settings<C>) {
        let database = settings.database;
        let connection = database.connect().unwrap();

        CREATE_MIGRATION_TABLE.apply::<C>("tosin_meta", &connection);

        for app in settings.installed_apps {
            for migration in app.migrations {
                migration.apply::<C>(app.name, &connection);
            }
        }
    }
}