diff options
author | José Valim <jose.valim@gmail.com> | 2009-06-26 11:58:02 +0200 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2009-06-26 11:58:02 +0200 |
commit | 915f108d6a47b9c29e11ebe0b140ef017eb015b0 (patch) | |
tree | ae0c42330a9fbdc8395f8a5bae4605ef455c18c1 /activerecord/lib | |
parent | aee63a12f2cfd110725c74686ae1eed58e18fda7 (diff) | |
parent | b5dfdc714fab7d2836e0a979ca88b4a17db9ec06 (diff) | |
download | rails-915f108d6a47b9c29e11ebe0b140ef017eb015b0.tar.gz rails-915f108d6a47b9c29e11ebe0b140ef017eb015b0.tar.bz2 rails-915f108d6a47b9c29e11ebe0b140ef017eb015b0.zip |
Merge branch 'master' of git://github.com/rails/rails
Diffstat (limited to 'activerecord/lib')
5 files changed, 59 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 39fac0b7e7..f1b7c323dc 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 d5536e4d67..83cb9cff15 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -563,6 +563,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 18b499904e..e77ae93c21 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -941,6 +941,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 d96c6d6b51..5e5e30776a 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb @@ -431,6 +431,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: |