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

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

Instance Method Summary collapse

Instance Method Details

#add_check_constraint(table, name_or_conditions, conditions = nil) ⇒ Object



7
8
9
10
# File 'lib/active_record/postgres/constraints/postgresql_adapter.rb', line 7

def add_check_constraint(table, name_or_conditions, conditions = nil)
  constraint = Constraints.to_sql(table, name_or_conditions, conditions)
  execute("ALTER TABLE #{table} ADD #{constraint}")
end

#constraints(table) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/active_record/postgres/constraints/postgresql_adapter.rb', line 16

def constraints(table)
  sql = "SELECT conname, consrc FROM pg_constraint
    JOIN pg_class ON pg_constraint.conrelid = pg_class.oid
    WHERE
      pg_constraint.contype = 'c'
      AND
      pg_class.relname = '#{table}'".tr("\n", ' ').squeeze(' ')
  execute sql
end

#remove_check_constraint(table, name, _conditions = nil) ⇒ Object



12
13
14
# File 'lib/active_record/postgres/constraints/postgresql_adapter.rb', line 12

def remove_check_constraint(table, name, _conditions = nil)
  execute("ALTER TABLE #{table} DROP CONSTRAINT #{name}")
end