Module: ActiveRecord::ConnectionAdapters::ConstraintHandlers::Postgresql

Defined in:
lib/activerecord_constraint_handlers.rb

Overview

A PostgreSQL specific implementation.

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

NOT_NULL_REGEXP =
Regexp.new("PGError: +ERROR: +null value in column \"([^\"]*)\" violates not-null constraint")
UNIQUE_REGEXP =
Regexp.new("PGError: +ERROR: +duplicate key .*violates unique constraint \"([^\"]+)\"")
FOREIGN_REGEXP =
Regexp.new("PGError: +ERROR: +insert or update on table \"([^\"]+)\" violates " +
"foreign key constraint \"([^\"]+)\"")

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

When the include of ActiveRecord::ConnectionAdapters::ConstraintConnectionHook happens this hook is called with ActiveRecord::Base as the base. We extend the base with the class methods so they are class methods and the instance variables are then class instance variables.



208
209
210
# File 'lib/activerecord_constraint_handlers.rb', line 208

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#handle_create_or_update_exception(e) ⇒ Object

Called with exception when create_or_update throws an exception



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/activerecord_constraint_handlers.rb', line 213

def handle_create_or_update_exception(e)
  raise e until e.is_a? ActiveRecord::StatementInvalid
  logger.debug("trace_report create_or_update error is '#{e.message}'")
  if md = NOT_NULL_REGEXP.match(e.message)
    errors.add(md[1].to_sym, "can't be blank")
  elsif md = UNIQUE_REGEXP.match(e.message)
    constraint = md[1]
    ffoo(constraint, "has already been taken")
  elsif md = FOREIGN_REGEXP.match(e.message)
    table = md[1]
    constraint = md[2]
    ffoo(constraint, "is invalid")
  else
    logger.debug("Nothing matched")
  end
  false
end