Skip to content

Commit

Permalink
Translate adapter errors that indicate a violated uniqueness constrai…
Browse files Browse the repository at this point in the history
…nt to ActiveRecord::RecordNotUnique exception derived from ActiveReecord::StatementInvalid.

Signed-off-by: Michael Koziarski <michael@koziarski.com>
  • Loading branch information
mschuerig authored and NZKoz committed Jun 26, 2009
1 parent 18a97a6 commit 53a3eaa
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 1 deletion.
4 changes: 4 additions & 0 deletions activerecord/lib/active_record/base.rb
Expand Up @@ -67,6 +67,10 @@ class RecordNotSaved < ActiveRecordError
class StatementInvalid < ActiveRecordError
end

# Raised when a record cannot be inserted because it would violate a uniqueness constraint.
class RecordNotUnique < StatementInvalid
end

# Raised when number of bind variables in statement given to <tt>:condition</tt> key (for example, when using +find+ method)
# does not match number of expected variables.
#
Expand Down
Expand Up @@ -211,9 +211,14 @@ def log(sql, name)
@last_verification = 0
message = "#{e.class.name}: #{e.message}: #{sql}"
log_info(message, name, 0)
raise ActiveRecord::StatementInvalid, message
raise translate_exception(e, message)
end

def translate_exception(e, message)
# override in derived class
ActiveRecord::StatementInvalid.new(message)
end

def format_log_entry(message, dump = nil)
if ActiveRecord::Base.colorize_logging
if @@row_even
Expand Down
Expand Up @@ -563,6 +563,17 @@ def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key)
where_sql
end

protected

def translate_exception(exception, message)
case exception.errno
when 1062
RecordNotUnique.new(message)
else
super
end
end

private
def connect
encoding = @config[:encoding]
Expand Down
Expand Up @@ -941,6 +941,15 @@ def postgresql_version
end
end

def translate_exception(exception, message)
case exception.message
when /duplicate key value violates unique constraint/
RecordNotUnique.new(message)
else
super
end
end

private
# The internal PostgreSQL identifier of the money data type.
MONEY_COLUMN_TYPE_OID = 790 #:nodoc:
Expand Down
Expand Up @@ -431,6 +431,16 @@ def default_primary_key_type
'INTEGER PRIMARY KEY NOT NULL'.freeze
end
end

def translate_exception(exception, message)
case exception.message
when /column(s)? .* (is|are) not unique/
RecordNotUnique.new(message)
else
super
end
end

end

class SQLite2Adapter < SQLiteAdapter # :nodoc:
Expand Down
7 changes: 7 additions & 0 deletions activerecord/test/cases/adapter_test.rb
Expand Up @@ -130,4 +130,11 @@ def test_add_limit_offset_should_sanitize_sql_injection_for_limit_with_comas
assert_equal " LIMIT 1,7 OFFSET 7", @connection.add_limit_offset!("", :limit=>sql_inject, :offset=>7)
end
end

def test_uniqueness_violations_are_translated_to_specific_exception
@connection.execute "INSERT INTO subscribers(nick) VALUES('me')"
assert_raises(ActiveRecord::RecordNotUnique) do
@connection.execute "INSERT INTO subscribers(nick) VALUES('me')"
end
end
end

0 comments on commit 53a3eaa

Please sign in to comment.