Sha256: ba008e922689d7fefe41c02819ff016182bee12d4d4196e94cd86c4df8f82d82
Contents?: true
Size: 1.51 KB
Versions: 2
Compression:
Stored size: 1.51 KB
Contents
require 'rein/util' module Rein module Constraint # This module contains methods for defining unique constraints. module Unique include ActiveRecord::ConnectionAdapters::Quoting def add_unique_constraint(*args) reversible do |dir| dir.up do _add_unique_constraint(*args) end dir.down { _remove_unique_constraint(*args) } end end def remove_unique_constraint(*args) reversible do |dir| dir.up do _remove_unique_constraint(*args) end dir.down { _add_unique_constraint(*args) } end end private def _add_unique_constraint(table, attributes, options = {}) attributes = [attributes].flatten name = Util.constraint_name(table, attributes.join('_'), 'unique', options) table = Util.wrap_identifier(table) attributes = attributes.map { |attribute| Util.wrap_identifier(attribute) } initially = options[:deferred] ? 'DEFERRED' : 'IMMEDIATE' sql = "ALTER TABLE #{table} ADD CONSTRAINT #{name} UNIQUE (#{attributes.join(', ')})" sql << " DEFERRABLE INITIALLY #{initially}" unless options[:deferrable] == false execute(sql) end def _remove_unique_constraint(table, attributes, options = {}) attributes = [attributes].flatten name = Util.constraint_name(table, attributes.join('_'), 'unique', 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/unique.rb |
rein-3.4.0 | lib/rein/constraint/unique.rb |