aboutsummaryrefslogtreecommitdiff
path: root/src/cli/migrate.rs
blob: 4085fe105e0136ecc7ced30e132ec1fd64d15881 (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
use structopt::StructOpt;

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

#[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(self, _urls: UrlMap, settings: Settings) {
        let database = settings.database;
        let connection = database.connect().unwrap();

        CREATE_MIGRATION_TABLE.apply("tosin_meta", &connection);

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