Bug Description

The log_* methods in the sqlx::ConnectOptions trait are not usable, since they return &mut Self, while Pool::connect_with implementations require an owned reference of the *Option type.

Minimal Reproduction

use log::LevelFilter;
use sqlx::{
    postgres::{PgConnectOptions, PgPoolOptions, PgSslMode},
    ConnectOptions, PgPool,
};

pub async fn connect() -> anyhow::Result<PgPool> {
    let connect_options = PgConnectOptions::new()
        .log_statements(LevelFilter::Debug)
        .host(
            std::env::var("DATABASE_HOST")
                .expect("env var DATABASE_HOST is required")
                .as_ref(),
        )
        .port(5432)
        .username("postgres")
        .password("password")
        .ssl_mode(PgSslMode::Disable);

    // error: mismatched type, expects PgConnectOptions, is &mut PgConnectOptions.
    Ok(PgPool::connect_with(connect_options).await?)
}

Info

  • SQLx version: 0.6.2
0

Unfortunately ergonomics of &mut self and self builders don't mix well, but you can still use the .log_*() methods

pub async fn connect() -> anyhow::Result<PgPool> {
    let mut connect_options = PgConnectOptions::new()
        .host(
            std::env::var("DATABASE_HOST")
                .expect("env var DATABASE_HOST is required")
                .as_ref(),
        )
        .port(5432)
        .username("postgres")
        .password("password")
        .ssl_mode(PgSslMode::Disable);

    connect_options.log_statements(LevelFilter::Debug);

    Ok(PgPool::connect_with(connect_options).await?)
}
1

@LovecraftianHorror fair point, haven't thought of that. Well I've created #2132 to fix that, which I think would be nice to have :)

0
© 2022 pullanswer.com - All rights reserved.