Skip to content
Merged
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
64 changes: 51 additions & 13 deletions src/commands/databases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,33 @@ fn partition_keys(values: &[String]) -> Result<Vec<serde_json::Value>, String> {
.collect()
}

/// Build the request body for `POST /v1/databases/{id}/schemas/{schema}/tables`,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super nit: add_table lost its doc comment (not blocking). The sentence on line 960 describes the databases tables add command, so it now sits on the wrong function. Move that sentence back above add_table.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in de0e182. The sentence is back above add_table.

/// leaving out every empty list so the server applies its defaults.
fn table_declaration_body(
table: &str,
key: &[String],
key_determines: &[String],
sorted_by: &[serde_json::Value],
partition_by: &[serde_json::Value],
) -> serde_json::Value {
let mut body = serde_json::json!({ "name": table });
if !key.is_empty() {
body["key"] = serde_json::json!(key);
}
if !key_determines.is_empty() {
// The API calls this `constant_per_key`; the flag keeps its original
// name. The server rejects unknown fields, so the old wire name fails.
body["constant_per_key"] = serde_json::json!(key_determines);
}
if !sorted_by.is_empty() {
body["sorted_by"] = serde_json::json!(sorted_by);
}
if !partition_by.is_empty() {
body["partition_by"] = serde_json::json!(partition_by);
}
body
}

/// `databases tables add` — declare a table on an existing instant database.
#[allow(clippy::too_many_arguments)]
pub fn add_table(
Expand Down Expand Up @@ -1001,19 +1028,7 @@ pub fn add_table(
let api = Api::new(Some(workspace_id));
let db = resolve_database(&api, &database);

let mut body = serde_json::json!({ "name": table });
if !key.is_empty() {
body["key"] = serde_json::json!(key);
}
if !key_determines.is_empty() {
body["key_determines"] = serde_json::json!(key_determines);
}
if !sorted_by.is_empty() {
body["sorted_by"] = serde_json::json!(sorted_by);
}
if !partition_by.is_empty() {
body["partition_by"] = serde_json::json!(partition_by);
}
let body = table_declaration_body(table, key, key_determines, &sorted_by, &partition_by);

let (status, resp) = declare_table(&api, &db.id, schema, &body);

Expand Down Expand Up @@ -4091,6 +4106,29 @@ mod tests {
mock.assert();
}

#[test]
fn table_declaration_body_sends_key_determines_as_constant_per_key() {
let cols = |v: &[&str]| v.iter().map(|s| s.to_string()).collect::<Vec<_>>();
assert_eq!(
table_declaration_body(
"orders",
&cols(&["order_id"]),
&cols(&["event_date"]),
&[],
&[]
),
serde_json::json!({
"name": "orders",
"key": ["order_id"],
"constant_per_key": ["event_date"],
})
);
assert_eq!(
table_declaration_body("orders", &[], &[], &[], &[]),
serde_json::json!({"name": "orders"})
);
}

#[test]
fn fork_database_request_defaults_name_to_source_label_fork() {
let to_json = |r| serde_json::to_value(&r).unwrap();
Expand Down
Loading