Sha256: 31778766325f3b35a8dba1fd870b9de9d7034febf11916f315c6491dbb942638

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: false

# @!parse
#   class ActiveRecord::Migration
#     # Change the name and/or schema of a statistics
#     #
#     # @param [#to_s] :name (nil) The qualified name of the statistics
#     # @option options [#to_s] :to (nil) The new qualified name for the statistics
#     # @return [void]
#     #
#     # A custom statistics can be renamed by changing both the name
#     # and the schema (namespace) it belongs to.
#     #
#     # ```ruby
#     # rename_statistics "math.my_stat", to: "public.my_stats"
#     # ```
#     #
#     # The operation is always reversible.
#     def rename_statistics(name, to:); end
#   end
module PGTrunk::Operations::Statistics
  # @private
  class RenameStatistics < Base
    after_initialize { self.new_name ||= generated_name }

    validates :new_name, presence: true
    validates :if_exists, :if_not_exists, :force, absence: true

    def to_sql(_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 STATISTICS #{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 STATISTICS #{changed_name} RENAME TO #{new_name.routine.inspect};"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pg_trunk-0.2.0 lib/pg_trunk/operations/statistics/rename_statistics.rb
pg_trunk-0.1.3 lib/pg_trunk/operations/statistics/rename_statistics.rb