Sha256: 2f659b137cb4b51a60a8a8c39cc61e553a21b1abf839a07ddc0740f2e56cf3bd

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: false

# @!method ActiveRecord::Migration#rename_statistics(name, to:)
# Change the name and/or schema of a statistics
#
# @param [#to_s] :name (nil) The qualified name of the statistics
# @option [#to_s] :to (nil) The new qualified name for the statistics
#
# A custom statistics can be renamed by changing both the name
# and the schema (namespace) it belongs to.
#
#   rename_statistics "math.my_stat", to: "public.my_stats"
#
# The operation is always reversible.

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

1 entries across 1 versions & 1 rubygems

Version Path
pg_trunk-0.1.0 lib/pg_trunk/operations/statistics/rename_statistics.rb