class PGTrunk::Operations::Procedures::CreateProcedure

@private

Public Instance Methods

invert() click to toggle source
# File lib/pg_trunk/operations/procedures/create_procedure.rb, line 113
def invert
  irreversible!("replace_existing: true") if replace_existing
  DropProcedure.new(**to_h)
end
to_sql(version) click to toggle source
# File lib/pg_trunk/operations/procedures/create_procedure.rb, line 106
def to_sql(version)
  # Procedures were added to PostgreSQL in v11
  check_version!(version)

  [create_proc, *comment_proc, register_proc].join(" ")
end

Private Instance Methods

comment_proc() click to toggle source
# File lib/pg_trunk/operations/procedures/create_procedure.rb, line 129
    def comment_proc
      <<~SQL
        COMMENT ON PROCEDURE #{name.to_sql(true)}
        IS $comment$#{comment}$comment$;
      SQL
    end
create_proc() click to toggle source
# File lib/pg_trunk/operations/procedures/create_procedure.rb, line 120
def create_proc
  sql = "CREATE"
  sql << " OR REPLACE" if replace_existing
  sql << " PROCEDURE #{name.to_sql(true)}"
  sql << " LANGUAGE #{language&.downcase || 'sql'}"
  sql << " SECURITY DEFINER" if security == :definer
  sql << " AS $$#{body}$$;"
end
register_proc() click to toggle source

Register the most recent ‘oid` of procedures 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/procedures/create_procedure.rb, line 139
    def register_proc
      <<~SQL.squish
        WITH latest AS (
          SELECT
            oid,
            (proname = #{name.quoted} AND pronamespace = #{name.namespace}) AS ok
          FROM pg_proc
          WHERE prokind = 'p'
          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