class PGTrunk::Operations::Functions::CreateFunction

@private

Public Instance Methods

invert() click to toggle source
# File lib/pg_trunk/operations/functions/create_function.rb, line 145
def invert
  irreversible!("replace_existing: true") if replace_existing
  DropFunction.new(**to_h)
end
to_sql(version) click to toggle source
# File lib/pg_trunk/operations/functions/create_function.rb, line 137
def to_sql(version)
  [
    create_function,
    *comment_function,
    register_function(version),
  ].join(" ")
end

Private Instance Methods

comment_function() click to toggle source
# File lib/pg_trunk/operations/functions/create_function.rb, line 176
    def comment_function
      <<~SQL
        COMMENT ON FUNCTION #{name.to_sql(true)}
        IS $comment$#{comment}$comment$;
      SQL
    end
create_function() click to toggle source
# File lib/pg_trunk/operations/functions/create_function.rb, line 152
def create_function
  sql = "CREATE"
  sql << " OR REPLACE" if replace_existing
  sql << " FUNCTION #{name.to_sql(true)}"
  sql << " RETURNS #{name.returns}" if name.returns
  sql << " RETURNS void" if name.returns.blank? && name.args.blank?
  sql << " LANGUAGE #{language || 'sql'}"
  sql << " IMMUTABLE" if volatility == :immutable
  sql << " STABLE" if volatility == :stable
  sql << " VOLATILE" if volatility.blank? || volatility == :volatile
  sql << " LEAKPROOF" if leakproof
  sql << " NOT LEAKPROOF" unless leakproof
  sql << " STRICT" if strict
  sql << " CALLED ON NULL INPUT" if strict == false
  sql << " SECURITY DEFINER" if security == :definer
  sql << " SECURITY INVOKER" if security == :invoker
  sql << " PARALLEL SAFE" if parallel == :safe
  sql << " PARALLEL RESTRICTED" if parallel == :restricted
  sql << " PARALLEL UNSAFE" if parallel.blank? || parallel == :unsafe
  sql << " COST #{cost}" if cost
  sql << " ROWS #{rows}" if rows
  sql << " AS $$#{body}$$;"
end
register_function(version) click to toggle source

Register the most recent ‘oid` of functions with this schema/name There can be several overloaded definitions, but we’re interested in that one we created just now so we can skip checking its args.

# File lib/pg_trunk/operations/functions/create_function.rb, line 186
    def register_function(version)
      function_only = "NOT proisagg AND NOT proiswindow"
      function_only = "prokind = 'f'" if version >= "11"

      <<~SQL.squish
        WITH latest AS (
          SELECT
            oid,
            (
              proname = #{name.quoted} AND pronamespace = #{name.namespace}
            ) AS ok
          FROM pg_proc
          WHERE #{function_only}
          ORDER BY oid DESC LIMIT 1
        )
        INSERT INTO pg_trunk (oid, classid)
          SELECT oid, 'pg_proc'::regclass
          FROM latest
          WHERE ok
        ON CONFLICT DO NOTHING;
      SQL
    end