instance method
exclusion_constraints
Ruby on Rails 8.0.4
Since v7.1.6Signature
exclusion_constraints(table_name)
Returns an array of exclusion constraints for the given table. The exclusion constraints are represented as ExclusionConstraintDefinition objects.
Parameters
-
table_namereq
Source
# File activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb, line 663
def exclusion_constraints(table_name)
scope = quoted_scope(table_name)
exclusion_info = internal_exec_query(<<-SQL, "SCHEMA")
SELECT conname, pg_get_constraintdef(c.oid) AS constraintdef, c.condeferrable, c.condeferred
FROM pg_constraint c
JOIN pg_class t ON c.conrelid = t.oid
JOIN pg_namespace n ON n.oid = c.connamespace
WHERE c.contype = 'x'
AND t.relname = #{scope[:name]}
AND n.nspname = #{scope[:schema]}
SQL
exclusion_info.map do |row|
method_and_elements, predicate = row["constraintdef"].split(" WHERE ")
method_and_elements_parts = method_and_elements.match(/EXCLUDE(?: USING (?<using>\S+))? \((?<expression>.+)\)/)
predicate.remove!(/ DEFERRABLE(?: INITIALLY (?:IMMEDIATE|DEFERRED))?/) if predicate
predicate = predicate.from(2).to(-3) if predicate # strip 2 opening and closing parentheses
deferrable = extract_constraint_deferrable(row["condeferrable"], row["condeferred"])
options = {
name: row["conname"],
using: method_and_elements_parts["using"].to_sym,
where: predicate,
deferrable: deferrable
}
ExclusionConstraintDefinition.new(table_name, method_and_elements_parts["expression"], options)
end
end
Defined in activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb line 663
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaStatements