Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

log_* in sqlx::ConnectOptions are not usable #2131

Closed
ar3s3ru opened this issue Oct 2, 2022 · 2 comments · Fixed by #2132
Closed

log_* in sqlx::ConnectOptions are not usable #2131

ar3s3ru opened this issue Oct 2, 2022 · 2 comments · Fixed by #2132
Labels

Comments

@ar3s3ru
Copy link
Contributor

ar3s3ru commented Oct 2, 2022

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
@CosmicHorrorDev
Copy link
Contributor

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?)
}

@ar3s3ru
Copy link
Contributor Author

ar3s3ru commented Oct 2, 2022

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

@ar3s3ru ar3s3ru closed this as completed Feb 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants