Module: ActiveRecord::Postgres::Constraints::PostgreSQLAdapter

Defined in:
lib/active_record/postgres/constraints/postgresql_adapter.rb

Instance Method Summary collapse

Instance Method Details

#add_constraint(type, table, name_or_conditions, conditions) ⇒ Object



17
18
19
20
21
22
23
# 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) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# 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) ⇒ Object



25
26
27
# 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