Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Translate adapter errors that indicate a violated uniqueness constrai…

…nt to ActiveRecord::RecordNotUnique exception derived from ActiveReecord::StatementInvalid.

Signed-off-by: Michael Koziarski <michael@koziarski.com>
  • Loading branch information...
commit 53a3eaa8603cf6e7a3c007f327fe5d3bb68de1ee 1 parent 18a97a6
@mschuerig mschuerig authored NZKoz committed
View
4 activerecord/lib/active_record/base.rb
@@ -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.
#
View
7 activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
@@ -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
View
11 activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -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]
View
9 activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -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:
View
10 activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
@@ -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:
View
7 activerecord/test/cases/adapter_test.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.