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

Support renaming SQL databases #116

Merged
merged 2 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ openssl = { version = "0.10" }

[workspace.dependencies]
tracing = { version = "0.1", features = ["log"] }
cloud-openapi = { git = "https://github.com/fermyon/cloud-openapi", rev = "5e1f78041927dafc186ef6fa06ebe93bdec37fe7" }
cloud-openapi = { git = "https://github.com/fermyon/cloud-openapi", rev = "3e70369e3fc9574e262827332157da40da0a4f66" }

[build-dependencies]
vergen = { version = "^8.2.1", default-features = false, features = [
Expand Down
10 changes: 8 additions & 2 deletions crates/cloud/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use cloud_openapi::{
revisions_api::{api_revisions_get, api_revisions_post},
sql_databases_api::{
api_sql_databases_create_post, api_sql_databases_database_links_delete,
api_sql_databases_database_links_post, api_sql_databases_delete,
api_sql_databases_execute_post, api_sql_databases_get,
api_sql_databases_database_links_post, api_sql_databases_database_rename_patch,
api_sql_databases_delete, api_sql_databases_execute_post, api_sql_databases_get,
},
variable_pairs_api::{
api_variable_pairs_delete, api_variable_pairs_get, api_variable_pairs_post,
Expand Down Expand Up @@ -472,6 +472,12 @@ impl Client {
.await
.map_err(format_response_error)
}

pub async fn rename_database(&self, database: String, new_name: String) -> anyhow::Result<()> {
api_sql_databases_database_rename_patch(&self.configuration, &database, &new_name, None)
.await
.map_err(format_response_error)
}
}

#[derive(Deserialize, Debug)]
Expand Down
36 changes: 36 additions & 0 deletions src/commands/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub enum SqliteCommand {
Execute(ExecuteCommand),
/// List all NoOps SQL databases of a user
List(ListCommand),
/// Rename a NoOps SQL database
Rename(RenameCommand),
}

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -62,6 +64,18 @@ pub struct ExecuteCommand {
common: CommonArgs,
}

#[derive(Parser, Debug)]
pub struct RenameCommand {
/// Current name of database to rename
name: String,

/// New name for the database
new_name: String,

#[clap(flatten)]
common: CommonArgs,
}

fn disallow_empty(statement: &str) -> anyhow::Result<String> {
if statement.trim().is_empty() {
anyhow::bail!("cannot be empty");
Expand Down Expand Up @@ -140,6 +154,7 @@ impl SqliteCommand {
Self::Delete(cmd) => cmd.run().await,
Self::Execute(cmd) => cmd.run().await,
Self::List(cmd) => cmd.run().await,
Self::Rename(cmd) => cmd.run().await,
}
}
}
Expand Down Expand Up @@ -304,6 +319,27 @@ struct ResourceLabelJson<'a> {
app: &'a str,
}

impl RenameCommand {
pub async fn run(self) -> Result<()> {
let client = create_cloud_client(self.common.deployment_env_id.as_deref()).await?;
let list = CloudClient::get_databases(&client, None)
.await
.context("Problem fetching databases")?;
let found = list.iter().find(|d| d.name == self.name);
if found.is_none() {
anyhow::bail!("No database found with name \"{}\"", self.name);
}
client
.rename_database(self.name.clone(), self.new_name.clone())
.await?;
println!(
"Database \"{}\" is now named \"{}\"",
self.name, self.new_name
);
Ok(())
}
}

/// Print apps optionally filtering to a specifically supplied app and/or database
fn print_apps<'a>(
mut links: Vec<Link>,
Expand Down