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
1 change: 1 addition & 0 deletions linux/changelog.d/4132.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The sidebar could select the wrong database after a query tab changed databases.
30 changes: 16 additions & 14 deletions linux/crates/app/src/ui/editor/tab_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,16 +237,17 @@ impl SqlEditor {
)
}

/// Completion reads `session`'s catalog: the session the window holds
/// for the tab's database, or `None` back on the connection's own.
pub(crate) fn follow_catalog(&self, session: Option<Arc<Session>>) {
// A session opened late for a database the tab has since left
// changes nothing; the one it is in may have answered first.
let current = self.imp().named_database.borrow().clone();
if session
let database = session
.as_ref()
.is_some_and(|session| session.database() != current.as_deref())
{
.and_then(|session| session.database())
.map(str::to_owned);
self.follow_catalog_for(database.as_deref(), session);
}

pub(crate) fn follow_catalog_for(&self, database: Option<&str>, session: Option<Arc<Session>>) {
// A late catalog must not replace the one for a newer database choice.
if session.is_some() && self.imp().named_database.borrow().as_deref() != database {
return;
}
for provider in self.imp().completion_providers.borrow().iter() {
Expand All @@ -256,17 +257,18 @@ impl SqlEditor {
}
}

/// The database of the session whose catalog completion reads, when
/// the window handed one over.
#[cfg(test)]
pub(crate) fn followed_catalog(&self) -> Option<String> {
pub(crate) fn catalog_session(&self) -> Option<Arc<Session>> {
self.imp().completion_providers.borrow().iter().find_map(|provider| {
let provider = provider.downcast_ref::<SqlCompletionProvider>()?;
let session = provider.imp().catalog_session.borrow().clone()?;
session.database().map(str::to_owned)
provider.imp().catalog_session.borrow().clone()
})
}

#[cfg(test)]
pub(crate) fn followed_catalog(&self) -> Option<String> {
self.catalog_session()?.database().map(str::to_owned)
}

/// Where unqualified names in this tab resolve, for what the assistant
/// says about it.
pub(crate) fn catalog_home(&self) -> CatalogHome {
Expand Down
23 changes: 23 additions & 0 deletions linux/crates/app/src/ui/editor/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3091,6 +3091,29 @@ fn completion_reads_only_the_session_of_the_tabs_database() {
assert_eq!(followed_catalog(&harness), None, "the last database's catalog stayed");
}

#[gtk4::test]
fn a_catalog_reply_keeps_the_database_choice_that_requested_it() {
let harness = databases_editor(answering(), false);
let catalog = session_in("", harness.runtime.tasks());
pick_database(&harness, "analytics");
harness
.editor
.follow_catalog_for(Some("analytics"), Some(catalog.clone()));
assert!(
harness
.editor
.catalog_session()
.is_some_and(|held| Arc::ptr_eq(&held, &catalog))
);

pick_database(&harness, "shop");
harness.editor.follow_catalog_for(Some("analytics"), Some(catalog));
assert!(
harness.editor.catalog_session().is_none(),
"a late lookup restored the old catalog"
);
}

mod result_edits {
use tablepro_core::column::{ColumnDefault, ColumnInfo, ColumnOrigin};
use tablepro_core::edit::CellInput;
Expand Down
121 changes: 102 additions & 19 deletions linux/crates/app/src/ui/session_window/other_databases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use super::{MovedTab, SessionWindow};
use crate::i18n::{gettext, gettext_f};
use crate::runtime::DeliverInput;
use crate::ui::error_text::{connect_error_message, operation_error_message};
use crate::ui::workspace::{DatabaseDrop, Workspace, WorkspaceInput};
use crate::ui::workspace::{DatabaseDrop, EditorInput, Workspace, WorkspaceInput};
use crate::workspace::CloseDecision;

/// The response of the chooser that opens the database picked.
Expand Down Expand Up @@ -335,7 +335,7 @@ impl SessionWindow {
// The connection's tabs in that database read its catalog now.
for other in others {
if let Some(input) = other.sender() {
input.deliver(WorkspaceInput::DatabaseOpened(joined.clone()));
input.deliver(WorkspaceInput::Editor(EditorInput::DatabaseOpened(joined.clone())));
}
}
}
Expand All @@ -349,31 +349,114 @@ impl SessionWindow {
}
}

/// Editor tab `tab_id` of the session under `key` moved to
/// `database`, `None` for the connection's own. Its completion reads
/// the session the window holds for that database, opened beside
/// the connection where a connection's catalog holds one database.
fn follow_database(&self, key: &SessionKey, tab_id: Uuid, database: Option<String>) {
// A tab of another database's own workspace keeps that catalog:
// the connection's session may not know the name of its own.
let SessionKey::Saved(id) = *key else {
let Some(id) = connection_of(key) else {
return;
};
let Some(database) = database else {
self.deliver_to(
key,
WorkspaceInput::Editor(EditorInput::FollowCatalog {
tab_id,
database: None,
session: None,
}),
);
return;
};
if !self.opens_databases(id) {
return;
}
let session = match self.holding(id, database.as_deref()) {
// The tab's own workspace reads that catalog already.
Some((held, _)) if &held == key => None,
Some((_, session)) => Some(session),
None => {
if let Some(database) = database {
self.open_database(id, database);
}
if let Some((held, session)) = self.holding(id, Some(&database)) {
self.deliver_to(
key,
WorkspaceInput::Editor(EditorInput::FollowCatalog {
tab_id,
database: Some(database),
session: (held != *key).then_some(session),
}),
);
return;
}
let Some(root) = self.held_session(&SessionKey::Saved(id)) else {
return;
};
if root.database().is_some_and(|name| !name.is_empty()) {
self.open_database(id, database);
return;
}
self.resolve_default_catalog(key, tab_id, id, database, root);
}

fn resolve_default_catalog(&self, key: &SessionKey, tab_id: Uuid, id: Uuid, database: String, root: Arc<Session>) {
let (Some(state), Some(owner)) = (self.state(), self.held_session(key)) else {
return;
};
let reading = root.clone();
let listing = state.services.tasks.spawn_task(async move {
reading
.run(OperationKind::Metadata, |lease, options| async move {
Ok(lease.list_databases(options).await?)
})
.await
});
let window = self.downgrade();
let key = key.clone();
glib::spawn_future_local(async move {
let listed = listing.await;
let Some(window) = window.upgrade() else {
return;
};
// A replaced connection or a newer tab choice owns the next lookup.
if !window
.held_session(&SessionKey::Saved(id))
.is_some_and(|held| Arc::ptr_eq(&held, &root))
|| !window.held_session(&key).is_some_and(|held| Arc::ptr_eq(&held, &owner))
|| !window
.find_open(|open| (open.claim.key() == &key).then(|| open.workspace.tab_database(tab_id)))
.flatten()
.is_some_and(|current| current == database)
{
return;
}
};
self.deliver_to(key, WorkspaceInput::FollowCatalog { tab_id, session });
let databases = match listed {
Ok(Ok(databases)) => databases,
Ok(Err(error)) => {
window.show_toast(&operation_error_message(&error));
return;
}
Err(failure) => {
tracing::warn!(%failure, "the connection's default database could not be read");
return;
}
};
if databases
.iter()
.any(|listed| listed.is_default && listed.name == database)
{
window.deliver_to(
&key,
WorkspaceInput::Editor(EditorInput::FollowCatalog {
tab_id,
database: Some(database),
session: Some(root),
}),
);
} else {
// A sibling may have joined while the metadata was being read.
match window.holding(id, Some(&database)) {
Some((_, session)) => window.deliver_to(
&key,
WorkspaceInput::Editor(EditorInput::FollowCatalog {
tab_id,
database: Some(database),
session: Some(session),
}),
),
None => window.open_database(id, database),
}
}
});
}

/// A tab of the session under `key` changed the catalog of
Expand Down
Loading
Loading