diff --git a/CHANGELOG.md b/CHANGELOG.md index 006632c..fc660dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## [Unreleased] +- Fix bug with aggregate filter where clause + ## [0.6.0] - 2025-08-21 - Add preprocessors config that allows applying text transforms to the input before parsing diff --git a/lib/activerecord-pg-format-db-structure/indenter.rb b/lib/activerecord-pg-format-db-structure/indenter.rb index eff1f0b..5e73001 100644 --- a/lib/activerecord-pg-format-db-structure/indenter.rb +++ b/lib/activerecord-pg-format-db-structure/indenter.rb @@ -125,6 +125,8 @@ def indent output.append_token output.pop_scope output.append_scope(type:, indent: 0) + in { current_token: WHERE, inside: PARENS } + output.append_token in { current_token: FROM | WHERE | GROUP | ORDER | WINDOW | HAVING | LIMIT | OFFSET | FETCH | FOR | UNION | INTERSECT | EXCEPT => token_type diff --git a/spec/activerecord-pg-format-db-structure/formatter_spec.rb b/spec/activerecord-pg-format-db-structure/formatter_spec.rb index 4ac68a4..295eb16 100644 --- a/spec/activerecord-pg-format-db-structure/formatter_spec.rb +++ b/spec/activerecord-pg-format-db-structure/formatter_spec.rb @@ -263,5 +263,21 @@ ); SQL end + + it "handles filter where clauses" do + formatter = described_class.new + + source = +<<~SQL + select range_agg(my_range) filter (where true), + range_agg(my_range) filter (where false) + from my_table + SQL + + expect(formatter.format(source)).to eq(<<~SQL) + SELECT range_agg(my_range) FILTER (WHERE true), + range_agg(my_range) FILTER (WHERE false) + FROM my_table; + SQL + end end end