Skip to content

database config just work with default and global #2936

@icode

Description

@icode

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

  • My bug report relates to functionality.
  • I have tested against the latest Rocket release or a recent git commit.
  • I have tested against the latest stable rustc toolchain.
  • I was unable to find this issue previously reported.

Metadata

Metadata

Assignees

No one assigned

    Labels

    triageA bug report being investigated

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions