Only pass the mysql config portion when connecting

This commit is contained in:
Oystein Kristoffer Tveit 2024-08-08 19:40:57 +02:00
parent 452fa22e9c
commit e2193f47c1
Signed by: oysteikt
GPG Key ID: 9F2F7D8250F35146
4 changed files with 19 additions and 11 deletions

View File

@ -5,7 +5,7 @@ use crate::{
cli::{database_command, mysql_admutils_compatibility::common::filter_db_or_user_names},
core::{
common::{yn, DbOrUser},
config::{get_config, mysql_connection_from_config, GlobalConfigArgs},
config::{create_mysql_connection_from_config, get_config, GlobalConfigArgs},
database_operations::{create_database, drop_database, get_database_list},
database_privilege_operations,
},
@ -126,7 +126,7 @@ pub async fn main() -> anyhow::Result<()> {
};
let config = get_config(args.config_overrides)?;
let mut connection = mysql_connection_from_config(config).await?;
let mut connection = create_mysql_connection_from_config(config.mysql).await?;
match command {
Command::Create(args) => {

View File

@ -5,7 +5,7 @@ use crate::{
cli::{mysql_admutils_compatibility::common::filter_db_or_user_names, user_command},
core::{
common::{close_database_connection, get_current_unix_user, DbOrUser},
config::{get_config, mysql_connection_from_config, GlobalConfigArgs},
config::{create_mysql_connection_from_config, get_config, GlobalConfigArgs},
user_operations::*,
},
};
@ -86,7 +86,7 @@ pub async fn main() -> anyhow::Result<()> {
};
let config = get_config(args.config_overrides)?;
let mut connection = mysql_connection_from_config(config).await?;
let mut connection = create_mysql_connection_from_config(config.mysql).await?;
match command {
Command::Create(args) => {

View File

@ -5,6 +5,9 @@ use clap::Parser;
use serde::{Deserialize, Serialize};
use sqlx::{mysql::MySqlConnectOptions, ConnectOptions, MySqlConnection};
// NOTE: this might look empty now, and the extra wrapping for the mysql
// config seems unnecessary, but it will be useful later when we
// add more configuration options.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Config {
pub mysql: MysqlConfig,
@ -57,6 +60,8 @@ pub struct GlobalConfigArgs {
mysql_connect_timeout: Option<u64>,
}
/// Use the arguments and whichever configuration file which might or might not
/// be found and default values to determine the configuration for the program.
pub fn get_config(args: GlobalConfigArgs) -> anyhow::Result<Config> {
let config_path = PathBuf::from(args.config_file);
@ -94,14 +99,17 @@ pub fn get_config(args: GlobalConfigArgs) -> anyhow::Result<Config> {
})
}
pub async fn mysql_connection_from_config(config: Config) -> anyhow::Result<MySqlConnection> {
/// Use the provided configuration to establish a connection to a MySQL server.
pub async fn create_mysql_connection_from_config(
config: MysqlConfig,
) -> anyhow::Result<MySqlConnection> {
match tokio::time::timeout(
Duration::from_secs(config.mysql.timeout.unwrap_or(DEFAULT_TIMEOUT)),
Duration::from_secs(config.timeout.unwrap_or(DEFAULT_TIMEOUT)),
MySqlConnectOptions::new()
.host(&config.mysql.host)
.username(&config.mysql.username)
.password(&config.mysql.password)
.port(config.mysql.port.unwrap_or(DEFAULT_PORT))
.host(&config.host)
.username(&config.username)
.password(&config.password)
.port(config.port.unwrap_or(DEFAULT_PORT))
.database("mysql")
.connect(),
)

View File

@ -66,7 +66,7 @@ async fn main() -> anyhow::Result<()> {
let args: Args = Args::parse();
let config = core::config::get_config(args.config_overrides)?;
let connection = core::config::mysql_connection_from_config(config).await?;
let connection = core::config::create_mysql_connection_from_config(config.mysql).await?;
let result = match args.command {
Command::Db(command) => cli::database_command::handle_command(command, connection).await,