From 58d2f63f4577bc701b6bd655064cefebb65118b4 Mon Sep 17 00:00:00 2001 From: Melody Horn / boringcactus Date: Wed, 16 Jun 2021 14:22:50 -0600 Subject: actually run migrations --- src/cli/migrate.rs | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) (limited to 'src/cli') diff --git a/src/cli/migrate.rs b/src/cli/migrate.rs index 4b5123a..a6efbb5 100644 --- a/src/cli/migrate.rs +++ b/src/cli/migrate.rs @@ -1,19 +1,47 @@ use structopt::StructOpt; -use crate::{Settings, UrlMap, db::backend::{Connectable, Connection}}; +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) { + pub fn execute(self, _urls: UrlMap, settings: Settings) { let database = settings.database; let connection = database.connect().unwrap(); - let migration_table_vibe_check = connection.transaction::<(), diesel::result::Error, _>(|| { - todo!() - }); + CREATE_MIGRATION_TABLE.apply::("tosin_meta", &connection); + + for app in settings.installed_apps { + for migration in app.migrations { + migration.apply::(app.name, &connection); + } + } } } -- cgit v1.2.3