Consistently name db connection connection
This commit is contained in:
@@ -143,9 +143,9 @@ pub struct DatabaseEditPrivsArgs {
|
||||
|
||||
pub async fn handle_command(
|
||||
command: DatabaseCommand,
|
||||
mut conn: MySqlConnection,
|
||||
mut connection: MySqlConnection,
|
||||
) -> anyhow::Result<()> {
|
||||
let result = conn
|
||||
let result = connection
|
||||
.transaction(|txn| {
|
||||
Box::pin(async move {
|
||||
match command {
|
||||
@@ -159,14 +159,14 @@ pub async fn handle_command(
|
||||
})
|
||||
.await;
|
||||
|
||||
close_database_connection(conn).await;
|
||||
close_database_connection(connection).await;
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
async fn create_databases(
|
||||
args: DatabaseCreateArgs,
|
||||
conn: &mut MySqlConnection,
|
||||
connection: &mut MySqlConnection,
|
||||
) -> anyhow::Result<()> {
|
||||
if args.name.is_empty() {
|
||||
anyhow::bail!("No database names provided");
|
||||
@@ -174,7 +174,7 @@ async fn create_databases(
|
||||
|
||||
for name in args.name {
|
||||
// TODO: This can be optimized by fetching all the database privileges in one query.
|
||||
if let Err(e) = create_database(&name, conn).await {
|
||||
if let Err(e) = create_database(&name, connection).await {
|
||||
eprintln!("Failed to create database '{}': {}", name, e);
|
||||
eprintln!("Skipping...");
|
||||
}
|
||||
@@ -183,14 +183,17 @@ async fn create_databases(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn drop_databases(args: DatabaseDropArgs, conn: &mut MySqlConnection) -> anyhow::Result<()> {
|
||||
async fn drop_databases(
|
||||
args: DatabaseDropArgs,
|
||||
connection: &mut MySqlConnection,
|
||||
) -> anyhow::Result<()> {
|
||||
if args.name.is_empty() {
|
||||
anyhow::bail!("No database names provided");
|
||||
}
|
||||
|
||||
for name in args.name {
|
||||
// TODO: This can be optimized by fetching all the database privileges in one query.
|
||||
if let Err(e) = drop_database(&name, conn).await {
|
||||
if let Err(e) = drop_database(&name, connection).await {
|
||||
eprintln!("Failed to drop database '{}': {}", name, e);
|
||||
eprintln!("Skipping...");
|
||||
}
|
||||
@@ -199,8 +202,11 @@ async fn drop_databases(args: DatabaseDropArgs, conn: &mut MySqlConnection) -> a
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn list_databases(args: DatabaseListArgs, conn: &mut MySqlConnection) -> anyhow::Result<()> {
|
||||
let databases = get_database_list(conn).await?;
|
||||
async fn list_databases(
|
||||
args: DatabaseListArgs,
|
||||
connection: &mut MySqlConnection,
|
||||
) -> anyhow::Result<()> {
|
||||
let databases = get_database_list(connection).await?;
|
||||
|
||||
if databases.is_empty() {
|
||||
println!("No databases to show.");
|
||||
@@ -220,15 +226,15 @@ async fn list_databases(args: DatabaseListArgs, conn: &mut MySqlConnection) -> a
|
||||
|
||||
async fn show_database_privileges(
|
||||
args: DatabaseShowPrivsArgs,
|
||||
conn: &mut MySqlConnection,
|
||||
connection: &mut MySqlConnection,
|
||||
) -> anyhow::Result<()> {
|
||||
let database_users_to_show = if args.name.is_empty() {
|
||||
get_all_database_privileges(conn).await?
|
||||
get_all_database_privileges(connection).await?
|
||||
} else {
|
||||
// TODO: This can be optimized by fetching all the database privileges in one query.
|
||||
let mut result = Vec::with_capacity(args.name.len());
|
||||
for name in args.name {
|
||||
match get_database_privileges(&name, conn).await {
|
||||
match get_database_privileges(&name, connection).await {
|
||||
Ok(db) => result.extend(db),
|
||||
Err(e) => {
|
||||
eprintln!("Failed to show database '{}': {}", name, e);
|
||||
@@ -417,12 +423,12 @@ fn format_privileges_line(
|
||||
|
||||
pub async fn edit_privileges(
|
||||
args: DatabaseEditPrivsArgs,
|
||||
conn: &mut MySqlConnection,
|
||||
connection: &mut MySqlConnection,
|
||||
) -> anyhow::Result<()> {
|
||||
let privilege_data = if let Some(name) = &args.name {
|
||||
get_database_privileges(name, conn).await?
|
||||
get_database_privileges(name, connection).await?
|
||||
} else {
|
||||
get_all_database_privileges(conn).await?
|
||||
get_all_database_privileges(connection).await?
|
||||
};
|
||||
|
||||
let privileges_to_change = if !args.privs.is_empty() {
|
||||
@@ -530,7 +536,7 @@ pub async fn edit_privileges(
|
||||
};
|
||||
|
||||
for row in privileges_to_change.iter() {
|
||||
if !user_exists(&row.user, conn).await? {
|
||||
if !user_exists(&row.user, connection).await? {
|
||||
// TODO: allow user to return and correct their mistake
|
||||
anyhow::bail!("User {} does not exist", row.user);
|
||||
}
|
||||
@@ -545,7 +551,7 @@ pub async fn edit_privileges(
|
||||
|
||||
// TODO: Add confirmation prompt.
|
||||
|
||||
apply_privilege_diffs(diffs, conn).await?;
|
||||
apply_privilege_diffs(diffs, connection).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@@ -177,12 +177,12 @@ pub async fn main() -> anyhow::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn show_db(name: &str, conn: &mut MySqlConnection) -> anyhow::Result<()> {
|
||||
async fn show_db(name: &str, connection: &mut MySqlConnection) -> anyhow::Result<()> {
|
||||
// NOTE: mysql-dbadm show has a quirk where valid database names
|
||||
// for non-existent databases will report with no users.
|
||||
// This function should *not* check for db existence, only
|
||||
// validate the names.
|
||||
let privileges = database_privilege_operations::get_database_privileges(name, conn)
|
||||
let privileges = database_privilege_operations::get_database_privileges(name, connection)
|
||||
.await
|
||||
.unwrap_or(vec![]);
|
||||
|
||||
|
@@ -74,8 +74,11 @@ pub struct UserShowArgs {
|
||||
json: bool,
|
||||
}
|
||||
|
||||
pub async fn handle_command(command: UserCommand, mut conn: MySqlConnection) -> anyhow::Result<()> {
|
||||
let result = conn
|
||||
pub async fn handle_command(
|
||||
command: UserCommand,
|
||||
mut connection: MySqlConnection,
|
||||
) -> anyhow::Result<()> {
|
||||
let result = connection
|
||||
.transaction(|txn| {
|
||||
Box::pin(async move {
|
||||
match command {
|
||||
@@ -88,18 +91,21 @@ pub async fn handle_command(command: UserCommand, mut conn: MySqlConnection) ->
|
||||
})
|
||||
.await;
|
||||
|
||||
close_database_connection(conn).await;
|
||||
close_database_connection(connection).await;
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
async fn create_users(args: UserCreateArgs, conn: &mut MySqlConnection) -> anyhow::Result<()> {
|
||||
async fn create_users(
|
||||
args: UserCreateArgs,
|
||||
connection: &mut MySqlConnection,
|
||||
) -> anyhow::Result<()> {
|
||||
if args.username.is_empty() {
|
||||
anyhow::bail!("No usernames provided");
|
||||
}
|
||||
|
||||
for username in args.username {
|
||||
if let Err(e) = create_database_user(&username, conn).await {
|
||||
if let Err(e) = create_database_user(&username, connection).await {
|
||||
eprintln!("{}", e);
|
||||
eprintln!("Skipping...\n");
|
||||
continue;
|
||||
@@ -120,7 +126,7 @@ async fn create_users(args: UserCreateArgs, conn: &mut MySqlConnection) -> anyho
|
||||
username,
|
||||
password_file: None,
|
||||
},
|
||||
conn,
|
||||
connection,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
@@ -129,13 +135,13 @@ async fn create_users(args: UserCreateArgs, conn: &mut MySqlConnection) -> anyho
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn drop_users(args: UserDeleteArgs, conn: &mut MySqlConnection) -> anyhow::Result<()> {
|
||||
async fn drop_users(args: UserDeleteArgs, connection: &mut MySqlConnection) -> anyhow::Result<()> {
|
||||
if args.username.is_empty() {
|
||||
anyhow::bail!("No usernames provided");
|
||||
}
|
||||
|
||||
for username in args.username {
|
||||
if let Err(e) = delete_database_user(&username, conn).await {
|
||||
if let Err(e) = delete_database_user(&username, connection).await {
|
||||
eprintln!("{}", e);
|
||||
eprintln!("Skipping...");
|
||||
}
|
||||
@@ -156,7 +162,7 @@ pub fn read_password_from_stdin_with_double_check(username: &str) -> anyhow::Res
|
||||
|
||||
async fn change_password_for_user(
|
||||
args: UserPasswdArgs,
|
||||
conn: &mut MySqlConnection,
|
||||
connection: &mut MySqlConnection,
|
||||
) -> anyhow::Result<()> {
|
||||
// NOTE: although this also is checked in `set_password_for_database_user`, we check it here
|
||||
// to provide a more natural order of error messages.
|
||||
@@ -172,16 +178,16 @@ async fn change_password_for_user(
|
||||
read_password_from_stdin_with_double_check(&args.username)?
|
||||
};
|
||||
|
||||
set_password_for_database_user(&args.username, &password, conn).await?;
|
||||
set_password_for_database_user(&args.username, &password, connection).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn show_users(args: UserShowArgs, conn: &mut MySqlConnection) -> anyhow::Result<()> {
|
||||
async fn show_users(args: UserShowArgs, connection: &mut MySqlConnection) -> anyhow::Result<()> {
|
||||
let unix_user = get_current_unix_user()?;
|
||||
|
||||
let users = if args.username.is_empty() {
|
||||
get_all_database_users_for_unix_user(&unix_user, conn).await?
|
||||
get_all_database_users_for_unix_user(&unix_user, connection).await?
|
||||
} else {
|
||||
let mut result = vec![];
|
||||
for username in args.username {
|
||||
@@ -191,7 +197,7 @@ async fn show_users(args: UserShowArgs, conn: &mut MySqlConnection) -> anyhow::R
|
||||
continue;
|
||||
}
|
||||
|
||||
let user = get_database_user_for_user(&username, conn).await?;
|
||||
let user = get_database_user_for_user(&username, connection).await?;
|
||||
if let Some(user) = user {
|
||||
result.push(user);
|
||||
} else {
|
||||
@@ -205,7 +211,7 @@ async fn show_users(args: UserShowArgs, conn: &mut MySqlConnection) -> anyhow::R
|
||||
for user in users.iter() {
|
||||
user_databases.insert(
|
||||
user.user.clone(),
|
||||
get_databases_where_user_has_privileges(&user.user, conn).await?,
|
||||
get_databases_where_user_has_privileges(&user.user, connection).await?,
|
||||
);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user