Corrige ejecución de las migraciones en módulos

This commit is contained in:
Manuel Cillero 2022-03-17 19:41:41 +01:00
parent 2167ab9417
commit 58afd1f258
9 changed files with 35 additions and 38 deletions

View file

@ -1,12 +1 @@
use crate::db::migration::*;
pub mod m20220312_000001_create_table_user;
pub struct Migrator;
#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![Box::new(m20220312_000001_create_table_user::Migration)]
}
}

View file

@ -1,6 +1,6 @@
use crate::prelude::*;
localize!("en-US", "src/base/module/user/locales");
localize!("src/base/module/user/locales");
mod entity;
mod migration;
@ -24,8 +24,10 @@ impl ModuleTrait for UserModule {
cfg.route("/user/login", server::web::get().to(login));
}
fn migrations(&self, dbconn: &db::DbConn) -> Result<(), db::DbErr> {
db_migrations!(dbconn)
fn migrations(&self) -> Vec<Box<dyn db::migration::MigrationTrait>> {
vec![
boxed_migration!(m20220312_000001_create_table_user)
]
}
}

View file

@ -45,15 +45,15 @@ pub static CONFIG: Lazy<Config> = Lazy::new(|| {
/// seguros. Produce un *panic!* en caso de asignaciones no válidas.
macro_rules! config_map {
(
$COMM:expr,
$CONF:ident,
$TYPE:tt
$doc:expr,
$SETTINGS:ident,
$Type:tt
$(, $key:expr => $value:expr)*
) => {
$crate::doc_comment! {
concat!($COMM),
concat!($doc),
pub static $CONF: $crate::Lazy<$TYPE> = $crate::Lazy::new(|| {
pub static $SETTINGS: $crate::Lazy<$Type> = $crate::Lazy::new(|| {
let mut settings = $crate::config::CONFIG.clone();
$(
settings.set_default($key, $value).unwrap();

View file

@ -1,4 +1,5 @@
use crate::{Lazy, trace};
use crate::{Lazy, run_now};
use crate::db::migration::*;
use crate::core::theme::ThemeTrait;
use crate::core::module::ModuleTrait;
use crate::core::server;
@ -41,9 +42,18 @@ pub fn modules(cfg: &mut server::web::ServiceConfig) {
}
#[cfg(any(feature = "mysql", feature = "postgres", feature = "sqlite"))]
pub fn migrations() {
trace::info!("Checking migrations");
for m in MODULES.read().unwrap().iter() {
m.migrations(&*server::db::DBCONN).expect("Failed to run migrations");
}
pub fn run_migrations() {
run_now({
struct Migrator;
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
let mut migrations = vec![];
for m in MODULES.read().unwrap().iter() {
migrations.append(&mut m.migrations());
}
migrations
}
}
Migrator::up(&server::db::DBCONN, None)
}).unwrap();
}

View file

@ -19,7 +19,7 @@ pub trait ModuleTrait: Send + Sync {
#[cfg(any(feature = "mysql", feature = "postgres", feature = "sqlite"))]
#[allow(unused_variables)]
fn migrations(&self, dbconn: &db::DbConn) -> Result<(), db::DbErr> {
Ok(())
fn migrations(&self) -> Vec<Box<dyn db::migration::MigrationTrait>> {
vec![]
}
}

View file

@ -74,7 +74,7 @@ impl Application {
// Comprueba actualizaciones pendientes de la base de datos (opcional).
#[cfg(any(feature = "mysql", feature = "postgres", feature = "sqlite"))]
all::migrations();
all::run_migrations();
// Prepara el servidor web.
let server = server::HttpServer::new(move || {

View file

@ -14,12 +14,8 @@ pub mod migration {
}
#[macro_export]
macro_rules! db_migrations {
( $DBCONN:ident ) => {{
$crate::run_now({
use $crate::db::migration::MigratorTrait;
migration::Migrator::up($DBCONN, None)
})
macro_rules! boxed_migration {
( $migration_module:ident ) => {{
Box::new(migration::$migration_module::Migration)
}};
}

View file

@ -6,7 +6,7 @@ pub use crate::trace;
pub use crate::localize;
#[cfg(any(feature = "mysql", feature = "postgres", feature = "sqlite"))]
pub use crate::{db, db_migrations};
pub use crate::{db, boxed_migration};
pub use crate::core::html::*;
pub use crate::core::theme::*;

View file

@ -9,10 +9,10 @@
/// ];
/// ```
macro_rules! args {
( $($KEY:expr => $VALUE:expr),* ) => {{
( $($key:expr => $value:expr),* ) => {{
let mut a = std::collections::HashMap::new();
$(
a.insert(String::from($KEY), $VALUE.into());
a.insert(String::from($key), $value.into());
)*
a
}};