module ActiveRecord::Postgres::Constraints::PostgreSQLAdapter

Public Instance Methods

add_constraint(type, table, name_or_conditions, conditions) click to toggle source
# File lib/active_record/postgres/constraints/postgresql_adapter.rb, line 17
def add_constraint(type, table, name_or_conditions, conditions)
  constraint =
    ActiveRecord::Postgres::Constraints.
      class_for_constraint_type(type).
      to_sql(table, name_or_conditions, conditions)
  execute("ALTER TABLE #{table} ADD #{constraint}")
end
constraints(table) click to toggle source
# File lib/active_record/postgres/constraints/postgresql_adapter.rb, line 29
def constraints(table)
  types = CONSTRAINT_TYPES.values.map { |v| "'#{v}'" }.join(', ')
  sql = "SELECT conname, contype,
    pg_get_constraintdef(pg_constraint.oid) AS definition
    FROM pg_constraint
    JOIN pg_class ON pg_constraint.conrelid = pg_class.oid
    WHERE
      pg_constraint.contype IN (#{types})
      AND
      pg_class.relname = '#{table}'".tr("\n", ' ').squeeze(' ')
  execute sql
end
remove_constraint(_type, table, name, _conditions) click to toggle source
# File lib/active_record/postgres/constraints/postgresql_adapter.rb, line 25
def remove_constraint(_type, table, name, _conditions)
  execute("ALTER TABLE #{table} DROP CONSTRAINT #{name}")
end