Sha256: 6b0cbe271325e2c6c0407b88ac04c6185cc4098471b084486d476d166f569ad0

Contents?: true

Size: 894 Bytes

Versions: 3

Compression:

Stored size: 894 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 { _add_primary_key(*args) }
          dir.down { _remove_primary_key(*args) }
        end
      end

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

      private

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

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

Version data entries

3 entries across 3 versions & 1 rubygems

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