diff options
author | Emilio Tagua <miloops@gmail.com> | 2009-06-30 19:34:26 -0300 |
---|---|---|
committer | Emilio Tagua <miloops@gmail.com> | 2009-06-30 19:34:26 -0300 |
commit | 9c70442534c7754eac3738e0ddf11446b01c4f9e (patch) | |
tree | 7af8614ad91e7400735a6be1b36dca704d87e3b6 /activerecord | |
parent | 50f73bd39c729b4ba4b5215b608e5767ab0aa7a1 (diff) | |
parent | b5dfdc714fab7d2836e0a979ca88b4a17db9ec06 (diff) | |
download | rails-9c70442534c7754eac3738e0ddf11446b01c4f9e.tar.gz rails-9c70442534c7754eac3738e0ddf11446b01c4f9e.tar.bz2 rails-9c70442534c7754eac3738e0ddf11446b01c4f9e.zip |
Merge commit 'rails/master'
Conflicts:
activerecord/test/cases/adapter_test.rb
Diffstat (limited to 'activerecord')
6 files changed, 74 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index c7fc1fa124..b3ecd01348 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -67,6 +67,25 @@ module ActiveRecord #:nodoc: class StatementInvalid < ActiveRecordError end + # Parent class for all specific exceptions which wrap database driver exceptions + # provides access to the original exception also. + class WrappedDatabaseException < StatementInvalid + attr_reader :original_exception + + def initialize(message, original_exception) + super(message) + @original_exception, = original_exception + end + end + + # Raised when a record cannot be inserted because it would violate a uniqueness constraint. + class RecordNotUnique < WrappedDatabaseException + end + + # Raised when a record cannot be inserted or updated because it references a non-existent record. + class InvalidForeignKey < WrappedDatabaseException + 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. # diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index 91b111ab55..c533d4cdb6 100755 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -211,9 +211,14 @@ module ActiveRecord @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 diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index cee93dcac2..9173c8bca3 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -552,6 +552,19 @@ module ActiveRecord where_sql end + protected + + def translate_exception(exception, message) + case exception.errno + when 1062 + RecordNotUnique.new(message, exception) + when 1452 + InvalidForeignKey.new(message, exception) + else + super + end + end + private def connect encoding = @config[:encoding] diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 6b230787c3..fc8ca357e5 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -928,6 +928,17 @@ module ActiveRecord end end + def translate_exception(exception, message) + case exception.message + when /duplicate key value violates unique constraint/ + RecordNotUnique.new(message, exception) + when /violates foreign key constraint/ + InvalidForeignKey.new(message, exception) + else + super + end + end + private # The internal PostgreSQL identifier of the money data type. MONEY_COLUMN_TYPE_OID = 790 #:nodoc: diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb index b33c460a93..8cbe08cd77 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb @@ -432,6 +432,16 @@ module ActiveRecord '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, exception) + else + super + end + end + end class SQLite2Adapter < SQLiteAdapter # :nodoc: diff --git a/activerecord/test/cases/adapter_test.rb b/activerecord/test/cases/adapter_test.rb index 544daf8f60..12808194f9 100644 --- a/activerecord/test/cases/adapter_test.rb +++ b/activerecord/test/cases/adapter_test.rb @@ -109,4 +109,19 @@ class AdapterTest < ActiveRecord::TestCase end 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 + + def test_foreign_key_violations_are_translated_to_specific_exception + unless @connection.adapter_name == 'SQLite' + assert_raises(ActiveRecord::InvalidForeignKey) do + @connection.execute "INSERT INTO fk_test_has_fk (fk_id) VALUES (0)" + end + end + end end |