Sha256: e0a1d57864d5db3985808e7946cb74baf7295f0dc2c7b167132b043ffc05fb85

Contents?: true

Size: 1.35 KB

Versions: 5

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: false

module PGTrunk::Operations::Enums
  # @private
  # Definition for the value's change
  class Change
    include ActiveModel::Model
    include ActiveModel::Attributes
    include ActiveModel::Validations

    def self.build(data)
      data.is_a?(self) ? data : new(**data)
    end

    attribute :name, :string
    attribute :new_name, :string
    attribute :after, :string
    attribute :before, :string

    validates :name, presence: true
    validates :new_name, "PGTrunk/difference": { from: :name }, allow_nil: true
    validate { errors.add :after,  :present if rename? && after.present? }
    validate { errors.add :before, :present if rename? && before.present? }
    validate { errors.add :before, :present if [after, before].all?(&:present?) }

    def rename?
      new_name.present?
    end

    def add?
      new_name.blank?
    end

    def to_h
      attributes.compact.symbolize_keys
    end

    def opts
      to_h.slice(:before, :after).compact
    end

    def invert
      { name: new_name, new_name: name }
    end

    def to_sql
      return "RENAME VALUE '#{name}' TO '#{new_name}'" if new_name.present?

      sql = "ADD VALUE IF NOT EXISTS $value$#{name}$value$"
      sql << " BEFORE $value$#{before}$value$" if before.present?
      sql << " AFTER $value$#{after}$value$" if after.present?
      sql
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pg_trunk-0.2.0 lib/pg_trunk/operations/enums/change.rb
pg_trunk-0.1.3 lib/pg_trunk/operations/enums/change.rb
pg_trunk-0.1.2 lib/pg_trunk/operations/enums/change.rb
pg_trunk-0.1.1 lib/pg_trunk/operations/enums/change.rb
pg_trunk-0.1.0 lib/pg_trunk/operations/enums/change.rb