|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Sequel extension that adds default ORDER BY id to model queries. |
| 4 | +# |
| 5 | +# Hooks into fetch methods (all, each, first) to add ORDER BY id just before |
| 6 | +# execution. This ensures ordering is only added to the final query, not to |
| 7 | +# subqueries or compound query parts. |
| 8 | +# |
| 9 | +# Skips default ordering when: |
| 10 | +# - Query already has explicit ORDER BY |
| 11 | +# - Query is incompatible (GROUP BY, compounds, DISTINCT ON, from_self) |
| 12 | +# - Query is schema introspection (LIMIT 0) |
| 13 | +# - Model doesn't have id as primary key |
| 14 | +# - id is not in the select list |
| 15 | +# |
| 16 | +# For JOIN queries with SELECT *, uses qualified column (table.id) to avoid |
| 17 | +# ambiguity. |
| 18 | +# |
| 19 | +# Ensures deterministic query results for consistent API responses and |
| 20 | +# reliable test behavior. |
| 21 | +# |
| 22 | +# Usage: |
| 23 | +# DB.extension(:sql_comments) |
| 24 | +# DB.extension(:default_order_by_id) |
| 25 | +# |
| 26 | +module Sequel |
| 27 | + module DefaultOrderById |
| 28 | + module DatasetMethods |
| 29 | + def all(*, &) |
| 30 | + ds = default_order_by_id |
| 31 | + return super unless ds |
| 32 | + |
| 33 | + ds.all(*, &) |
| 34 | + end |
| 35 | + |
| 36 | + def each(*, &) |
| 37 | + ds = default_order_by_id |
| 38 | + return super unless ds |
| 39 | + |
| 40 | + ds.each(*, &) |
| 41 | + end |
| 42 | + |
| 43 | + def first(*, &) |
| 44 | + ds = default_order_by_id |
| 45 | + return super unless ds |
| 46 | + |
| 47 | + ds.first(*, &) |
| 48 | + end |
| 49 | + |
| 50 | + private |
| 51 | + |
| 52 | + def default_order_by_id |
| 53 | + id_col = id_column_for_order |
| 54 | + return unless id_col |
| 55 | + |
| 56 | + order(id_col).comment('default_order_by_id') |
| 57 | + end |
| 58 | + |
| 59 | + def id_column_for_order |
| 60 | + return if already_ordered? || incompatible_with_order? || not_a_data_query? || !model_has_id_primary_key? |
| 61 | + |
| 62 | + find_id_column |
| 63 | + end |
| 64 | + |
| 65 | + def already_ordered? |
| 66 | + opts[:order] |
| 67 | + end |
| 68 | + |
| 69 | + def incompatible_with_order? |
| 70 | + opts[:group] || # Aggregated results don't have individual ids |
| 71 | + opts[:compounds] || # Compound queries (e.g. UNION) have own ordering |
| 72 | + distinct_on? || # DISTINCT ON requires matching ORDER BY |
| 73 | + from_self? # Outer query handles ordering |
| 74 | + end |
| 75 | + |
| 76 | + def distinct_on? |
| 77 | + opts[:distinct].is_a?(Array) && opts[:distinct].any? |
| 78 | + end |
| 79 | + |
| 80 | + def from_self? |
| 81 | + opts[:from].is_a?(Array) && opts[:from].any? { |f| f.is_a?(Sequel::SQL::AliasedExpression) && f.expression.is_a?(Sequel::Dataset) } |
| 82 | + end |
| 83 | + |
| 84 | + def not_a_data_query? |
| 85 | + opts[:limit] == 0 # Schema introspection query |
| 86 | + end |
| 87 | + |
| 88 | + def model_has_id_primary_key? |
| 89 | + return false unless respond_to?(:model) && model |
| 90 | + |
| 91 | + model.primary_key == :id |
| 92 | + end |
| 93 | + |
| 94 | + def find_id_column |
| 95 | + select_cols = opts[:select] |
| 96 | + |
| 97 | + if select_cols.nil? || select_cols.empty? |
| 98 | + # SELECT * includes id |
| 99 | + if opts[:join] |
| 100 | + # Qualify to avoid ambiguity with joined tables |
| 101 | + return Sequel.qualify(model.table_name, :id) |
| 102 | + end |
| 103 | + |
| 104 | + return :id |
| 105 | + end |
| 106 | + |
| 107 | + select_cols.each do |col| |
| 108 | + # SELECT table.* includes id |
| 109 | + return :id if col.is_a?(Sequel::SQL::ColumnAll) && col.table == model.table_name |
| 110 | + |
| 111 | + id_col = extract_id_column(col) |
| 112 | + return id_col if id_col |
| 113 | + end |
| 114 | + |
| 115 | + nil |
| 116 | + end |
| 117 | + |
| 118 | + def extract_id_column(col) |
| 119 | + return col if id_expression?(col) |
| 120 | + |
| 121 | + return col.alias if col.is_a?(Sequel::SQL::AliasedExpression) && id_expression?(col.expression) |
| 122 | + |
| 123 | + nil |
| 124 | + end |
| 125 | + |
| 126 | + def id_expression?(expr) |
| 127 | + case expr |
| 128 | + when Symbol |
| 129 | + expr == :id || expr.to_s.end_with?('__id') |
| 130 | + when Sequel::SQL::Identifier |
| 131 | + expr.value == :id |
| 132 | + when Sequel::SQL::QualifiedIdentifier |
| 133 | + expr.column == :id |
| 134 | + else |
| 135 | + false |
| 136 | + end |
| 137 | + end |
| 138 | + end |
| 139 | + end |
| 140 | + |
| 141 | + Dataset.register_extension(:default_order_by_id, DefaultOrderById::DatasetMethods) |
| 142 | +end |
0 commit comments