class Mv::Postgresql::Constraint::Builder::Check

Public Instance Methods

create() click to toggle source
# File lib/mv/postgresql/constraint/builder/check.rb, line 6
def create
  validation_builders.group_by(&:table_name).each do |table_name, validations|
    db.execute(drop_check_statement(table_name))
    db.execute(create_check_statement(table_name))
  end
end
delete() click to toggle source
# File lib/mv/postgresql/constraint/builder/check.rb, line 18
def delete
  validation_builders.group_by(&:table_name).each do |table_name, validations|
    if db.data_source_exists?(table_name)
      db.execute(drop_check_statement(table_name))
    end
  end
end
update(new_constraint_builder) click to toggle source
# File lib/mv/postgresql/constraint/builder/check.rb, line 13
def update new_constraint_builder
  delete
  new_constraint_builder.create
end

Private Instance Methods

check_body(table_name) click to toggle source
# File lib/mv/postgresql/constraint/builder/check.rb, line 28
def check_body(table_name)
  validation_builders.select{|b| b.table_name == table_name }.collect(&:conditions).flatten.collect do |condition|
    "(#{condition[:statement]})"
  end.join(" AND ")
end
create_check_statement(table_name) click to toggle source
# File lib/mv/postgresql/constraint/builder/check.rb, line 34
def create_check_statement(table_name)
  "ALTER TABLE #{table_name} ADD CONSTRAINT #{name} CHECK (#{check_body(table_name)});"
end
drop_check_statement(table_name) click to toggle source
# File lib/mv/postgresql/constraint/builder/check.rb, line 38
def drop_check_statement(table_name)
  "ALTER TABLE #{table_name} DROP CONSTRAINT IF EXISTS #{name};"
end