Class: ActiveRecord::ConnectionAdapters::PostgreSQLExcludeConstraint

Inherits:
PostgreSQLConstraint show all
Defined in:
lib/active_record/postgresql_extensions/constraints.rb

Overview

Creates EXCLUDE constraints for PostgreSQL tables and columns.

This class is meant to be used by PostgreSQL column and table definition and manipulation methods. There are two ways to create a EXCLUDE constraint:

  • on a table definition

  • when altering a table

In both cases, a Hash or an Array of Hashes should be used to set the EXCLUDE constraint checks. The Hash(es) should be in the format { :element => ..., :with => ... }, where :element is a column name or expression and :with is the operator to compare against. The key :operator is an alias for :where.

Table Definition

EXCLUDE constraints can also be applied to the table directly rather than on a column definition.

Example

The following example produces the same result as above:

create_table('foo') do |t|
  t.integer :blort
  t.exclude({
    :element => 'length(blort)',
    :with => '='
  }, {
    :name => 'exclude_blort_length'
  })
end

# Produces:
#
# CREATE TABLE "foo" (
#   "id" serial primary key,
#   "blort" text,
#   CONSTRAINT "exclude_blort_length" EXCLUDE (length(blort) WITH =)
# );

Table Manipulation

You can also create new EXCLUDE constraints outside of a table definition using PostgreSQLAdapter#add_exclude_constraint.

Examples

add_exclude_constraint(:foo, { :element => :bar_id, :with => '=' })
# => ALTER TABLE "foo" ADD EXCLUDE ("bar_id" WITH =);

Options for EXCLUDE Constraints

  • :name - specifies a name for the constraint.

  • :using - sets the index type to be used. Usually this will :gist, but the default is left blank to allow for the PostgreSQL default which is :btree. See the PostgreSQL docs for details.

  • :storage_parameters - PostgreSQL allows you to add a couple of additional parameters to indexes to govern disk usage and such. This option is a simple String or a Hash that lets you insert these options as necessary. See the PostgreSQL documentation on index storage parameters for details. :index_parameters can also be used.

  • :tablespace - allows you to specify a tablespace for the index being created. See the PostgreSQL documentation on tablespaces for details.

  • :conditions - sets the WHERE conditions for the EXCLUDE constraint. You can also use the :where option.

Dropping EXCLUDE Constraints

Like all PostgreSQL constraints, you can use PostgreSQLAdapter#drop_constraint to remove a constraint from a table.

Instance Attribute Summary collapse

Attributes inherited from PostgreSQLConstraint

#base, #options

Instance Method Summary collapse

Methods included from PostgreSQLExtensions::Utils

#hash_or_array_of_hashes, #options_from_hash_or_string, #strip_heredoc

Constructor Details

#initialize(base, table, excludes, options = {}) ⇒ PostgreSQLExcludeConstraint

:nodoc:



703
704
705
706
707
# File 'lib/active_record/postgresql_extensions/constraints.rb', line 703

def initialize(base, table, excludes, options = {}) #:nodoc:
  @excludes = ActiveRecord::PostgreSQLExtensions::Utils.hash_or_array_of_hashes(excludes)

  super(base, options)
end

Instance Attribute Details

#excludesObject

Returns the value of attribute excludes.



701
702
703
# File 'lib/active_record/postgresql_extensions/constraints.rb', line 701

def excludes
  @excludes
end

Instance Method Details

#to_sqlObject Also known as: to_s

:nodoc:



709
710
711
712
713
714
715
716
717
718
719
720
721
# File 'lib/active_record/postgresql_extensions/constraints.rb', line 709

def to_sql #:nodoc:
  sql = String.new
  sql << "#{constraint_name}EXCLUDE "
  sql << "USING #{base.quote_column_name(options[:using])} " if options[:using]
  sql << "(" << excludes.collect { |e|
    "#{e[:element]} WITH #{e[:with] || e[:operator]}"
  }.join(', ')
  sql << ")"
  sql << storage_parameters
  sql << using_tablespace
  sql << " WHERE (#{options[:conditions] || options[:where]})" if options[:conditions] || options[:where]
  sql
end