Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,11 @@ pub enum DatabasesCommands {
#[arg(long = "table")]
tables: Vec<String>,

/// When the database expires. Accepts a relative duration (e.g. 24h, 7d, 90m)
/// or an RFC 3339 timestamp. Defaults to 24h when omitted.
#[arg(long)]
expires_at: Option<String>,

/// Output format
#[arg(long = "output", short = 'o', default_value = "table", value_parser = ["table", "json", "yaml"])]
output: String,
Expand Down
21 changes: 16 additions & 5 deletions src/databases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ pub fn create_database_request(
description: Option<&str>,
schema: &str,
tables: &[String],
expires_at: Option<&str>,
) -> serde_json::Value {
let mut req = serde_json::Map::new();

Expand All @@ -151,6 +152,13 @@ pub fn create_database_request(
);
}

if let Some(exp) = expires_at {
req.insert(
"expires_at".to_string(),
serde_json::Value::String(exp.to_string()),
Comment thread
eddietejeda marked this conversation as resolved.
);
}
Comment thread
eddietejeda marked this conversation as resolved.

serde_json::Value::Object(req)
}

Expand Down Expand Up @@ -414,11 +422,12 @@ pub fn create(
description: Option<&str>,
schema: &str,
tables: &[String],
expires_at: Option<&str>,
format: &str,
) {
use crossterm::style::Stylize;

let body = create_database_request(description, schema, tables);
let body = create_database_request(description, schema, tables, expires_at);

let api = ApiClient::new(Some(workspace_id));
let spinner = (format == "table").then(|| crate::util::spinner("Creating database..."));
Expand Down Expand Up @@ -643,13 +652,13 @@ mod tests {

#[test]
fn create_database_request_empty_without_description_or_tables() {
let req = create_database_request(None, "public", &[]);
let req = create_database_request(None, "public", &[], None);
assert_eq!(req, serde_json::json!({}));
}

#[test]
fn create_database_request_includes_description() {
let req = create_database_request(Some("my db"), "public", &[]);
let req = create_database_request(Some("my db"), "public", &[], None);
assert_eq!(req["description"], "my db");
assert!(req.get("schemas").is_none());
}
Expand All @@ -660,6 +669,7 @@ mod tests {
Some("sales"),
"public",
&["orders".to_string(), "customers".to_string()],
None,
);
assert_eq!(req["description"], "sales");
assert_eq!(req["schemas"][0]["name"], "public");
Expand All @@ -669,7 +679,7 @@ mod tests {

#[test]
fn create_database_request_schemas_without_description() {
let req = create_database_request(None, "analytics", &["events".to_string()]);
let req = create_database_request(None, "analytics", &["events".to_string()], None);
assert!(req.get("description").is_none());
assert_eq!(req["schemas"][0]["name"], "analytics");
}
Expand Down Expand Up @@ -855,13 +865,14 @@ mod tests {
Some("mydb"),
"public",
&["gdp".to_string()],
None,
))
.unwrap(),
))
.create();

let api = ApiClient::test_new(&server.url(), "k", Some("ws-test"));
let body = create_database_request(Some("mydb"), "public", &["gdp".to_string()]);
let body = create_database_request(Some("mydb"), "public", &["gdp".to_string()], None);
let (status, resp_body) = api.post_raw("/databases", &body);
assert_eq!(status.as_u16(), 201);
let parsed: CreateDatabaseResponse = serde_json::from_str(&resp_body).unwrap();
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,12 +387,14 @@ fn main() {
description,
schema,
tables,
expires_at,
output,
}) => databases::create(
&workspace_id,
description.as_deref(),
&schema,
&tables,
expires_at.as_deref(),
&output,
),
Some(DatabasesCommands::Set { id_or_description }) => {
Expand Down
Loading