Module: PGSpecHelper::Validations

Included in:
PGSpecHelper
Defined in:
lib/pg_spec_helper/validations.rb

Instance Method Summary collapse

Instance Method Details

#create_validation(schema_name, table_name, validation_name, check_clause) ⇒ Object

create a validation on the provided table and columns



6
7
8
9
10
11
12
13
# File 'lib/pg_spec_helper/validations.rb', line 6

def create_validation schema_name, table_name, validation_name, check_clause
  # todo the check_clause is vulnerable to sql injection (although this is very low risk because
  #      it is only ever provided by the test suite, and is never provided by the user)
  connection.exec(<<~SQL)
    ALTER TABLE #{connection.quote_ident schema_name.to_s}.#{connection.quote_ident table_name.to_s}
      ADD CONSTRAINT #{connection.quote_ident validation_name.to_s} CHECK (#{check_clause})
  SQL
end

#get_validation_names(schema_name, table_name) ⇒ Object

return a list of validation names for the provided table



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pg_spec_helper/validations.rb', line 16

def get_validation_names schema_name, table_name
  # get the validation names
  rows = connection.exec(<<~SQL, [schema_name.to_s, table_name.to_s])
    SELECT
      constraint_name
    FROM
      information_schema.table_constraints
    WHERE
      table_schema = $1
      AND table_name = $2
      AND constraint_type = 'CHECK'
  SQL
  rows.map { |r| r["constraint_name"].to_sym }
end