fix(datagrid): keep query result columns in SELECT order (#1565)#1570
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5a3baf65d8
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| guard tabType == .table else { | ||
| guard !binding.columnWidths.isEmpty else { return nil } | ||
| var layout = binding | ||
| layout.columnOrder = nil |
There was a problem hiding this comment.
Keep query column drags from snapping back immediately
When a user drags a column in a query result, tableViewColumnDidMove immediately calls persistColumnLayoutToStorage(), which writes the captured order back through the columnLayout binding; that state change causes updateNSView to call savedColumnLayout and reconcile the pool again. Because this line strips columnOrder for every non-table tab, the next SwiftUI update recomputes schema/SELECT order and moves the column back right after the drag, rather than keeping the drag for the current view as intended.
Useful? React with 👍 / 👎.
Signed-off-by: Ngô Quốc Đạt <datlechin@gmail.com>
Fixes #1565.
Problem
In a SQL/query tab, adding a column in the middle of the SELECT (for example
select id, business_modelthenselect id, state, business_model) showed the new column at the end of the grid instead of its query position. Same effect when a previously dragged column order from an earlier query was reapplied to a different result.Root cause
For query tabs the per-tab layout (
tab.columnLayout) captured acolumnOrderwhenever a column was resized or dragged, and nothing cleared it when the query re-ran with a different column set. On the next grid rebuild that stale order was reapplied and any column not in it was appended at the end. The per-table file persister was never involved; this was the in-memory per-tab layout leaking into a changed result.Fix
Query results now always follow the order written in the SELECT:
savedColumnLayoutno longer restores a saved column order for non-table tabs. It still keeps remembered column widths. Table-tab behavior is unchanged (per-table order still persists).This matches TablePlus, Postico, and Sequel Ace, where query results follow the SELECT and persistent column order is a table-browse feature.
Behavior note
A column drag in a query result is now transient: it holds for the current view and reverts to query order on the next page change, sort, refresh, or re-run. Persistent custom column order remains available on table tabs.
Tests
TableViewCoordinatorLayoutTestsnow covers that a stale saved order is dropped while widths survive, and that an empty layout returns nil.Out of scope
Column order and width are keyed by column name, so a join returning two columns named
idcannot be keyed reliably. This fix sidesteps it for query tabs. A position-stable column identifier would be a proper follow-up.