Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/fx/adapters/postgres.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def triggers
#
# @return [void]
def create_function(sql_definition)
execute sql_definition
execute with_check_function_bodies(sql_definition)
end

# Creates a trigger in the database.
Expand Down Expand Up @@ -162,6 +162,18 @@ def support_drop_function_without_args
pg_connection = connectable.connection.raw_connection
pg_connection.server_version >= 10_00_00
end

def with_check_function_bodies(sql_definition)
should_wrap = [true, false].include?(Fx.configuration.check_function_bodies)
return sql_definition unless should_wrap

<<-SQL
BEGIN;
SET LOCAL check_function_bodies TO #{Fx.configuration.check_function_bodies};
#{sql_definition}
COMMIT;
SQL
end
end
end
end
16 changes: 16 additions & 0 deletions lib/fx/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,25 @@ class Configuration
# @return Boolean
attr_accessor :dump_functions_at_beginning_of_schema

# Check function bodies during creation by issuing
# `SET LOCAL check_function_bodies TO <<value>>;` before creating functions.
#
# This is only useful with the Postgresql adapter.
#
# See https://stackoverflow.com/a/36983831 and
# https://www.postgresql.org/docs/9.5/runtime-config-client.html#GUC-CHECK-FUNCTION-BODIES
#
# Set to `nil` to use the database default configuration.
# The default and recommended value is `false` to mimic pg_dump's behavior.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure about this. The docs say:

This parameter is normally on.

So maybe the default should be true here, with the possibility to set it to false for specific use cases?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could do that. It's off by default for pg_dump from what I remember, and this gem is the closest thing to pg_dump so it seemed reasonable to follow the pg_dump conventions. Either way works. Just having the option would be a huge win!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#
# Defaults to false
# @return Boolean, nil
attr_accessor :check_function_bodies

def initialize
@database = Fx::Adapters::Postgres.new
@dump_functions_at_beginning_of_schema = false
@check_function_bodies = false
end
end
end