Module: Postgresql::Check::SchemaStatements

Extended by:
ActiveSupport::Concern
Defined in:
lib/postgresql/check/schema_statements.rb

Instance Method Summary collapse

Instance Method Details

#add_check(table_name, condition, options) ⇒ Object

Note:

:name option is mandatory.

Add a new Check constraint to table with given table_name using given conditions. Constraint name should be specified by :name options in options argument.

Examples:

add_check :products, 'price > 0', name: 'products_price_check'

# Generates:
# ALTER TABLE products ADD CONSTRAINT products_price_check CHECK (price > 0)

Parameters:

  • Table name which constraint created on

  • Raw SQL string specifying constraint condition

  • Hash with single mandatory key :name

Options Hash (options):

  • :name (String|Symbol)

    Constraint name



26
27
28
29
30
31
32
33
34
# File 'lib/postgresql/check/schema_statements.rb', line 26

def add_check(table_name, condition, options)
  name = options.fetch(:name) { raise 'add_check, :name option required' }

  execute "    ALTER TABLE \#{quote_table_name(table_name)}\n    ADD CONSTRAINT \#{quote_column_name(name)}\n    CHECK (\#{condition})\n  SQL\nend\n"

#checks(table_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/postgresql/check/schema_statements.rb', line 72

def checks(table_name)
  checks_info = select_all "    SELECT c.conname, c.consrc\n    FROM pg_constraint c\n    INNER JOIN pg_class t\n      ON c.conrelid = t.oid\n    WHERE c.contype = 'c'\n      AND t.relname = '\#{table_name}'\n  SQL\n\n  checks_info.map do |row|\n    Constraint.new table_name, row['conname'], row['consrc']\n  end\nend\n"

#create_table_with_checks(table_name, *args) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/postgresql/check/schema_statements.rb', line 58

def create_table_with_checks(table_name, *args)
  definition = nil

  create_table_without_checks(table_name, *args) do |td|
    definition = td # trick to get the definition
    yield td if block_given?
  end

  definition.checks.each do |condition, options|
    add_check table_name, condition, options
  end
end

#remove_check(table_name, options) ⇒ Object

Remove constraint with given name from table. Constraint name is specified with options hash.

Examples:

remove_check :products, :name => 'products_price_chk'

# Generates:
# ALTER TABLE products DROP CONSTRAINT products_price_chk

Parameters:

  • Table name which constraint defined on

  • Hash with single mandatory key :name

Options Hash (options):

  • :name (String|Symbol)

    Constraint name



48
49
50
51
52
53
54
55
# File 'lib/postgresql/check/schema_statements.rb', line 48

def remove_check(table_name, options)
  name = options.fetch(:name) { raise 'remove_check, :name option required' }

  execute "    ALTER TABLE \#{quote_table_name(table_name)}\n    DROP CONSTRAINT \#{quote_column_name(name)}\n  SQL\nend\n"