Sha256: 0d0c57d53527ddbb536a72220e5323bd3e84e2f3291c4e047f661b72b960f907

Contents?: true

Size: 900 Bytes

Versions: 1

Compression:

Stored size: 900 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 = {})
        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

1 entries across 1 versions & 1 rubygems

Version Path
rein-3.3.0 lib/rein/constraint/primary_key.rb