From f994ae603a471f3821fffc8706101f10c8dc7415 Mon Sep 17 00:00:00 2001 From: Carl Mercier Date: Tue, 29 Aug 2023 11:09:11 -0400 Subject: [PATCH] Implement check_function_bodies only for Postgres --- lib/fx/adapters/postgres.rb | 14 +++++++++++++- lib/fx/configuration.rb | 16 ++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/fx/adapters/postgres.rb b/lib/fx/adapters/postgres.rb index fe6681f3..7d36f37e 100644 --- a/lib/fx/adapters/postgres.rb +++ b/lib/fx/adapters/postgres.rb @@ -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. @@ -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 diff --git a/lib/fx/configuration.rb b/lib/fx/configuration.rb index bd158fe5..15eb4d8b 100644 --- a/lib/fx/configuration.rb +++ b/lib/fx/configuration.rb @@ -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 <>;` 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. + # + # 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