Skip to content

Commit

Permalink
Fix switching branch after current branch is deleted (#1435)
Browse files Browse the repository at this point in the history
There was code that allegedly handled this case, but there wasn't a
test and it didn't work at all.

Fixes #1359.

Co-authored-by: Aljaž Mur Eržen <[email protected]>
  • Loading branch information
msullivan and aljazerzen authored Dec 19, 2024
1 parent 1be6bc3 commit 1207b93
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/branch/connections.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::ops::Deref;

use crate::commands::Options;
use crate::connect::{Connection, Connector};
use crate::connect::{Connection, ConnectionError, Connector};
use crate::print;
use edgedb_errors::UnknownDatabaseError;
use uuid::Uuid;

pub struct BranchConnection<'a> {
Expand Down Expand Up @@ -40,23 +41,21 @@ impl BranchConnection<'_> {

/// Attempts to connect a provided connection, returning `Ok(Some)` if the connection was
/// established, `Ok(None)` if the connection couldn't be established because of a
/// `ClientConnectionFailedError`, and `Err` if any other type of error occurred.
/// `UnknownDatabaseError`, and `Err` if any other type of error occurred.
pub async fn connect_if_branch_exists(connector: &Connector) -> anyhow::Result<Option<Connection>> {
match connector.connect().await {
Ok(c) => Ok(Some(c)),
Err(e) => {
match e.downcast::<edgedb_tokio::Error>() {
Ok(e) => {
if e.code() == 0x_FF_01_01_00 {
// 0x_FF_01_01_00: ClientConnectionFailedError | https://www.edgedb.com/docs/reference/protocol/errors
return Ok(None);
}

Err(e) => match e.downcast::<ConnectionError>() {
Ok(ConnectionError::Error(e)) => {
if e.is::<UnknownDatabaseError>() {
Ok(None)
} else {
Err(e.into())
}
Err(e) => Err(e),
}
}
Ok(e) => Err(e.into()),
Err(e) => Err(e),
},
}
}

Expand Down
22 changes: 22 additions & 0 deletions tests/func/non_interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,28 @@ fn branch_commands() {
.success()
.stdout(predicates::str::contains("test_branch_2"));

// Drop test_branch_2 from main
crate::edgedb_cli_cmd()
.arg("--instance")
.arg(instance_name)
.arg("-b")
.arg("main")
.arg("query")
.arg("drop branch test_branch_2")
.assert()
.context("drop", "drop current branch")
.success();

crate::edgedb_cli_cmd()
.arg("--instance")
.arg(instance_name)
.arg("branch")
.arg("switch")
.arg("main")
.assert()
.context("switch", "when current branch is destroyed")
.success();

// TODO: test how this works in projects
}

Expand Down

0 comments on commit 1207b93

Please sign in to comment.