Skip to content

Commit 4000678

Browse files
committed
bugfix: pop last scope when indenting
1 parent 1e6ab0d commit 4000678

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

lib/activerecord-pg-format-db-structure/indenter.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ def to_s
233233
end
234234

235235
def current_scope_type
236-
@scopes.last.type
236+
@scopes.last&.type
237237
end
238238

239239
def current_token=(token)
@@ -251,7 +251,7 @@ def current_padding
251251
end
252252

253253
def pop_scope
254-
@scopes.pop.type
254+
@scopes.pop&.type
255255
end
256256

257257
def newline
@@ -263,8 +263,10 @@ def append_whitespace
263263
end
264264

265265
def apply_indent
266-
@string << (INDENT_STRING * @scopes.sum(&:indent))
267-
@string << @scopes.last.padding
266+
if @scopes.any?
267+
@string << (INDENT_STRING * @scopes.sum(&:indent))
268+
@string << @scopes.last.padding
269+
end
268270
end
269271

270272
def append_token(rjust: 0)

spec/activerecord-pg-format-db-structure/formatter_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,5 +263,20 @@
263263
);
264264
SQL
265265
end
266+
267+
it "handles filter where clauses" do
268+
formatter = described_class.new
269+
270+
source = +<<~SQL
271+
select range_agg(my_range) filter (where true) from my_table
272+
SQL
273+
274+
expect(formatter.format(source)).to eq(<<~SQL)
275+
SELECT range_agg(my_range) FILTER (
276+
WHERE true
277+
)
278+
FROM my_table;
279+
SQL
280+
end
266281
end
267282
end

0 commit comments

Comments
 (0)