Sha256: a39d93b9e8d3635b7cf8ea393620eee4d1fa37e4b3c45bbc9dc7746a57fd87bf

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 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 do _add_null_constraint(*args) end
          dir.down { _remove_null_constraint(*args) }
        end
      end

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

      private

      def _add_null_constraint(table, attribute, options = {})
        name = Util.constraint_name(table, attribute, 'null', options)
        table = Util.wrap_identifier(table)
        attribute = Util.wrap_identifier(attribute)
        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)
        table = Util.wrap_identifier(table)
        execute("ALTER TABLE #{table} DROP CONSTRAINT #{name}")
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rein-3.5.0 lib/rein/constraint/null.rb
rein-3.4.0 lib/rein/constraint/null.rb