Skip to content

Commit

Permalink
Branches: Print current branch/database in REPL (#1258)
Browse files Browse the repository at this point in the history
* print current branch/database

* clippy

* cache current database

* update tests to expect 'main' for branch
  • Loading branch information
quinchs authored Mar 25, 2024
1 parent d1de4af commit f6168f7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ pub fn main(options: Options, cfg: Config) -> Result<(), anyhow::Error> {
initial_text: "".into(),
edgeql_state_desc: RawTypedesc::uninitialized(),
edgeql_state: State::empty(),
current_database: None,
};
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
Expand Down
32 changes: 28 additions & 4 deletions src/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub struct State {
pub initial_text: String,
pub edgeql_state_desc: RawTypedesc,
pub edgeql_state: EdgeqlState,
pub current_database: Option<String>,
}

impl PromptRpc {
Expand Down Expand Up @@ -180,6 +181,7 @@ impl State {
self.connection = Some(conn);
self.read_state();
self.set_idle_transaction_timeout().await?;
self.current_database = self.try_get_current_database().await?;
Ok(())
}
pub async fn soft_reconnect(&mut self) -> anyhow::Result<()> {
Expand Down Expand Up @@ -241,13 +243,18 @@ impl State {
{
use TransactionState::*;

let txstate = match self.connection.as_ref().map(|c| c.transaction_state()) {
let txstate = match self.connection.as_mut().map(|c| c.transaction_state()) {
Some(NotInTransaction) => "",
Some(InTransaction) => TX_MARKER,
Some(InFailedTransaction) => FAILURE_MARKER,
None => "",
};

let current_database = match self.try_get_current_database().await? {
Some(db) => db,
None => self.get_current_database().await?
};

let inst = self.conn_params.get()?.instance_name().to_owned();

let location = match inst {
Expand All @@ -256,16 +263,16 @@ impl State {
"{}/{}:{}",
org,
name,
self.database,
current_database,
),
Some(edgedb_tokio::InstanceName::Local(name)) =>
format!(
"{}:{}",
name,
self.database,
current_database,
),
_ =>
format!("{}", self.database)
format!("{}", current_database)
};

let prompt = format!("{}{}> ", location, txstate);
Expand All @@ -278,6 +285,23 @@ impl State {
}
}).await
}

pub async fn get_current_database(&mut self) -> anyhow::Result<String> {
self.ensure_connection().await?;
Ok(self.try_get_current_database().await?.unwrap())
}

pub async fn try_get_current_database(&mut self) -> anyhow::Result<Option<String>> {
if self.connection.is_none() {
return Ok(None);
}

Ok(
self.connection.as_mut().unwrap()
.query_required_single("select sys::get_current_database()", &()).await?
)
}

pub async fn input_mode(&mut self, value: InputMode) -> anyhow::Result<()>
{
self.input_mode = value;
Expand Down
12 changes: 6 additions & 6 deletions tests/func/interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use crate::{Config, SERVER};
#[test]
fn simple_query() -> Result<(), Box<dyn Error>> {
let mut cmd = SERVER.admin_interactive();
cmd.exp_string("edgedb>")?;
cmd.exp_string("main>")?;
cmd.send_line("SELECT 'abc'++'def';\n")?;
cmd.exp_string("abcdef")?;
cmd.exp_string("edgedb>")?;
cmd.exp_string("main>")?;
cmd.send_line(" SELECT 'xy'++'z';\n")?;
cmd.exp_string("xyz")?;
Ok(())
Expand All @@ -17,7 +17,7 @@ fn simple_query() -> Result<(), Box<dyn Error>> {
#[test]
fn two_queries() -> Result<(), Box<dyn Error>> {
let mut cmd = SERVER.admin_interactive();
cmd.exp_string("edgedb>")?;
cmd.exp_string("main>")?;
cmd.send_line("SELECT 'AB'++'C'; SELECT 'XY'++'Z';\n")?;
cmd.exp_string("ABC")?;
cmd.exp_string("XYZ")?;
Expand All @@ -27,7 +27,7 @@ fn two_queries() -> Result<(), Box<dyn Error>> {
#[test]
fn create_report() -> Result<(), Box<dyn Error>> {
let mut cmd = SERVER.admin_interactive();
cmd.exp_string("edgedb>")?;
cmd.exp_string("main>")?;
cmd.send_line("CREATE TYPE default::Type1;\n")?;
cmd.exp_string("OK: CREATE")?;
Ok(())
Expand All @@ -42,7 +42,7 @@ limit = 2
let mut cmd = SERVER.custom_interactive(|cmd| {
cmd.env("XDG_CONFIG_HOME", config.path());
});
cmd.exp_string("edgedb>")?;
cmd.exp_string("main>")?;
cmd.send_line("SELECT {'abc', 'def', 'fgh'};\n")?;
cmd.exp_string("...")?;

Expand All @@ -53,7 +53,7 @@ limit = 3
let mut cmd = SERVER.custom_interactive(|cmd| {
cmd.env("XDG_CONFIG_HOME", config.path());
});
cmd.exp_string("edgedb>")?;
cmd.exp_string("main>")?;
cmd.send_line("SELECT {'abc', 'def', 'fgh'};\n")?;
cmd.exp_string("{")?;
cmd.exp_string("fgh")?;
Expand Down

0 comments on commit f6168f7

Please sign in to comment.