Sha256: 9c81615735fd09ca338855ad8e78e0b1d4868a5252994d7d062b6c416dc5697c

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 KB

Contents

require 'active_support/inflector'
require 'rein/util'

module Rein
  module Constraint
    # This module contains methods for defining check constraints.
    module Check
      def add_check_constraint(*args)
        reversible do |dir|
          dir.up { _add_check_constraint(*args) }
          dir.down { _remove_check_constraint(*args) }
        end
      end

      def remove_check_constraint(*args)
        reversible do |dir|
          dir.up { _remove_check_constraint(*args) }
          dir.down { _add_check_constraint(*args) }
        end
      end

      private

      def _add_check_constraint(table_name, predicate, options = {})
        raise 'Generic CHECK constraints must have a name' unless options[:name].present?
        name = Util.wrap_identifier(options[:name])
        sql = "ALTER TABLE #{Util.wrap_identifier(table_name)}"
        sql << " ADD CONSTRAINT #{name}"
        sql << " CHECK (#{predicate})"
        execute(sql)
      end

      def _remove_check_constraint(table_name, _predicate, options = {})
        raise 'Generic CHECK constraints must have a name' unless options[:name].present?
        name = Util.wrap_identifier(options[:name])
        execute("ALTER TABLE #{Util.wrap_identifier(table_name)} DROP CONSTRAINT #{name}")
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rein-5.0.0 lib/rein/constraint/check.rb
rein-4.0.0 lib/rein/constraint/check.rb