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



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

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



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

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



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

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