Enable loading migrations from multiple directories. #2189
Replies: 4 comments 1 reply
-
After looking into the source of the scaffolded crate, I just found that you can customize the behavior of Therefore, I'm changing the category of this discussion from Ideas to Q&A for now to avoid possible confusion. (I'm going to try if this approach works or not) (btw, is this a recommended practice to do so, or does SeaORM have a requirement or limitation that depends on that |
Beta Was this translation helpful? Give feedback.
-
tl;dr: The "Multiple migrator" approach either doesn't work or requires additional modification.The following is my attempt making use of
use sea_orm_migration::prelude::*;
#[async_std::main]
async fn main() {
cli::run_cli(migration::KernelMigrator).await;
cli::run_cli(migration::NotesMigrator).await;
}
#![allow(elided_lifetimes_in_paths)]
#![allow(clippy::wildcard_imports)]
pub use sea_orm_migration::prelude::*;
mod m20220101_000001_users;
mod m20231103_114510_notes;
pub struct KernelMigrator;
pub struct NotesMigrator;
#[async_trait::async_trait]
impl MigratorTrait for KernelMigrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![
Box::new(m20220101_000001_users::Migration),
]
}
}
#[async_trait::async_trait]
impl MigratorTrait for NotesMigrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![
Box::new(m20231103_114510_notes::Migration),
]
}
}
➜ tester $ DATABASE_URL="mysql://test:test@localhost:3306/tester" sea-orm-cli migrate up (base)
Running `cargo run --manifest-path ./migration/Cargo.toml -- up -u mysql://test:test@localhost:3306/tester`
Finished dev [unoptimized + debuginfo] target(s) in 0.15s
Running `target/debug/migration up -u 'mysql://test:test@localhost:3306/tester'`
Applying all pending migrations
Applying migration 'm20220101_000001_users'
Migration 'm20220101_000001_users' has been applied
thread 'main' panicked at /home/jsl/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tracing-subscriber-0.3.18/src/util.rs:91:14:
failed to set global default subscriber: SetGlobalDefaultError("a global default trace dispatcher has already been set")
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Fail to run migration |
Beta Was this translation helpful? Give feedback.
-
Did you find a workaround? I'm facing the same issue trying to call |
Beta Was this translation helpful? Give feedback.
-
Btw this is what I'm doing for now, unless there's a better way (create a " use sea_orm_migration::prelude::*;
/// main migrator, which combines all the other migrations
pub struct MainMigrator;
#[async_trait::async_trait]
impl MigratorTrait for MainMigrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
let mut migrations = vec![];
// get migrations from the specific migrators
migrations.extend(lib_foo::migration::Migrator::migrations());
migrations.extend(lib_bar::migration::Migrator::migrations());
.... add as many migrators as we want ...
// return the combined migrations vector
migrations
}
}
#[async_std::main]
async fn main() {
// execute em!
cli::run_cli(MainMigrator).await;
} |
Beta Was this translation helpful? Give feedback.
-
I'm new to Rust, so I may have missed some obvious hints. If that's the case, please pardon my ignorance. 🙏
I'm looking for a way to implement modular design in my project using SeaORM.
e.g. Each module folder may correspond to a distinct set of features in the system, and they have their migrations, models, controllers, and routes.
However, I had a difficult time finding a way to put migrations into separate module folders. (at least I cannot find it in docs)
Is it possible to achieve this kind of behavior in SeaORM?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions