database config just work with `default` and `global`
Rocket Version
0.5.1
Operating System
macOS 15.4.1
Rust Toolchain Version
rustc 1.86.0 (05f9846f8 2025-03-31)
What happened?
database pool config can not read [debug] config. just support [default],[global].
Test Case
use rocket::fairing::{self, AdHoc};
use rocket::{Build, Rocket};
use rocket_db_pools::{sqlx, Database};
#[derive(Database)]
#[database("postgres")]
struct Db(sqlx::PgPool);
async fn run_migrations(rocket: Rocket<Build>) -> fairing::Result {
match Db::fetch(&rocket) {
Some(db) => match sqlx::migrate!("src/sqlx/migrations").run(&**db).await {
Ok(_) => Ok(rocket),
Err(e) => {
error!("Failed to initialize SQLx database: {}", e);
Err(rocket)
}
},
None => Err(rocket),
}
}
pub trait ConfigureDbRocket {
fn configure_db(self) -> Self;
}
impl ConfigureDbRocket for Rocket<Build> {
fn configure_db(self) -> Self {
self.attach(Db::init())
.attach(AdHoc::try_on_ignite("SQLx Migrations", run_migrations))
}
}
#[launch]
fn rocket() -> _ {
rocket::custom(Config::default_figment())
.attach(AdHoc::config::<rocket::Config>())
.attach(AdHoc::config::<Config>())
.configure_db()
}
Log Output
Error: failed to initialize database: bad configuration: missing field `url`
Error: Attempted to fetch unattached database `moke::sqlx::Db`.
>> `moke::sqlx::Db::init()` fairing must be attached prior to using this database.
Error: failed to initialize database: bad configuration: missing field `url`
Error: Rocket failed to launch due to failing fairings:
>> 'postgres' Database Pool
>> SQLx Migrations
>> 'redis' Database Pool
thread 'main' panicked at /Users/icode/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/rocket-0.5.1/src/error.rs:279:9:
aborting due to fairing failure(s)
stack backtrace:
0: rust_begin_unwind
at /rustc/05f9846f893b09a1be1fc8560e33fc3c815cfecb/library/std/src/panicking.rs:695:5
1: core::panicking::panic_fmt
at /rustc/05f9846f893b09a1be1fc8560e33fc3c815cfecb/library/core/src/panicking.rs:75:14
2: core::panicking::panic_display
at /Volumes/Data/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/panicking.rs:261:5
3: <rocket::error::Error as core::ops::drop::Drop>::drop::panic_cold_display
at /Volumes/Data/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/panic.rs:100:13
4: <rocket::error::Error as core::ops::drop::Drop>::drop
at /Users/icode/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/rocket-0.5.1/src/error.rs:279:9
5: core::ptr::drop_in_place<rocket::error::Error>
at /Volumes/Data/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/ptr/mod.rs:523:1
6: core::ptr::drop_in_place<core::result::Result<rocket::rkt::Rocket<rocket::phase::Ignite>,rocket::error::Error>>
at /Volumes/Data/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/ptr/mod.rs:523:1
7: moke::main
at ./backend/src/main.rs:62:1
8: core::ops::function::FnOnce::call_once
at /Volumes/Data/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
Additional Context
No response
System Checks
- [x] My bug report relates to functionality.
- [x] I have tested against the latest Rocket release or a recent git commit.
- [x] I have tested against the latest stable
rustctoolchain. - [x] I was unable to find this issue previously reported.
Error: failed to initialize database: bad configuration: missing field
url
It looks like it can't find the url value for your db. To debug this issue, I'd need to know more information about your Config type, and how you're setting up the figment provider. However, my first guess is that you aren't setting the profile at all. Rocket doesn't set the profile for you in a custom config (It's part of the Figment provider instead). Rocket's default provider looks like this:
Figment::from(Config::default())
.merge(Toml::file(Env::var_or("ROCKET_CONFIG", "Rocket.toml")).nested())
.merge(Env::prefixed("ROCKET_").ignore(&["PROFILE"]).global())
.select(Profile::from_env_or("ROCKET_PROFILE", Self::DEFAULT_PROFILE))
You might want to do something similar.
yes, i use like this:
Figment::from(Config::default())
.merge(Toml::file(Env::var_or("ROCKET_CONFIG", "Rocket.toml")).nested())
.merge(Env::prefixed("ROCKET_").ignore(&["PROFILE"]).global())
.select(Profile::from_env_or("ROCKET_PROFILE", Self::DEFAULT_PROFILE))
and is use ROCKET_PROFILE="debug" evn var