Sha256: bc17c50407357ed6438f6a09fcd2bd9496e19bf2c123c4c14f8a3235a69c6a01

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: false

# @!parse
#   class ActiveRecord::Migration
#     # Rename a rule
#     #
#     # @param [#to_s] table (nil) The qualified name of the table
#     # @param [#to_s] name (nil) The current name of the rule
#     # @option options [#to_s] :to (nil) The new name for the rule
#     # @yield [c] the block with the constraint's definition
#     # @yieldparam Object receiver of methods specifying the constraint
#     # @return [void]
#     #
#     # A constraint can be identified by the table and explicit name
#     #
#     # ```ruby
#     # rename_rule :users, "_forbid_insertion", to: "_skip_insertion"
#     # ```
#     #
#     # Alternatively the name can be got from the event and kind.
#     #
#     # ```ruby
#     # rename_rule :users, to: "_skip_insertion" do |r|
#     #   r.event :insert
#     #   r.kind :instead
#     # end
#     # ```
#     #
#     # The name can be reset to auto-generated when
#     # the `:to` option is missed or blank:
#     #
#     # ```ruby
#     # rename_rule :users, "_skip_insertion" do |r|
#     #   r.event :insert
#     #   r.kind :instead
#     # end
#     # ```
#     #
#     # The operation is always reversible.
#     def rename_rule(table, name = nil, **options, &block); end
#   end
module PGTrunk::Operations::Rules
  # @private
  class RenameRule < Base
    after_initialize { self.new_name = generated_name if new_name.blank? }

    validates :new_name, presence: true
    validates :where, :command, :replace_existing, :force, :if_exists,
              absence: true

    def to_sql(_version)
      <<~SQL
        ALTER RULE #{name.to_sql} ON #{table.to_sql}
        RENAME TO #{new_name.to_sql};
      SQL
    end

    def invert
      self.class.new(**to_h, name: new_name, to: name)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pg_trunk-0.1.3 lib/pg_trunk/operations/rules/rename_rule.rb