Sha256: 4894d339aaa430e1ac9b5c82fb6d5a5190d40a36b37ce770e55d0099fc1a0150

Contents?: true

Size: 988 Bytes

Versions: 2

Compression:

Stored size: 988 Bytes

Contents

module Rein
  module Constraint
    # This module contains methods for defining primary key constraints.
    module PrimaryKey
      def add_primary_key(*args)
        reversible do |dir|
          dir.up do _add_primary_key(*args) end
          dir.down { _remove_primary_key(*args) }
        end
      end

      def remove_primary_key(*args)
        reversible do |dir|
          dir.up do _remove_primary_key(*args) end
          dir.down { _add_primary_key(*args) }
        end
      end

      private

      def _add_primary_key(table, options = {})
        table = Util.wrap_identifier(table)
        attribute = (options[:column] || 'id').to_sym
        execute("ALTER TABLE #{table} ADD PRIMARY KEY (#{attribute})")
      end

      def _remove_primary_key(table, options = {})
        table = Util.wrap_identifier(table)
        attribute = (options[:column] || 'id').to_sym
        execute("ALTER TABLE #{table} DROP CONSTRAINT #{attribute}_pkey")
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

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