Skip to content

Commit 89addc7

Browse files
Prevent extra newlines after table generation block in shema.rb (rails#48731)
* Prevent extra newlines after table generation block in shema.rb file when there are no foreign keys or indexes. This will produce valid code without linting issues (issue: Trailing whitespace detected) * fix linting issues * fix linting Co-authored-by: Rafael Mendonça França <[email protected]>
1 parent 23db60b commit 89addc7

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

activerecord/lib/active_record/schema_dumper.rb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,24 @@ def schemas(stream)
127127
def tables(stream)
128128
sorted_tables = @connection.tables.sort
129129

130-
sorted_tables.each do |table_name|
131-
table(table_name, stream) unless ignored?(table_name)
130+
not_ignored_tables = sorted_tables.reject { |table_name| ignored?(table_name) }
131+
132+
not_ignored_tables.each_with_index do |table_name, index|
133+
table(table_name, stream)
134+
stream.puts if index < not_ignored_tables.count - 1
132135
end
133136

134137
# dump foreign keys at the end to make sure all dependent tables exist.
135-
if @connection.use_foreign_keys?
136-
sorted_tables.each do |tbl|
137-
foreign_keys(tbl, stream) unless ignored?(tbl)
138+
if @connection.supports_foreign_keys?
139+
foreign_keys_stream = StringIO.new
140+
not_ignored_tables.each do |tbl|
141+
foreign_keys(tbl, foreign_keys_stream)
138142
end
143+
144+
foreign_keys_string = foreign_keys_stream.string
145+
stream.puts if foreign_keys_string.length > 0
146+
147+
stream.print foreign_keys_string
139148
end
140149
end
141150

@@ -196,7 +205,6 @@ def table(table, stream)
196205
unique_constraints_in_create(table, tbl) if @connection.supports_unique_constraints?
197206

198207
tbl.puts " end"
199-
tbl.puts
200208

201209
stream.print tbl.string
202210
rescue => e

0 commit comments

Comments
 (0)