Skip to content

Commit 0c817fd

Browse files
authored
Add spaces between functions and triggers in schema.rb (#195)
I've been dealing with the changes to schema_dumper in Rails 8.1 and Senic 1.9 and found that it's most common for their methods not to emit a newline after their last operation. Hence I think it's safe to assume that functions() and triggers() should emit a newline before their loops and within their loops, up to the last iteration. And, not having these functions emit a final newline holds with the pattern used by the Rails and Scenic schema_dumpers. I've tested this in my own apps with a patch on this gem and the spacing is correct with the new Rails 8.1 schema changes (and with Scenic emitting `create_view` statements before the `create_function` and `create_trigger` statements).
1 parent 1ae616d commit 0c817fd

File tree

2 files changed

+60
-6
lines changed

2 files changed

+60
-6
lines changed

lib/fx/schema_dumper.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,16 @@ def functions(stream)
2121
dumpable_functions_in_database = Fx.database.functions
2222

2323
dumpable_functions_in_database.each do |function|
24+
stream.puts
2425
stream.puts(function.to_schema)
2526
end
26-
27-
stream.puts if dumpable_functions_in_database.any?
2827
end
2928

3029
def triggers(stream)
3130
dumpable_triggers_in_database = Fx.database.triggers
3231

33-
if dumpable_triggers_in_database.any?
34-
stream.puts
35-
end
36-
3732
dumpable_triggers_in_database.each do |trigger|
33+
stream.puts
3834
stream.puts(trigger.to_schema)
3935
end
4036
end

spec/fx/schema_dumper_spec.rb

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,64 @@
159159
connection.schema_search_path = "public"
160160
end
161161

162+
it "puts a blank line before each function and trigger" do
163+
connection.create_table :users do |t|
164+
t.string :name
165+
t.string :upper_name
166+
t.string :down_name
167+
end
168+
Fx.database.create_function <<~SQL
169+
CREATE OR REPLACE FUNCTION uppercase_users_name()
170+
RETURNS trigger AS $$
171+
BEGIN
172+
NEW.upper_name = UPPER(NEW.name);
173+
RETURN NEW;
174+
END;
175+
$$ LANGUAGE plpgsql;
176+
SQL
177+
sql_definition = <<~SQL
178+
CREATE TRIGGER uppercase_users_name
179+
BEFORE INSERT ON users
180+
FOR EACH ROW
181+
EXECUTE FUNCTION uppercase_users_name();
182+
SQL
183+
connection.create_trigger(
184+
:uppercase_users_name,
185+
sql_definition: sql_definition
186+
)
187+
Fx.database.create_function <<~SQL
188+
CREATE OR REPLACE FUNCTION downcase_users_name()
189+
RETURNS trigger AS $$
190+
BEGIN
191+
NEW.down_name = LOWER(NEW.name);
192+
RETURN NEW;
193+
END;
194+
$$ LANGUAGE plpgsql;
195+
SQL
196+
sql_definition = <<~SQL
197+
CREATE TRIGGER downcase_users_name
198+
BEFORE INSERT ON users
199+
FOR EACH ROW
200+
EXECUTE FUNCTION downcase_users_name();
201+
SQL
202+
connection.create_trigger(
203+
:downcase_users_name,
204+
sql_definition: sql_definition
205+
)
206+
stream = StringIO.new
207+
output = stream.string
208+
209+
dump(connection: connection, stream: stream)
210+
211+
pattern = /(?<delimiter>end|SQL)\n\n (?<statement>create_function|create_trigger)/
212+
# a blank line between:
213+
# - the table and the uppercase_users_name function,
214+
# - the uppercase_users_name function and the downcase_users_name function,
215+
# - the downcase_users_name function and the uppercase_users_name trigger,
216+
# - the uppercase_users_name trigger and the downcase_users_name trigger,
217+
expect(output.scan(pattern).size).to eq(4)
218+
end
219+
162220
def dump(connection:, stream:)
163221
if Rails.version >= "7.2"
164222
ActiveRecord::SchemaDumper.dump(connection.pool, stream)

0 commit comments

Comments
 (0)