Skip to content
Open
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 .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ jobs:
run: python -m pip install psycopg2-binary xmltodict
- name: Run smoketests
# Note: clear_database and replication only work in private
run: python -m smoketests ${{ matrix.smoketest_args }} -x clear_database replication
run: python -m smoketests ${{ matrix.smoketest_args }} -x clear_database replication teams
- name: Stop containers (Linux)
if: always() && runner.os == 'Linux'
run: docker compose down
Expand Down
8 changes: 8 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ tar = "0.4"
tempdir = "0.3.7"
tempfile = "3.20"
termcolor = "1.2.0"
termtree = "0.5.1"
thin-vec = "0.2.13"
thiserror = "1.0.37"
tokio = { version = "1.37", features = ["full"] }
Expand Down
4 changes: 4 additions & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ tabled.workspace = true
tar.workspace = true
tempfile.workspace = true
termcolor.workspace = true
termtree.workspace = true
thiserror.workspace = true
tokio.workspace = true
tokio-tungstenite.workspace = true
Expand All @@ -83,6 +84,9 @@ quick-xml.workspace = true
names.workspace = true
notify.workspace = true

[dev-dependencies]
pretty_assertions.workspace = true

[target.'cfg(not(target_env = "msvc"))'.dependencies]
tikv-jemallocator = { workspace = true }
tikv-jemalloc-ctl = { workspace = true }
Expand Down
152 changes: 146 additions & 6 deletions crates/cli/src/subcommands/delete.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
use std::io;

use crate::common_args;
use crate::config::Config;
use crate::util::{add_auth_header_opt, database_identity, get_auth_header};
use crate::util::{add_auth_header_opt, database_identity, get_auth_header, y_or_n, AuthHeader};
use clap::{Arg, ArgMatches};
use http::StatusCode;
use itertools::Itertools as _;
use reqwest::Response;
use spacetimedb_client_api_messages::http::{DatabaseDeleteConfirmationResponse, DatabaseTree, DatabaseTreeNode};
use spacetimedb_lib::Hash;
use tokio::io::AsyncWriteExt as _;

pub fn cli() -> clap::Command {
clap::Command::new("delete")
Expand All @@ -22,11 +30,143 @@ pub async fn exec(mut config: Config, args: &ArgMatches) -> Result<(), anyhow::E
let force = args.get_flag("force");

let identity = database_identity(&config, database, server).await?;

let builder = reqwest::Client::new().delete(format!("{}/v1/database/{}", config.get_host_url(server)?, identity));
let host_url = config.get_host_url(server)?;
let request_path = format!("{host_url}/v1/database/{identity}");
let auth_header = get_auth_header(&mut config, false, server, !force).await?;
let builder = add_auth_header_opt(builder, &auth_header);
builder.send().await?.error_for_status()?;
let client = reqwest::Client::new();

let response = send_request(&client, &request_path, &auth_header, None).await?;
match response.status() {
StatusCode::PRECONDITION_REQUIRED => {
let confirm = response.json::<DatabaseDeleteConfirmationResponse>().await?;
println!("WARNING: Deleting the database {identity} will also delete its children!");
if !force {
print_database_tree_info(&confirm.database_tree).await?;
}
if y_or_n(force, "Do you want to proceed deleting above databases?")? {
send_request(&client, &request_path, &auth_header, Some(confirm.confirmation_token))
.await?
.error_for_status()?;
} else {
println!("Aborting");
}

Ok(())
}
StatusCode::OK => Ok(()),
_ => response.error_for_status().map(drop).map_err(Into::into),
}
}

async fn send_request(
client: &reqwest::Client,
request_path: &str,
auth: &AuthHeader,
confirmation_token: Option<Hash>,
) -> Result<Response, reqwest::Error> {
let mut builder = client.delete(request_path);
builder = add_auth_header_opt(builder, auth);
if let Some(token) = confirmation_token {
builder = builder.query(&[("token", token)]);
}
builder.send().await
}

async fn print_database_tree_info(tree: &DatabaseTree) -> io::Result<()> {
tokio::io::stdout()
.write_all(as_termtree(tree).to_string().as_bytes())
.await
}

fn as_termtree(tree: &DatabaseTree) -> termtree::Tree<String> {
let mut stack: Vec<(&DatabaseTree, bool)> = vec![];
stack.push((tree, false));

let mut built: Vec<termtree::Tree<String>> = <_>::default();

while let Some((node, visited)) = stack.pop() {
if visited {
let mut term_node = termtree::Tree::new(fmt_tree_node(&node.root));
term_node.leaves = built.drain(built.len() - node.children.len()..).collect();
term_node.leaves.reverse();
built.push(term_node);
} else {
stack.push((node, true));
stack.extend(node.children.iter().rev().map(|child| (child, false)));
}
}

built
.pop()
.expect("database tree contains a root and we pushed it last")
}

fn fmt_tree_node(node: &DatabaseTreeNode) -> String {
format!(
"{}{}",
node.database_identity,
if node.database_names.is_empty() {
<_>::default()
} else {
format!(": {}", node.database_names.iter().join(", "))
}
)
}

#[cfg(test)]
mod tests {
use super::*;
use spacetimedb_client_api_messages::http::{DatabaseTree, DatabaseTreeNode};
use spacetimedb_lib::{sats::u256, Identity};

Ok(())
#[test]
fn render_termtree() {
let tree = DatabaseTree {
root: DatabaseTreeNode {
database_identity: Identity::ONE,
database_names: ["parent".into()].into(),
},
children: vec![
DatabaseTree {
root: DatabaseTreeNode {
database_identity: Identity::from_u256(u256::new(2)),
database_names: ["child".into()].into(),
},
children: vec![
DatabaseTree {
root: DatabaseTreeNode {
database_identity: Identity::from_u256(u256::new(3)),
database_names: ["grandchild".into()].into(),
},
children: vec![],
},
DatabaseTree {
root: DatabaseTreeNode {
database_identity: Identity::from_u256(u256::new(5)),
database_names: [].into(),
},
children: vec![],
},
],
},
DatabaseTree {
root: DatabaseTreeNode {
database_identity: Identity::from_u256(u256::new(4)),
database_names: ["sibling".into(), "bro".into()].into(),
},
children: vec![],
},
],
};
pretty_assertions::assert_eq!(
"\
0000000000000000000000000000000000000000000000000000000000000001: parent
├── 0000000000000000000000000000000000000000000000000000000000000004: bro, sibling
└── 0000000000000000000000000000000000000000000000000000000000000002: child
├── 0000000000000000000000000000000000000000000000000000000000000005
└── 0000000000000000000000000000000000000000000000000000000000000003: grandchild
",
&as_termtree(&tree).to_string()
);
}
}
Loading
Loading