Sha256: 2d65a1d30ce746f17d242b3a0dddd6d0de38d70ed7914ac8c454374f27a5990e

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

require "rein/util"

module Rein
  module Constraint
    # This module contains methods for defining null constraints.
    module Null
      include ActiveRecord::ConnectionAdapters::Quoting

      def add_null_constraint(*args)
        reversible do |dir|
          dir.up { _add_null_constraint(*args) }
          dir.down { _remove_null_constraint(*args) }
        end
      end

      def remove_null_constraint(*args)
        reversible do |dir|
          dir.up { _remove_null_constraint(*args) }
          dir.down { _add_null_constraint(*args) }
        end
      end

      private

      def _add_null_constraint(table, attribute, options = {})
        name = Util.constraint_name(table, attribute, "null", options)
        conditions = Util.conditions_with_if("#{attribute} IS NOT NULL", options)
        execute("ALTER TABLE #{table} ADD CONSTRAINT #{name} CHECK (#{conditions})")
      end

      def _remove_null_constraint(table, attribute, options = {})
        name = Util.constraint_name(table, attribute, "null", options)
        execute("ALTER TABLE #{table} DROP CONSTRAINT #{name}")
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rein-3.2.0 lib/rein/constraint/null.rb
rein-3.1.0 lib/rein/constraint/null.rb
rein-3.0.0 lib/rein/constraint/null.rb