diff options
Diffstat (limited to 'activerecord/lib')
8 files changed, 47 insertions, 39 deletions
diff --git a/activerecord/lib/active_record/callbacks.rb b/activerecord/lib/active_record/callbacks.rb index 98c14e6eb0..7ebeb6079e 100644 --- a/activerecord/lib/active_record/callbacks.rb +++ b/activerecord/lib/active_record/callbacks.rb @@ -17,8 +17,13 @@ module ActiveRecord # * (-) <tt>create</tt> # * (5) <tt>after_create</tt> # * (6) <tt>after_save</tt> + # * (7) <tt>after_commit</tt> # - # That's a total of eight callbacks, which gives you immense power to react and prepare for each state in the + # Also, an <tt>after_rollback</tt> callback can be configured to be triggered whenever a rollback is issued. + # Check out <tt>ActiveRecord::Transactions</tt> for more details about <tt>after_commit</tt> and + # <tt>after_rollback</tt>. + # + # That's a total of ten callbacks, which gives you immense power to react and prepare for each state in the # Active Record lifecycle. The sequence for calling <tt>Base#save</tt> for an existing record is similar, except that each # <tt>_on_create</tt> callback is replaced by the corresponding <tt>_on_update</tt> callback. # diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb index 2f36bec764..2493095a04 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_specification.rb @@ -10,8 +10,8 @@ module ActiveRecord ## # :singleton-method: # The connection handler - cattr_accessor :connection_handler, :instance_writer => false - @@connection_handler = ConnectionAdapters::ConnectionHandler.new + class_inheritable_accessor :connection_handler, :instance_writer => false + self.connection_handler = ConnectionAdapters::ConnectionHandler.new # Returns the connection currently associated with the class. This can # also be used to "borrow" the connection to do database work that isn't @@ -54,7 +54,7 @@ module ActiveRecord raise AdapterNotSpecified unless defined?(Rails.env) establish_connection(Rails.env) when ConnectionSpecification - @@connection_handler.establish_connection(name, spec) + self.connection_handler.establish_connection(name, spec) when Symbol, String if configuration = configurations[spec.to_s] establish_connection(configuration) diff --git a/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb b/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb index 3c560903f7..533a7bb8e6 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb @@ -76,7 +76,7 @@ module ActiveRecord def cache_sql(sql) result = if @query_cache.has_key?(sql) - ActiveSupport::Notifications.instrument("active_record.sql", + ActiveSupport::Notifications.instrument("sql.active_record", :sql => sql, :name => "CACHE", :connection_id => self.object_id) @query_cache[sql] else diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index 578297029b..28a59c1e62 100755 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -197,7 +197,7 @@ module ActiveRecord def log(sql, name) name ||= "SQL" result = nil - ActiveSupport::Notifications.instrument("active_record.sql", + ActiveSupport::Notifications.instrument("sql.active_record", :sql => sql, :name => name, :connection_id => self.object_id) do @runtime += Benchmark.ms { result = yield } end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 74fed4ad62..6389094b8a 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -490,12 +490,8 @@ module ActiveRecord execute "ROLLBACK" end - if defined?(PGconn::PQTRANS_IDLE) - # The ruby-pg driver supports inspecting the transaction status, - # while the ruby-postgres driver does not. - def outside_transaction? - @connection.transaction_status == PGconn::PQTRANS_IDLE - end + def outside_transaction? + @connection.transaction_status == PGconn::PQTRANS_IDLE end def create_savepoint diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb index 0bc49c1daa..4bf33c3856 100644 --- a/activerecord/lib/active_record/fixtures.rb +++ b/activerecord/lib/active_record/fixtures.rb @@ -516,7 +516,7 @@ class Fixtures < (RUBY_VERSION < '1.9' ? YAML::Omap : Hash) # Cap primary key sequences to max(pk). if connection.respond_to?(:reset_pk_sequence!) table_names.each do |table_name| - connection.reset_pk_sequence!(table_name) + connection.reset_pk_sequence!(table_name.tr('/', '_')) end end end diff --git a/activerecord/lib/active_record/locale/en.yml b/activerecord/lib/active_record/locale/en.yml index 810359fef3..9d5cb54180 100644 --- a/activerecord/lib/active_record/locale/en.yml +++ b/activerecord/lib/active_record/locale/en.yml @@ -9,7 +9,7 @@ en: errors: messages: taken: "has already been taken" - record_invalid: "Validation failed: {{errors}}" + record_invalid: "Validation failed: %{errors}" # Append your own errors here or at the model/attributes scope. # You can define own errors for models or model attributes. @@ -18,7 +18,7 @@ en: # For example, # models: # user: - # blank: "This is a custom blank message for {{model}}: {{attribute}}" + # blank: "This is a custom blank message for %{model}: %{attribute}" # attributes: # login: # blank: "This is a custom blank message for User login" diff --git a/activerecord/lib/active_record/transactions.rb b/activerecord/lib/active_record/transactions.rb index 796dd99f02..1a195fbb81 100644 --- a/activerecord/lib/active_record/transactions.rb +++ b/activerecord/lib/active_record/transactions.rb @@ -31,10 +31,10 @@ module ActiveRecord # mary.deposit(100) # end # - # This example will only take money from David and give to Mary if neither - # +withdrawal+ nor +deposit+ raises an exception. Exceptions will force a - # ROLLBACK that returns the database to the state before the transaction was - # begun. Be aware, though, that the objects will _not_ have their instance + # This example will only take money from David and give it to Mary if neither + # +withdrawal+ nor +deposit+ raise an exception. Exceptions will force a + # ROLLBACK that returns the database to the state before the transaction + # began. Be aware, though, that the objects will _not_ have their instance # data returned to their pre-transactional state. # # == Different Active Record classes in a single transaction @@ -44,16 +44,16 @@ module ActiveRecord # that class. This is because transactions are per-database connection, not # per-model. # - # In this example a <tt>Balance</tt> record is transactionally saved even - # though <tt>transaction</tt> is called on the <tt>Account</tt> class: + # In this example a +balance+ record is transactionally saved even + # though +transaction+ is called on the +Account+ class: # # Account.transaction do # balance.save! # account.save! # end # - # Note that the +transaction+ method is also available as a model instance - # method. For example, you can also do this: + # The +transaction+ method is also available as a model instance method. + # For example, you can also do this: # # balance.transaction do # balance.save! @@ -62,9 +62,9 @@ module ActiveRecord # # == Transactions are not distributed across database connections # - # A transaction acts on a single database connection. If you have + # A transaction acts on a single database connection. If you have # multiple class-specific databases, the transaction will not protect - # interaction among them. One workaround is to begin a transaction + # interaction among them. One workaround is to begin a transaction # on each class whose models you alter: # # Student.transaction do @@ -74,16 +74,22 @@ module ActiveRecord # end # end # - # This is a poor solution, but full distributed transactions are beyond + # This is a poor solution, but fully distributed transactions are beyond # the scope of Active Record. # - # == Save and destroy are automatically wrapped in a transaction + # == +save+ and +destroy+ are automatically wrapped in a transaction # - # Both Base#save and Base#destroy come wrapped in a transaction that ensures - # that whatever you do in validations or callbacks will happen under the - # protected cover of a transaction. So you can use validations to check for - # values that the transaction depends on or you can raise exceptions in the - # callbacks to rollback, including <tt>after_*</tt> callbacks. + # Both +save+ and +destroy+ come wrapped in a transaction that ensures + # that whatever you do in validations or callbacks will happen under its + # protected cover. So you can use validations to check for values that + # the transaction depends on or you can raise exceptions in the callbacks + # to rollback, including <tt>after_*</tt> callbacks. + # + # As a consequence changes to the database are not seen outside your connection + # until the operation is complete. For example, if you try to update the index + # of a search engine in +after_save+ the indexer won't see the updated record. + # The +after_commit+ callback is the only one that is triggered once the update + # is committed. See below. # # == Exception handling and rolling back # @@ -91,14 +97,14 @@ module ActiveRecord # be propagated (after triggering the ROLLBACK), so you should be ready to # catch those in your application code. # - # One exception is the ActiveRecord::Rollback exception, which will trigger + # One exception is the <tt>ActiveRecord::Rollback</tt> exception, which will trigger # a ROLLBACK when raised, but not be re-raised by the transaction block. # - # *Warning*: one should not catch ActiveRecord::StatementInvalid exceptions - # inside a transaction block. StatementInvalid exceptions indicate that an + # *Warning*: one should not catch <tt>ActiveRecord::StatementInvalid</tt> exceptions + # inside a transaction block. <tt>ActiveRecord::StatementInvalid</tt> exceptions indicate that an # error occurred at the database level, for example when a unique constraint # is violated. On some database systems, such as PostgreSQL, database errors - # inside a transaction causes the entire transaction to become unusable + # inside a transaction cause the entire transaction to become unusable # until it's restarted from the beginning. Here is an example which # demonstrates the problem: # @@ -120,11 +126,12 @@ module ActiveRecord # # ignored until end of transaction block" # end # - # One should restart the entire transaction if a StatementError occurred. + # One should restart the entire transaction if an + # <tt>ActiveRecord::StatementInvalid</tt> occurred. # # == Nested transactions # - # #transaction calls can be nested. By default, this makes all database + # +transaction+ calls can be nested. By default, this makes all database # statements in the nested transaction block become part of the parent # transaction. For example: # @@ -139,7 +146,7 @@ module ActiveRecord # User.find(:all) # => empty # # It is also possible to requires a sub-transaction by passing - # <tt>:requires_new => true</tt>. If anything goes wrong, the + # <tt>:requires_new => true</tt>. If anything goes wrong, the # database rolls back to the beginning of the sub-transaction # without rolling back the parent transaction. For example: # |