Skip to content
Merged
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
31 changes: 30 additions & 1 deletion crates/barsql-ui/src/connection_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,9 +613,13 @@ impl ConnectionForm {
this.saving = false;
match result {
Some(Ok(saved)) => {
// That save closed the connection, so what was read through it may be stale.
// That save closed the connection, so the schemas read through it load again.
if !this.original.id.is_empty() && !this.original.connects_like(&saved) {
let loaded = schema::loaded_family(&saved.id, cx);
schema::forget_family(&saved.id, cx);
for id in loaded {
schema::reload(&id, saved.driver.clone(), cx);
}
}
window.close_dialog(cx);
(this.on_saved.clone())(saved, window, cx);
Expand Down Expand Up @@ -1876,6 +1880,31 @@ mod tests {
assert!(!loaded(cx), "new extensions drop it");
}

#[gpui_kit::test]
fn a_save_that_reconnects_loads_a_loaded_schema_again(cx: &mut TestAppContext) {
let env = Env::new(cx);
let id = env.connection.id.clone();
let cx = window(cx);
let loaded = |cx: &mut VisualTestContext| {
cx.update(|_, cx| crate::schema::get(cx, &id).is_some_and(|entry| entry.schemas().is_some()))
};
let edit = |change: fn(&mut ConnectionForm), cx: &mut VisualTestContext| {
let stored = env.bar.list_connections().into_iter().find(|c| c.id == id).unwrap();
let (form, saved) = open_form(Some(stored), cx);
form.update(cx, |form, _| change(form));
form.update_in(cx, |form, window, cx| form.save(window, cx));
settle(cx, |_| !saved.borrow().is_empty());
};
edit(|_| {}, cx);
edit(|form| form.read_only = !form.read_only, cx);
assert!(!env.bar.is_connected(&id) && !loaded(cx), "an unloaded connection stays closed");
cx.update(|_, cx| crate::schema::ensure_loaded(&id, DriverType::Sqlite, cx));
settle(cx, |cx| loaded(cx));
edit(|form| form.read_only = !form.read_only, cx);
settle(cx, |cx| loaded(cx));
assert!(env.bar.is_connected(&id), "a loaded one connects again");
}

#[gpui_kit::test]
fn only_sqlite_shows_extensions(cx: &mut TestAppContext) {
let _env = Env::new(cx);
Expand Down
60 changes: 60 additions & 0 deletions crates/barsql-ui/src/scenarios/engines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use barsql_sql::system_views::{SystemView, ViewScope, views};
use gpui_kit::assets::IconName as Lucide;
use gpui_kit::{Entity, Modifiers, TestAppContext};

use super::connections::row_button;
use super::driver::{Driver, open_with, selector};
use crate::cell_content::Kind;
use crate::cell_viewer::Opened;
Expand Down Expand Up @@ -699,6 +700,63 @@ fn database_changes(cx: &mut TestAppContext, engine: Engine) {
assert_eq!(shown(&mut app), Some(base), "the tree goes back to the connection's own database");
}

fn saved(app: &Driver) -> ConnectionConfig {
app.env.bar.list_connections().into_iter().find(|c| c.id == app.connection.id).expect("saved")
}

// Edit and Save, which fills in the missing schema and so reconnects.
fn reconnecting_save(app: &mut Driver, toggle: Option<&'static str>) {
let (id, before) = (app.connection.id.clone(), saved(app));
row_button(app, "edit", &id);
if let Some(toggle) = toggle {
app.click(toggle);
}
app.click("connection-save");
app.settle_dialog_closed();
assert!(!before.connects_like(&saved(app)), "the dialog filled in the schema");
}

// Show all databases turned on while connected and saved lists them, and the tree loads again.
fn reconnect_lists_databases(cx: &mut TestAppContext, engine: Engine) {
if !engine.driver().capabilities().all_databases {
return;
}
let mut app = open_on(cx, engine);
let row = format!("s:{}", app.schema_name());
app.connect();
tree_row(&mut app, &row);
reconnecting_save(&mut app, Some("conn-all-databases"));
let pane = app.sidebar().read_with(app.cx, |sidebar, _| sidebar.database_pane());
let own = engine.config().default_database();
app.settle(|cx| pane.read_with(cx, |pane, _| pane.listed().contains(&own)));
tree_row(&mut app, &row);
}

// The same save with the tree on another database, which loads again too.
fn reconnect_reloads_shown_database(cx: &mut TestAppContext, engine: Engine) {
if !engine.driver().capabilities().all_databases {
return;
}
let config = ConnectionConfig { show_all_databases: true, ..engine.config() };
let mut app = open_with(cx, |env| env.runtime.block_on(env.bar.save_connection(config)).unwrap());
let second = second_database(&app, engine);
app.connect();
let pane = app.sidebar().read_with(app.cx, |sidebar, _| sidebar.database_pane());
app.settle(|cx| pane.read_with(cx, |pane, _| pane.listed().contains(&second)));
pane.update(app.cx, |pane, _| pane.reveal(&second));
app.cx.run_until_parked();
app.click(selector(format!("database:{second}")));
let shown = app.sidebar().read_with(app.cx, |sidebar, _| sidebar.selected().map(|c| c.id));
assert_eq!(shown, Some(scope::scoped_id(&app.connection.id, &second)));
let row = match engine.driver().capabilities().default_schema {
DefaultSchema::Named(schema) => format!("s:{schema}"),
DefaultSchema::Database => format!("s:{second}"),
};
tree_row(&mut app, &row);
reconnecting_save(&mut app, None);
tree_row(&mut app, &row);
}

macro_rules! per_engine {
($($test:ident => $body:ident;)*) => {
mod postgres {
Expand Down Expand Up @@ -744,6 +802,8 @@ per_engine! {
an_array_cell_opens_as_json => array_cells;
the_databases_list_browses_and_opens_another_database => databases_list;
databases_are_made_renamed_and_dropped_from_the_list => database_changes;
a_save_that_reconnects_lists_the_databases_and_loads_the_tree => reconnect_lists_databases;
a_save_that_reconnects_loads_the_database_the_tree_shows => reconnect_reloads_shown_database;
}

mod postgres_only {
Expand Down
11 changes: 11 additions & 0 deletions crates/barsql-ui/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,17 @@ pub fn forget_family(connection_id: &str, cx: &mut App) {
}
}

// Of a saved connection and the databases opened under it, those loaded or loading.
pub fn loaded_family(connection_id: &str, cx: &App) -> Vec<String> {
let Some(schemas) = cx.try_global::<Schemas>() else { return Vec::new() };
schemas
.connections
.iter()
.filter(|(id, entry)| scope::same_connection(id, connection_id) && (entry.schemas.is_some() || entry.loading))
.map(|(id, _)| id.clone())
.collect()
}

fn load(connection_id: &str, driver: DriverType, cx: &mut App) {
let entry = entry(cx, connection_id);
entry.loading = true;
Expand Down