Sha256: a8d9130ad67cc08dde0d5f88c61edf975b3c3664b6d8385e91317031caf97ebd

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: false

# @!method ActiveRecord::Migration#rename_domain(name, to:)
# Change the name and/or schema of a domain type
#
# @param [#to_s] :name (nil) The qualified name of the type
# @option [#to_s] :to (nil) The new qualified name for the type
#
# A domain type can be both renamed and moved to another schema.
#
#   rename_domain "us_code", to: "dict.us_postal_code"
#
# The operation is always reversible.

module PGTrunk::Operations::Domains
  # @private
  class RenameDomain < Base
    validates :new_name, presence: true
    validates :force, :if_exists, absence: true

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

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

    private

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

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

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

      moved = name.merge(schema: new_name.schema)
      "ALTER DOMAIN #{moved.to_sql} RENAME TO #{new_name.name.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/domains/rename_domain.rb