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