Skip to content

fix: order lane cells by the board query's order like columns - #230

Merged
ManukMinasyan merged 3 commits into
4.xfrom
fix/lanes-honour-query-order
Nov 13, 2026
Merged

ManukMinasyan merged 3 commits into
4.xfrom
fix/lanes-honour-query-order

Conversation

@ManukMinasyan

@ManukMinasyan ManukMinasyan commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

A board whose query already carries an orderBy() was sorted with lanes off and lost that sort as soon as lanes were switched on: lane cells always fell back to manual position. Lane cells now order their cards by the board query's own orders first, then by position and key, exactly as columns do.

What changed

HasBoardGroupings::getLaneRecordsQuery() reads the orders (and their order bindings) from the filtered, lane-scoped query before it clears them. The protected helper compileBoardOrders() compiles them with the connection grammar. They go first in the row_number() over (partition by <column> order by ...) window, ahead of position and key. The per-cell limit and the final flowforge_cell_row ordering come from that ranking, so the top N of a cell is now the top N by the board order. flowforge_cell_total, laneHasCards() and the count paths are untouched.

How each stored order shape is compiled:

  • A plain string column that is qualified (table.column) is compiled as is.
  • A bare plain column that is a real column of the model's table is compiled qualified with that table. A window order by sees only input columns, so a bare name shared with a joined table (for example latest() with a joined created_at) would be ambiguous there while it works in a flat query.
  • A bare plain column that is not a column of the model's table, or that a selected alias of the same name shadows, is an output alias or an unqualified joined column. The whole query falls back to position order. A plain ORDER BY resolves a bare name to the output alias first, so compiling the table column would sort differently from the flat board.
  • A raw order, an Expression order or a subquery order is guarded by selectAliases() and mentionsAlias(). Every select item is collected: each top-level comma-separated part of a raw select, quoted and unquoted aliases, and selectSub and withCount aliases. The order SQL is then checked, outside string literals, for an alias as a whole token or as a quoted identifier. A token followed by ( is a function call and does not count.
  • Orders with bindings move from the order bucket to the end of the select bucket, where the window expression sits, so select, from, where and order bindings stay aligned. The orders are removed from the inner query as before, so nothing is bound twice.

Why this way

The window function applies the per-cell limit, so the order has to live in it. Ordering only the outer query would keep the top N by position and merely re-sort them. Global scope orders are deliberately not read: they are added by applyScopes() after the orders are captured, and an existing test pins that they never reach lane queries. Output aliases cannot be referenced inside a window on Postgres, which is why a bare non-column name falls back instead of being compiled.

Behaviour to know about

  • A board query that already has an orderBy now orders lane cells too. Hosts that relied on lanes ignoring a query order will see cards reorder.
  • With no order on the query, the generated lane SQL and bindings are unchanged. A test pins the exact string.
  • Every fallback is all-or-nothing: the whole query uses position order. It happens for a bare name that is not a column of the model's table (a selected alias such as a withCount column) or that a selected alias shadows, an inOrderOf order, and a raw or expression order that uses a selected alias by name. A raw order that only calls a function with an alias's name, such as count(*), is honoured.
  • distinct rule: the window then sits on a wrapper subquery, so an order is honoured only when it is a plain column of the model's own table that the select list includes (table.*, *, or the column by name). Any other order falls back to position order.
  • A bare order column that exists only on a joined table, or a JSON path such as meta->rank, falls back too. To get it into lanes, qualify the column or use orderByRaw.
  • Write select aliases with as when a raw order refers to them. An alias with no as and no closing parenthesis (selectRaw('tasks.estimated_hours hrs')) is not detected, so a raw order on it reaches the window and Postgres rejects it.
  • Drag and drop: while the board query carries an order, a card dragged within a cell is written a position between neighbours that are not in position order. The host should refuse or ignore reorders inside a cell while the board sorts, the same as for flat columns. The docs say this.
  • No migration, no config key, no new public API.

Tests

All in tests/Modern/Feature/GroupingTest.php, with order shapes added to the TestGroupedBoard and TestCastGroupedBoard fixtures. Tests marked pgsql run in CI's PostgreSQL job.

  • Unchanged SQL and bindings with no order, for a plain query and a withCount query (pgsql).
  • Single column desc, position as tie-breaker (seeded so position order and id order differ), two orders, each cell ordering its own cards, and the cell limit keeping the top N by the new order with the full total (pgsql).
  • orderByRaw with a binding, a subquery order with a binding, and a fromSub base query whose subquery select carries a binding, each beside select, where and order bindings (pgsql). A mutation that put the order bindings first in the select bucket fails the first two.
  • A bare order column shadowed by a selected alias (lower(title) as title) falls back to position order (pgsql).
  • A joined select('tasks.*') board with latest(), and a bare orderBy('id') shared with a joined table (pgsql).
  • Several selected aliases (plain and raw order on the first one), and a quoted alias with a space, each falling back to position order (pgsql).
  • A raw order calling count(*) while count is an alias is honoured, and a raw order with a string literal equal to an alias is honoured (pgsql).
  • distinct: an honoured own-table column, and the fallbacks for a raw order and for an unselected joined column, seeded with distinct values so honouring would visibly reorder (SQLite only: the fixture selects a json column under distinct, which Postgres rejects regardless of this change).

Checked

At 8246dafcf: the full suite on SQLite, 685 passed. pest --group=pgsql against a scratch PostgreSQL database, 86 passed. pint --test passes. CI on this head: all jobs pass (PHP 8.3 and 8.4 on Laravel 12 and 13, PostgreSQL, Browser chromium and webkit, code styling, docs build, without panels). PHPStan reports the same 10 errors as a clean 4.x checkout, none in HasBoardGroupings.php, baseline untouched. The new alias and ambiguity tests failed on the earlier heads (ade1d83f5, 99b6dec1c) before their fixes.

Not checked

  • Orders inside union queries (unionOrders) are not read.
  • An alias written without as is detected only when it follows a closing parenthesis (see Behaviour to know about).
  • To close the Postgres gap on distinct, run it against a fixture without a json column.

Copilot AI balanced review requested due to automatic review settings November 12, 2026 22:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@ManukMinasyan
ManukMinasyan merged commit 91dc5f1 into 4.x Nov 13, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants