Sha256: b8e9a38065ac9d5e96e80cf350cd77c03c9536bc4e3c59d2a7fd05573500d384

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: false

# @!method ActiveRecord::Migration#rename_procedure(name, to:)
# Change the name and/or schema of a procedure
#
# @param [#to_s] :name (nil) The qualified name of the procedure
# @option [#to_s] :to (nil) The new qualified name for the procedure
#
# A procedure can be renamed by changing both the name
# and the schema (namespace) it belongs to.
#
# If there are no overloaded procedures, then you can use a plain name:
#
#   rename_procedure "math.set_foo", to: "public.foo_setup"
#
# otherwise the types of attributes must be explicitly specified.
#
#   rename_procedure "math.set_foo(int)", to: "public.foo_setup"
#
# Any specification of attributes in `to:` option
# is ignored because they cannot be changed anyway.
#
# The operation is always reversible.

module PGTrunk::Operations::Procedures
  # @private
  class RenameProcedure < Base
    validates :new_name, presence: true
    validates :body, :if_exists, :replace_existing, :language, :security, absence: true

    def to_sql(version)
      check_version!(version)

      [*change_schema, *change_name].join("; ")
    end

    def invert
      q_new_name = "#{new_name.schema}.#{new_name.routine}(#{name.args}) #{name.returns}"
      self.class.new(**to_h, name: q_new_name.strip, to: name)
    end

    private

    def change_schema
      return if name.schema == new_name.schema

      "ALTER PROCEDURE #{name.to_sql} SET SCHEMA #{new_name.schema.inspect};"
    end

    def change_name
      return if new_name.routine == name.routine

      changed_name = name.merge(schema: new_name.schema).to_sql
      "ALTER PROCEDURE #{changed_name} RENAME TO #{new_name.routine.inspect};"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pg_trunk-0.1.0 lib/pg_trunk/operations/procedures/rename_procedure.rb