Skip to content

Commit

Permalink
馃毟 prevent branch to be removed if used by another branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Odonno committed May 31, 2023
1 parent 9383c17 commit 689fb4d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/branch/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
common::{
create_branch_client, create_branching_feature_client, retrieve_existing_branch_names,
},
constants::BRANCH_TABLE,
constants::{BRANCH_NS, BRANCH_TABLE},
},
input::SurrealdbConfiguration,
models::Branch,
Expand All @@ -21,6 +21,19 @@ pub async fn main(name: String, db_configuration: &SurrealdbConfiguration) -> Re
return Err(anyhow!("Branch {} does not exist", name));
}

// Prevent branch to be removed if used by another branch
let number_of_dependent_branches: Option<u32> = branching_feature_client
.query("SELECT VALUE count() FROM branch WHERE from_ns = $ns AND from_db = $db")
.bind(("ns", BRANCH_NS))
.bind(("db", name.to_string()))
.await?
.take(0)?;
let number_of_dependent_branches = number_of_dependent_branches.unwrap_or(0);

if number_of_dependent_branches > 0 {
return Err(anyhow!("Branch {} is used by another branch", name));
}

// Remove database created for this branch
let client = create_branch_client(&name, &db_configuration).await?;
client
Expand Down
43 changes: 43 additions & 0 deletions tests/cli/branch/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,46 @@ async fn fails_to_remove_if_branch_does_not_exist() -> Result<()> {
})
.await
}

#[tokio::test]
#[serial]
async fn prevent_branch_to_be_removed_if_used_by_another_branch() -> Result<()> {
run_with_surreal_instance_async(|| {
Box::pin(async {
clear_tests_files()?;
scaffold_blog_template()?;
apply_migrations()?;
create_branch("branch-1")?;
create_branch_from_ns_db("branch-2", ("branches", "branch-1"))?;

let mut cmd = create_cmd()?;

cmd.arg("branch").arg("remove").arg("branch-1");

cmd.assert().try_failure().and_then(|assert| {
assert.try_stderr("Error: Branch branch-1 is used by another branch\n")
})?;

// Check "branch" record still exist in surrealdb
let branch: Option<Branch> = get_surrealdb_record(
"features".to_string(),
"branching".to_string(),
"branch".to_string(),
"branch-1".to_string(),
)
.await?;

ensure!(branch.is_some(), "Branch record should still exist");

// Check database is still here in surrealdb
let is_empty =
is_surrealdb_empty(Some("branches".to_string()), Some("branch-1".to_string()))
.await?;

ensure!(!is_empty, "SurrealDB database should not be empty");

Ok(())
})
})
.await
}
18 changes: 18 additions & 0 deletions tests/helpers/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ pub fn create_branch(branch_name: &str) -> Result<()> {
Ok(())
}

pub fn create_branch_from_ns_db(branch_name: &str, ns_db: (&str, &str)) -> Result<()> {
let (ns, db) = ns_db;

let mut cmd = create_cmd()?;
cmd.arg("branch")
.arg("new")
.arg(branch_name)
.arg("--address")
.arg("http://localhost:8000")
.arg("--ns")
.arg(ns)
.arg("--db")
.arg(db);
cmd.assert().try_success()?;

Ok(())
}

pub fn create_cmd() -> Result<Command> {
let cmd = Command::cargo_bin(env!("CARGO_PKG_NAME"))?;
Ok(cmd)
Expand Down

0 comments on commit 689fb4d

Please sign in to comment.