aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorMark Thomson <nzl216@gmail.com>2012-03-17 22:29:46 -0500
committerMark Thomson <nzl216@gmail.com>2012-03-17 22:29:46 -0500
commitf2bc404ba82431d32a35b4de15cb21f179bc24c7 (patch)
treeca58ce1118eeda244ced0ef0a1d94ed7ca38e5e0 /activerecord
parent98b4ef730696062b624c508d22ca76d9caa018cc (diff)
parent6ce54d4ba8c220a84e55e7dd798d364c3f48d9f7 (diff)
downloadrails-f2bc404ba82431d32a35b4de15cb21f179bc24c7.tar.gz
rails-f2bc404ba82431d32a35b4de15cb21f179bc24c7.tar.bz2
rails-f2bc404ba82431d32a35b4de15cb21f179bc24c7.zip
Merge remote-tracking branch 'origin/master'
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md37
-rw-r--r--activerecord/RUNNING_UNIT_TESTS7
-rw-r--r--activerecord/lib/active_record.rb1
-rw-r--r--activerecord/lib/active_record/associations/association.rb13
-rw-r--r--activerecord/lib/active_record/associations/collection_association.rb2
-rw-r--r--activerecord/lib/active_record/attribute_methods.rb73
-rw-r--r--activerecord/lib/active_record/attribute_methods/dirty.rb5
-rw-r--r--activerecord/lib/active_record/autosave_association.rb8
-rw-r--r--activerecord/lib/active_record/base.rb3
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb35
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract_adapter.rb39
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb2
-rw-r--r--activerecord/lib/active_record/core.rb4
-rw-r--r--activerecord/lib/active_record/counter_cache.rb2
-rw-r--r--activerecord/lib/active_record/dynamic_finder_match.rb26
-rw-r--r--activerecord/lib/active_record/fixtures.rb4
-rw-r--r--activerecord/lib/active_record/identity_map.rb144
-rw-r--r--activerecord/lib/active_record/inheritance.rb23
-rw-r--r--activerecord/lib/active_record/locking/optimistic.rb2
-rw-r--r--activerecord/lib/active_record/model.rb1
-rw-r--r--activerecord/lib/active_record/model_schema.rb3
-rw-r--r--activerecord/lib/active_record/persistence.rb20
-rw-r--r--activerecord/lib/active_record/railtie.rb5
-rw-r--r--activerecord/lib/active_record/relation.rb15
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb12
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb2
-rw-r--r--activerecord/lib/active_record/test_case.rb10
-rw-r--r--activerecord/lib/active_record/transactions.rb2
-rw-r--r--activerecord/lib/active_record/validations/uniqueness.rb20
-rw-r--r--activerecord/test/cases/adapters/postgresql/explain_test.rb7
-rw-r--r--activerecord/test/cases/associations/eager_load_includes_full_sti_class_test.rb1
-rw-r--r--activerecord/test/cases/associations/eager_test.rb20
-rw-r--r--activerecord/test/cases/associations/has_many_associations_test.rb7
-rw-r--r--activerecord/test/cases/associations/identity_map_test.rb137
-rw-r--r--activerecord/test/cases/autosave_association_test.rb8
-rw-r--r--activerecord/test/cases/base_test.rb14
-rw-r--r--activerecord/test/cases/connection_pool_test.rb63
-rw-r--r--activerecord/test/cases/dynamic_finder_match_test.rb8
-rw-r--r--activerecord/test/cases/finder_respond_to_test.rb10
-rw-r--r--activerecord/test/cases/finder_test.rb26
-rw-r--r--activerecord/test/cases/helper.rb3
-rw-r--r--activerecord/test/cases/identity_map/middleware_test.rb74
-rw-r--r--activerecord/test/cases/identity_map_test.rb439
-rw-r--r--activerecord/test/cases/log_subscriber_test.rb12
-rw-r--r--activerecord/test/cases/multiple_db_test.rb8
-rw-r--r--activerecord/test/cases/query_cache_test.rb2
-rw-r--r--activerecord/test/cases/relations_test.rb50
-rw-r--r--activerecord/test/cases/schema_dumper_test.rb2
-rw-r--r--activerecord/test/cases/validations/uniqueness_validation_test.rb12
-rw-r--r--activerecord/test/support/connection.rb2
50 files changed, 409 insertions, 1016 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index d7823855a3..5a289a5aac 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,5 +1,16 @@
## Rails 4.0.0 (unreleased) ##
+* Remove IdentityMap
+
+ IdentityMap has never graduated to be an "enabled-by-default" feature, due
+ to some inconsistencies with associations, as described in this commit:
+
+ https://github.com/rails/rails/commit/302c912bf6bcd0fa200d964ec2dc4a44abe328a6
+
+ Hence the removal from the codebase, until such issues are fixed.
+
+ *Carlos Antonio da Silva*
+
* Added the schema cache dump feature.
`Schema cache dump` feature was implemetend. This feature can dump/load internal state of `SchemaCache` instance
@@ -153,6 +164,28 @@
* PostgreSQL hstore types are automatically deserialized from the database.
+## Rails 3.2.3 (unreleased) ##
+
+* Added find_or_create_by_{attribute}! dynamic method. *Andrew White*
+
+* Whitelist all attribute assignment by default. Change the default for newly generated applications to whitelist all attribute assignment. Also update the generated model classes so users are reminded of the importance of attr_accessible. *NZKoz*
+
+* Update ActiveRecord::AttributeMethods#attribute_present? to return false for empty strings. *Jacobkg*
+
+* Fix associations when using per class databases. *larskanis*
+
+* Revert setting NOT NULL constraints in add_timestamps *fxn*
+
+* Fix mysql to use proper text types. Fixes #3931. *kennyj*
+
+* Fix #5069 - Protect foreign key from mass assignment through association builder. *byroot*
+
+
+## Rails 3.2.2 (March 1, 2012) ##
+
+* No changes.
+
+
## Rails 3.2.1 (January 26, 2012) ##
* The threshold for auto EXPLAIN is ignored if there's no logger. *fxn*
@@ -343,6 +376,7 @@
*Aaron Christy*
+
## Rails 3.1.4 (March 1, 2012) ##
* Fix a custom primary key regression *GH 3987*
@@ -370,6 +404,7 @@
*Julius de Bruijn*
+
### Rails 3.1.3 (November 20, 2011) ##
* Perf fix: If we're deleting all records in an association, don't add a IN(..) clause
@@ -383,6 +418,7 @@
*Christos Zisopoulos and Kenny J*
+
### Rails 3.1.2 (November 18, 2011) ##
* Fix bug with PostgreSQLAdapter#indexes. When the search path has multiple schemas, spaces
@@ -430,6 +466,7 @@
*Kenny J*
+
## Rails 3.1.1 (October 7, 2011) ##
* Add deprecation for the preload_associations method. Fixes #3022.
diff --git a/activerecord/RUNNING_UNIT_TESTS b/activerecord/RUNNING_UNIT_TESTS
index a1ed6712c5..2c310e7ac3 100644
--- a/activerecord/RUNNING_UNIT_TESTS
+++ b/activerecord/RUNNING_UNIT_TESTS
@@ -26,13 +26,6 @@ You can run all the tests for a given database via rake:
The 'rake test' task will run all the tests for mysql, mysql2, sqlite3 and postgresql.
-== Identity Map
-
-By default the tests run with the Identity Map turned off. But all tests should pass whether or
-not the identity map is on or off. You can turn it on using the IM env variable:
-
- $ IM=true ruby -Itest test/case/base_test.rb
-
== Config file
By default, the config file is expected to be at the path test/config.yml. You can specify a
diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb
index 73c8a06ab7..0cb0644bcf 100644
--- a/activerecord/lib/active_record.rb
+++ b/activerecord/lib/active_record.rb
@@ -65,7 +65,6 @@ module ActiveRecord
autoload :DynamicFinderMatch
autoload :DynamicScopeMatch
autoload :Explain
- autoload :IdentityMap
autoload :Inheritance
autoload :Integration
autoload :Migration
diff --git a/activerecord/lib/active_record/associations/association.rb b/activerecord/lib/active_record/associations/association.rb
index 512c52338e..fb0ca15c23 100644
--- a/activerecord/lib/active_record/associations/association.rb
+++ b/activerecord/lib/active_record/associations/association.rb
@@ -45,7 +45,6 @@ module ActiveRecord
# Resets the \loaded flag to +false+ and sets the \target to +nil+.
def reset
@loaded = false
- IdentityMap.remove(target) if IdentityMap.enabled? && target
@target = nil
end
@@ -135,17 +134,7 @@ module ActiveRecord
# ActiveRecord::RecordNotFound is rescued within the method, and it is
# not reraised. The proxy is \reset and +nil+ is the return value.
def load_target
- if find_target?
- begin
- if IdentityMap.enabled? && association_class && association_class.respond_to?(:base_class)
- @target = IdentityMap.get(association_class, owner[reflection.foreign_key])
- end
- rescue NameError
- nil
- ensure
- @target ||= find_target
- end
- end
+ @target ||= find_target if find_target?
loaded! unless loaded?
target
rescue ActiveRecord::RecordNotFound
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb
index b2136605e1..da4c311bce 100644
--- a/activerecord/lib/active_record/associations/collection_association.rb
+++ b/activerecord/lib/active_record/associations/collection_association.rb
@@ -544,7 +544,7 @@ module ActiveRecord
# If using a custom finder_sql, #find scans the entire collection.
def find_by_scan(*args)
expects_array = args.first.kind_of?(Array)
- ids = args.flatten.compact.uniq.map { |arg| arg.to_i }
+ ids = args.flatten.compact.map{ |arg| arg.to_i }.uniq
if ids.size == 1
id = ids.first
diff --git a/activerecord/lib/active_record/attribute_methods.rb b/activerecord/lib/active_record/attribute_methods.rb
index 93c243e7b1..bebf6a1a05 100644
--- a/activerecord/lib/active_record/attribute_methods.rb
+++ b/activerecord/lib/active_record/attribute_methods.rb
@@ -214,37 +214,64 @@ module ActiveRecord
value
end
- # Returns a copy of the attributes hash where all the values have been safely quoted for use in
- # an Arel insert/update method.
- def arel_attributes_values(include_primary_key = true, include_readonly_attributes = true, attribute_names = @attributes.keys)
- attrs = {}
- klass = self.class
- arel_table = klass.arel_table
+ def arel_attributes_with_values_for_create(pk_attribute_allowed)
+ arel_attributes_with_values(attributes_for_create(pk_attribute_allowed))
+ end
- attribute_names.each do |name|
- if (column = column_for_attribute(name)) && (include_primary_key || !column.primary)
+ def arel_attributes_with_values_for_update(attribute_names)
+ arel_attributes_with_values(attributes_for_update(attribute_names))
+ end
- if include_readonly_attributes || !self.class.readonly_attributes.include?(name)
+ def attribute_method?(attr_name)
+ defined?(@attributes) && @attributes.include?(attr_name)
+ end
- value = if klass.serialized_attributes.include?(name)
- @attributes[name].serialized_value
- else
- # FIXME: we need @attributes to be used consistently.
- # If the values stored in @attributes were already type
- # casted, this code could be simplified
- read_attribute(name)
- end
+ private
- attrs[arel_table[name]] = value
- end
- end
- end
+ # Returns a Hash of the Arel::Attributes and attribute values that have been
+ # type casted for use in an Arel insert/update method.
+ def arel_attributes_with_values(attribute_names)
+ attrs = {}
+ arel_table = self.class.arel_table
+ attribute_names.each do |name|
+ attrs[arel_table[name]] = typecasted_attribute_value(name)
+ end
attrs
end
- def attribute_method?(attr_name)
- defined?(@attributes) && @attributes.include?(attr_name)
+ # Filters the primary keys and readonly attributes from the attribute names.
+ def attributes_for_update(attribute_names)
+ attribute_names.select do |name|
+ column_for_attribute(name) && !pk_attribute?(name) && !readonly_attribute?(name)
+ end
+ end
+
+ # Filters out the primary keys, from the attribute names, when the primary
+ # key is to be generated (e.g. the id attribute has no value).
+ def attributes_for_create(pk_attribute_allowed)
+ @attributes.keys.select do |name|
+ column_for_attribute(name) && (pk_attribute_allowed || !pk_attribute?(name))
+ end
+ end
+
+ def readonly_attribute?(name)
+ self.class.readonly_attributes.include?(name)
+ end
+
+ def pk_attribute?(name)
+ column_for_attribute(name).primary
+ end
+
+ def typecasted_attribute_value(name)
+ if self.class.serialized_attributes.include?(name)
+ @attributes[name].serialized_value
+ else
+ # FIXME: we need @attributes to be used consistently.
+ # If the values stored in @attributes were already typecasted, this code
+ # could be simplified
+ read_attribute(name)
+ end
end
end
end
diff --git a/activerecord/lib/active_record/attribute_methods/dirty.rb b/activerecord/lib/active_record/attribute_methods/dirty.rb
index 3a737e5b35..11c63591e3 100644
--- a/activerecord/lib/active_record/attribute_methods/dirty.rb
+++ b/activerecord/lib/active_record/attribute_methods/dirty.rb
@@ -22,8 +22,6 @@ module ActiveRecord
if status = super
@previously_changed = changes
@changed_attributes.clear
- elsif IdentityMap.enabled?
- IdentityMap.remove(self)
end
status
end
@@ -34,9 +32,6 @@ module ActiveRecord
@previously_changed = changes
@changed_attributes.clear
end
- rescue
- IdentityMap.remove(self) if IdentityMap.enabled?
- raise
end
# <tt>reload</tt> the record and clears changed attributes.
diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb
index eb3ae7014e..01d25b8867 100644
--- a/activerecord/lib/active_record/autosave_association.rb
+++ b/activerecord/lib/active_record/autosave_association.rb
@@ -28,7 +28,7 @@ module ActiveRecord
# Association with autosave option defines several callbacks on your
# model (before_save, after_create, after_update). Please note that
# callbacks are executed in the order they were defined in
- # model. You should avoid modyfing the association content, before
+ # model. You should avoid modifying the association content, before
# autosave callbacks are executed. Placing your callbacks after
# associations is usually a good practice.
#
@@ -328,7 +328,6 @@ module ActiveRecord
autosave = reflection.options[:autosave]
if records = associated_records_to_validate_or_save(association, @new_record_before_save, autosave)
- begin
records.each do |record|
next if record.destroyed?
@@ -348,11 +347,6 @@ module ActiveRecord
raise ActiveRecord::Rollback unless saved
end
- rescue
- records.each {|x| IdentityMap.remove(x) } if IdentityMap.enabled?
- raise
- end
-
end
# reconstruct the scope now that we know the owner's id
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index d4d0220fb7..d25a821688 100644
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -201,6 +201,9 @@ module ActiveRecord #:nodoc:
# # Now 'Bob' exist and is an 'admin'
# User.find_or_create_by_name('Bob', :age => 40) { |u| u.admin = true }
#
+ # Adding an exclamation point (!) on to the end of <tt>find_or_create_by_</tt> will
+ # raise an <tt>ActiveRecord::RecordInvalid</tt> error if the new record is invalid.
+ #
# Use the <tt>find_or_initialize_by_</tt> finder if you want to return a new record without
# saving it first. Protected attributes won't be set unless they are given in a block.
#
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
index 37a9d216df..561e48d52e 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
@@ -128,10 +128,11 @@ module ActiveRecord
@reserved_connections[current_connection_id] ||= checkout
end
- # Check to see if there is an active connection in this connection
- # pool.
+ # Is there an open connection that is being used for the current thread?
def active_connection?
- active_connections.any?
+ @reserved_connections.fetch(current_connection_id) {
+ return false
+ }.in_use?
end
# Signal that the thread is finished with the current connection.
@@ -185,8 +186,9 @@ module ActiveRecord
end
def clear_stale_cached_connections! # :nodoc:
+ reap
end
- deprecate :clear_stale_cached_connections!
+ deprecate :clear_stale_cached_connections! => "Please use #reap instead"
# Check-out a database connection from the pool, indicating that you want
# to use it. You should call #checkin when you no longer need this.
@@ -233,6 +235,8 @@ module ActiveRecord
conn.run_callbacks :checkin do
conn.expire
end
+
+ release conn
end
end
@@ -244,10 +248,7 @@ module ActiveRecord
# FIXME: we might want to store the key on the connection so that removing
# from the reserved hash will be a little easier.
- thread_id = @reserved_connections.keys.find { |k|
- @reserved_connections[k] == conn
- }
- @reserved_connections.delete thread_id if thread_id
+ release conn
end
end
@@ -265,6 +266,20 @@ module ActiveRecord
private
+ def release(conn)
+ thread_id = nil
+
+ if @reserved_connections[current_connection_id] == conn
+ thread_id = current_connection_id
+ else
+ thread_id = @reserved_connections.keys.find { |k|
+ @reserved_connections[k] == conn
+ }
+ end
+
+ @reserved_connections.delete thread_id if thread_id
+ end
+
def new_connection
ActiveRecord::Base.send(spec.adapter_method, spec.config)
end
@@ -288,10 +303,6 @@ module ActiveRecord
end
c
end
-
- def active_connections
- @connections.find_all { |c| c.in_use? }
- end
end
# ConnectionHandler is a collection of ConnectionPool objects. It is used
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
index 767c99de4c..1d713e472b 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
@@ -284,26 +284,25 @@ module ActiveRecord
protected
- def log(sql, name = "SQL", binds = [])
- @instrumenter.instrument(
- "sql.active_record",
- :sql => sql,
- :name => name,
- :connection_id => object_id,
- :binds => binds) { yield }
- rescue Exception => e
- message = "#{e.class.name}: #{e.message}: #{sql}"
- @logger.debug message if @logger
- exception = translate_exception(e, message)
- exception.set_backtrace e.backtrace
- raise exception
- end
-
- def translate_exception(e, message)
- # override in derived class
- ActiveRecord::StatementInvalid.new(message)
- end
-
+ def log(sql, name = "SQL", binds = [])
+ @instrumenter.instrument(
+ "sql.active_record",
+ :sql => sql,
+ :name => name,
+ :connection_id => object_id,
+ :binds => binds) { yield }
+ rescue Exception => e
+ message = "#{e.class.name}: #{e.message}: #{sql}"
+ @logger.error message if @logger
+ exception = translate_exception(e, message)
+ exception.set_backtrace e.backtrace
+ raise exception
+ end
+
+ def translate_exception(e, message)
+ # override in derived class
+ ActiveRecord::StatementInvalid.new(message)
+ end
end
end
end
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
index 5b7fa029da..10a178e369 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -1006,7 +1006,7 @@ module ActiveRecord
# This should be not be called manually but set in database.yml.
def schema_search_path=(schema_csv)
if schema_csv
- execute "SET search_path TO #{schema_csv}"
+ execute("SET search_path TO #{schema_csv}", 'SCHEMA')
@schema_search_path = schema_csv
end
end
diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb
index e75a2a1cb4..9a2f859fc7 100644
--- a/activerecord/lib/active_record/core.rb
+++ b/activerecord/lib/active_record/core.rb
@@ -130,7 +130,7 @@ module ActiveRecord
end
def arel_engine
- @arel_engine ||= connection_handler.connection_pools[name] ? self : active_record_super.arel_engine
+ @arel_engine ||= connection_handler.retrieve_connection_pool(self) ? self : active_record_super.arel_engine
end
private
@@ -176,7 +176,7 @@ module ActiveRecord
assign_attributes(attributes, options) if attributes
yield self if block_given?
- run_callbacks :initialize
+ run_callbacks :initialize if _initialize_callbacks.any?
end
# Initialize an empty model object from +coder+. +coder+ must contain
diff --git a/activerecord/lib/active_record/counter_cache.rb b/activerecord/lib/active_record/counter_cache.rb
index 8d5388e1f3..f52979ebd9 100644
--- a/activerecord/lib/active_record/counter_cache.rb
+++ b/activerecord/lib/active_record/counter_cache.rb
@@ -69,8 +69,6 @@ module ActiveRecord
"#{quoted_column} = COALESCE(#{quoted_column}, 0) #{operator} #{value.abs}"
end
- IdentityMap.remove_by_id(symbolized_base_class, id) if IdentityMap.enabled?
-
update_all(updates.join(', '), primary_key => id)
end
diff --git a/activerecord/lib/active_record/dynamic_finder_match.rb b/activerecord/lib/active_record/dynamic_finder_match.rb
index 38dbbef5fc..0473d6aafc 100644
--- a/activerecord/lib/active_record/dynamic_finder_match.rb
+++ b/activerecord/lib/active_record/dynamic_finder_match.rb
@@ -7,7 +7,7 @@ module ActiveRecord
class DynamicFinderMatch
def self.match(method)
method = method.to_s
- klass = [FindBy, FindByBang, FindOrInitializeCreateBy].find do |_klass|
+ klass = klasses.find do |_klass|
_klass.matches?(method)
end
klass.new(method) if klass
@@ -17,6 +17,10 @@ module ActiveRecord
method =~ self::METHOD_PATTERN
end
+ def self.klasses
+ [FindBy, FindByBang, FindOrInitializeCreateBy, FindOrCreateByBang]
+ end
+
def initialize(method)
@finder = :first
@instantiator = nil
@@ -47,6 +51,14 @@ module ActiveRecord
arguments.size >= @attribute_names.size
end
+ def save_record?
+ @instantiator == :create
+ end
+
+ def save_method
+ bang? ? :save! : :save
+ end
+
private
def initialize_from_match_data(match_data)
@@ -81,4 +93,16 @@ module ActiveRecord
arguments.size == 1 && arguments.first.is_a?(Hash) || super
end
end
+
+ class FindOrCreateByBang < DynamicFinderMatch
+ METHOD_PATTERN = /^find_or_create_by_([_a-zA-Z]\w*)\!$/
+
+ def initialize_from_match_data(match_data)
+ @instantiator = :create
+ end
+
+ def bang?
+ true
+ end
+ end
end
diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb
index b82d5b5621..5c42e8f719 100644
--- a/activerecord/lib/active_record/fixtures.rb
+++ b/activerecord/lib/active_record/fixtures.rb
@@ -796,9 +796,7 @@ module ActiveRecord
@fixture_cache[fixture_name].delete(fixture) if force_reload
if @loaded_fixtures[fixture_name][fixture.to_s]
- ActiveRecord::IdentityMap.without do
- @fixture_cache[fixture_name][fixture] ||= @loaded_fixtures[fixture_name][fixture.to_s].find
- end
+ @fixture_cache[fixture_name][fixture] ||= @loaded_fixtures[fixture_name][fixture.to_s].find
else
raise StandardError, "No entry named '#{fixture}' found for fixture collection '#{fixture_name}'"
end
diff --git a/activerecord/lib/active_record/identity_map.rb b/activerecord/lib/active_record/identity_map.rb
deleted file mode 100644
index d9777bb2f6..0000000000
--- a/activerecord/lib/active_record/identity_map.rb
+++ /dev/null
@@ -1,144 +0,0 @@
-module ActiveRecord
- # = Active Record Identity Map
- #
- # Ensures that each object gets loaded only once by keeping every loaded
- # object in a map. Looks up objects using the map when referring to them.
- #
- # More information on Identity Map pattern:
- # http://www.martinfowler.com/eaaCatalog/identityMap.html
- #
- # == Configuration
- #
- # In order to enable IdentityMap, set <tt>config.active_record.identity_map = true</tt>
- # in your <tt>config/application.rb</tt> file.
- #
- # IdentityMap is disabled by default and still in development (i.e. use it with care).
- #
- # == Associations
- #
- # Active Record Identity Map does not track associations yet. For example:
- #
- # comment = @post.comments.first
- # comment.post = nil
- # @post.comments.include?(comment) #=> true
- #
- # Ideally, the example above would return false, removing the comment object from the
- # post association when the association is nullified. This may cause side effects, as
- # in the situation below, if Identity Map is enabled:
- #
- # Post.has_many :comments, :dependent => :destroy
- #
- # comment = @post.comments.first
- # comment.post = nil
- # comment.save
- # Post.destroy(@post.id)
- #
- # Without using Identity Map, the code above will destroy the @post object leaving
- # the comment object intact. However, once we enable Identity Map, the post loaded
- # by Post.destroy is exactly the same object as the object @post. As the object @post
- # still has the comment object in @post.comments, once Identity Map is enabled, the
- # comment object will be accidently removed.
- #
- # This inconsistency is meant to be fixed in future Rails releases.
- #
- module IdentityMap
-
- class << self
- def enabled=(flag)
- Thread.current[:identity_map_enabled] = flag
- end
-
- def enabled
- Thread.current[:identity_map_enabled]
- end
- alias enabled? enabled
-
- def repository
- Thread.current[:identity_map] ||= Hash.new { |h,k| h[k] = {} }
- end
-
- def use
- old, self.enabled = enabled, true
-
- yield if block_given?
- ensure
- self.enabled = old
- clear
- end
-
- def without
- old, self.enabled = enabled, false
-
- yield if block_given?
- ensure
- self.enabled = old
- end
-
- def get(klass, primary_key)
- record = repository[klass.symbolized_sti_name][primary_key]
-
- if record.is_a?(klass)
- ActiveSupport::Notifications.instrument("identity.active_record",
- :line => "From Identity Map (id: #{primary_key})",
- :name => "#{klass} Loaded",
- :connection_id => object_id)
-
- record
- else
- nil
- end
- end
-
- def add(record)
- repository[record.class.symbolized_sti_name][record.id] = record
- end
-
- def remove(record)
- repository[record.class.symbolized_sti_name].delete(record.id)
- end
-
- def remove_by_id(symbolized_sti_name, id)
- repository[symbolized_sti_name].delete(id)
- end
-
- def clear
- repository.clear
- end
- end
-
- # Reinitialize an Identity Map model object from +coder+.
- # +coder+ must contain the attributes necessary for initializing an empty
- # model object.
- def reinit_with(coder)
- @attributes_cache = {}
- dirty = @changed_attributes.keys
- attributes = self.class.initialize_attributes(coder['attributes'].except(*dirty))
- @attributes.update(attributes)
- @changed_attributes.update(coder['attributes'].slice(*dirty))
- @changed_attributes.delete_if{|k,v| v.eql? @attributes[k]}
-
- run_callbacks :find
-
- self
- end
-
- class Middleware
- def initialize(app)
- @app = app
- end
-
- def call(env)
- enabled = IdentityMap.enabled
- IdentityMap.enabled = true
-
- response = @app.call(env)
- response[2] = Rack::BodyProxy.new(response[2]) do
- IdentityMap.enabled = enabled
- IdentityMap.clear
- end
-
- response
- end
- end
- end
-end
diff --git a/activerecord/lib/active_record/inheritance.rb b/activerecord/lib/active_record/inheritance.rb
index 2c766411a0..ebe244c6a6 100644
--- a/activerecord/lib/active_record/inheritance.rb
+++ b/activerecord/lib/active_record/inheritance.rb
@@ -63,26 +63,9 @@ module ActiveRecord
# single-table inheritance model that makes it possible to create
# objects of different types from the same table.
def instantiate(record, column_types = {})
- sti_class = find_sti_class(record[inheritance_column])
- record_id = sti_class.primary_key && record[sti_class.primary_key]
-
- if ActiveRecord::IdentityMap.enabled? && record_id
- if (column = sti_class.columns_hash[sti_class.primary_key]) && column.number?
- record_id = record_id.to_i
- end
- if instance = IdentityMap.get(sti_class, record_id)
- instance.reinit_with('attributes' => record)
- else
- instance = sti_class.allocate.init_with('attributes' => record)
- IdentityMap.add(instance)
- end
- else
- column_types = sti_class.decorate_columns(column_types)
- instance = sti_class.allocate.init_with('attributes' => record,
- 'column_types' => column_types)
- end
-
- instance
+ sti_class = find_sti_class(record[inheritance_column])
+ column_types = sti_class.decorate_columns(column_types)
+ sti_class.allocate.init_with('attributes' => record, 'column_types' => column_types)
end
# For internal use.
diff --git a/activerecord/lib/active_record/locking/optimistic.rb b/activerecord/lib/active_record/locking/optimistic.rb
index 9b2dc096a0..8266427b71 100644
--- a/activerecord/lib/active_record/locking/optimistic.rb
+++ b/activerecord/lib/active_record/locking/optimistic.rb
@@ -84,7 +84,7 @@ module ActiveRecord
relation.table[self.class.primary_key].eq(id).and(
relation.table[lock_col].eq(quote_value(previous_lock_value))
)
- ).arel.compile_update(arel_attributes_values(false, false, attribute_names))
+ ).arel.compile_update(arel_attributes_with_values_for_update(attribute_names))
affected_rows = connection.update stmt
diff --git a/activerecord/lib/active_record/model.rb b/activerecord/lib/active_record/model.rb
index 86de5ab2fa..105d1e0e2b 100644
--- a/activerecord/lib/active_record/model.rb
+++ b/activerecord/lib/active_record/model.rb
@@ -60,7 +60,6 @@ module ActiveRecord
include AttributeMethods
include Callbacks, ActiveModel::Observing, Timestamp
include Associations
- include IdentityMap
include ActiveModel::SecurePassword
include AutosaveAssociation, NestedAttributes
include Aggregations, Transactions, Reflection, Serialization, Store
diff --git a/activerecord/lib/active_record/model_schema.rb b/activerecord/lib/active_record/model_schema.rb
index 99847ac161..c85d590ce1 100644
--- a/activerecord/lib/active_record/model_schema.rb
+++ b/activerecord/lib/active_record/model_schema.rb
@@ -160,6 +160,7 @@ module ActiveRecord
# Sets the value of inheritance_column
def inheritance_column=(value)
@inheritance_column = value.to_s
+ @explicit_inheritance_column = true
end
def sequence_name
@@ -303,7 +304,7 @@ module ActiveRecord
@column_types = nil
@content_columns = nil
@dynamic_methods_hash = nil
- @inheritance_column = nil
+ @inheritance_column = nil unless defined?(@explicit_inheritance_column) && @explicit_inheritance_column
@relation = nil
end
diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb
index c4bce87311..35c922e979 100644
--- a/activerecord/lib/active_record/persistence.rb
+++ b/activerecord/lib/active_record/persistence.rb
@@ -115,10 +115,7 @@ module ActiveRecord
# callbacks, Observer methods, or any <tt>:dependent</tt> association
# options, use <tt>#destroy</tt>.
def delete
- if persisted?
- self.class.delete(id)
- IdentityMap.remove(self) if IdentityMap.enabled?
- end
+ self.class.delete(id) if persisted?
@destroyed = true
freeze
end
@@ -129,7 +126,6 @@ module ActiveRecord
destroy_associations
if persisted?
- IdentityMap.remove(self) if IdentityMap.enabled?
pk = self.class.primary_key
column = self.class.columns_hash[pk]
substitute = connection.substitute_at(column, 0)
@@ -284,11 +280,9 @@ module ActiveRecord
clear_aggregation_cache
clear_association_cache
- IdentityMap.without do
- fresh_object = self.class.unscoped { self.class.find(id, options) }
- @attributes.update(fresh_object.instance_variable_get('@attributes'))
- @columns_hash = fresh_object.instance_variable_get('@columns_hash')
- end
+ fresh_object = self.class.unscoped { self.class.find(id, options) }
+ @attributes.update(fresh_object.instance_variable_get('@attributes'))
+ @columns_hash = fresh_object.instance_variable_get('@columns_hash')
@attributes_cache = {}
self
@@ -350,7 +344,7 @@ module ActiveRecord
# Updates the associated record with values matching those of the instance attributes.
# Returns the number of affected rows.
def update(attribute_names = @attributes.keys)
- attributes_with_values = arel_attributes_values(false, false, attribute_names)
+ attributes_with_values = arel_attributes_with_values_for_update(attribute_names)
return 0 if attributes_with_values.empty?
klass = self.class
stmt = klass.unscoped.where(klass.arel_table[klass.primary_key].eq(id)).arel.compile_update(attributes_with_values)
@@ -360,13 +354,11 @@ module ActiveRecord
# Creates a record with values matching those of the instance attributes
# and returns its id.
def create
- attributes_values = arel_attributes_values(!id.nil?)
+ attributes_values = arel_attributes_with_values_for_create(!id.nil?)
new_id = self.class.unscoped.insert attributes_values
-
self.id ||= new_id if self.class.primary_key
- IdentityMap.add(self) if IdentityMap.enabled?
@new_record = false
id
end
diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb
index 516c450f85..ee3a6bf8c0 100644
--- a/activerecord/lib/active_record/railtie.rb
+++ b/activerecord/lib/active_record/railtie.rb
@@ -53,11 +53,6 @@ module ActiveRecord
ActiveSupport.on_load(:active_record) { self.logger ||= ::Rails.logger }
end
- initializer "active_record.identity_map" do |app|
- config.app_middleware.insert_after "::ActionDispatch::Callbacks",
- "ActiveRecord::IdentityMap::Middleware" if config.active_record.delete(:identity_map)
- end
-
initializer "active_record.set_configs" do |app|
ActiveSupport.on_load(:active_record) do
if app.config.active_record.delete(:whitelist_attributes)
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index 7531e1fe6f..ae2dc6872a 100644
--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -168,13 +168,7 @@ module ActiveRecord
default_scoped = with_default_scope
if default_scoped.equal?(self)
- @records = if @readonly_value.nil? && !@klass.locking_enabled?
- eager_loading? ? find_with_associations : @klass.find_by_sql(arel, @bind_values)
- else
- IdentityMap.without do
- eager_loading? ? find_with_associations : @klass.find_by_sql(arel, @bind_values)
- end
- end
+ @records = eager_loading? ? find_with_associations : @klass.find_by_sql(arel, @bind_values)
preload = @preload_values
preload += @includes_values unless eager_loading?
@@ -274,7 +268,6 @@ module ActiveRecord
# # The same idea applies to limit and order
# Book.where('title LIKE ?', '%Rails%').order(:created_at).limit(5).update_all(:author => 'David')
def update_all(updates, conditions = nil, options = {})
- IdentityMap.repository[symbolized_base_class].clear if IdentityMap.enabled?
if conditions || options.present?
where(conditions).apply_finder_options(options.slice(:limit, :order)).update_all(updates)
else
@@ -404,7 +397,6 @@ module ActiveRecord
# If you need to destroy dependent associations or call your <tt>before_*</tt> or
# +after_destroy+ callbacks, use the +destroy_all+ method instead.
def delete_all(conditions = nil)
- IdentityMap.repository[symbolized_base_class] = {} if IdentityMap.enabled?
if conditions
where(conditions).delete_all
else
@@ -437,7 +429,6 @@ module ActiveRecord
# # Delete multiple rows
# Todo.delete([2,3,4])
def delete(id_or_array)
- IdentityMap.remove_by_id(self.symbolized_base_class, id_or_array) if IdentityMap.enabled?
where(primary_key => id_or_array).delete_all
end
@@ -516,6 +507,10 @@ module ActiveRecord
end
end
+ def blank?
+ to_a.blank?
+ end
+
private
def references_eager_loaded_tables?
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb
index 4cd703e0a5..52e67ecc6e 100644
--- a/activerecord/lib/active_record/relation/finder_methods.rb
+++ b/activerecord/lib/active_record/relation/finder_methods.rb
@@ -290,7 +290,7 @@ module ActiveRecord
r.assign_attributes(unprotected_attributes_for_create, :without_protection => true)
end
yield(record) if block_given?
- record.save if match.instantiator == :create
+ record.send(match.save_method) if match.save_record?
end
record
@@ -318,17 +318,7 @@ module ActiveRecord
def find_one(id)
id = id.id if ActiveRecord::Base === id
- if IdentityMap.enabled? && where_values.blank? &&
- limit_value.blank? && order_values.blank? &&
- includes_values.blank? && preload_values.blank? &&
- readonly_value.nil? && joins_values.blank? &&
- !@klass.locking_enabled? &&
- record = IdentityMap.get(@klass, id)
- return record
- end
-
column = columns_hash[primary_key]
-
substitute = connection.substitute_at(column, @bind_values.length)
relation = where(table[primary_key].eq(substitute))
relation.bind_values += [[column, id]]
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index 87dd513880..d737b34115 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -329,7 +329,7 @@ module ActiveRecord
arel.having(*@having_values.uniq.reject{|h| h.blank?}) unless @having_values.empty?
arel.take(connection.sanitize_limit(@limit_value)) if @limit_value
- arel.skip(@offset_value) if @offset_value
+ arel.skip(@offset_value.to_i) if @offset_value
arel.group(*@group_values.uniq.reject{|g| g.blank?}) unless @group_values.empty?
diff --git a/activerecord/lib/active_record/test_case.rb b/activerecord/lib/active_record/test_case.rb
index 4d881f0f7d..fcaa4b74a6 100644
--- a/activerecord/lib/active_record/test_case.rb
+++ b/activerecord/lib/active_record/test_case.rb
@@ -7,20 +7,10 @@ module ActiveRecord
#
# Defines some test assertions to test against SQL queries.
class TestCase < ActiveSupport::TestCase #:nodoc:
- setup :cleanup_identity_map
-
- def setup
- cleanup_identity_map
- end
-
def teardown
SQLCounter.log.clear
end
- def cleanup_identity_map
- ActiveRecord::IdentityMap.clear
- end
-
def assert_date_from_db(expected, actual, message = nil)
# SybaseAdapter doesn't have a separate column type just for dates,
# so the time is in the string and incorrectly formatted
diff --git a/activerecord/lib/active_record/transactions.rb b/activerecord/lib/active_record/transactions.rb
index b492377d18..743dfc5a38 100644
--- a/activerecord/lib/active_record/transactions.rb
+++ b/activerecord/lib/active_record/transactions.rb
@@ -251,7 +251,6 @@ module ActiveRecord
remember_transaction_record_state
yield
rescue Exception
- IdentityMap.remove(self) if IdentityMap.enabled?
restore_transaction_record_state
raise
ensure
@@ -270,7 +269,6 @@ module ActiveRecord
def rolledback!(force_restore_state = false) #:nodoc:
run_callbacks :rollback
ensure
- IdentityMap.remove(self) if IdentityMap.enabled?
restore_transaction_record_state(force_restore_state)
end
diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb
index 9556878f63..db618f617f 100644
--- a/activerecord/lib/active_record/validations/uniqueness.rb
+++ b/activerecord/lib/active_record/validations/uniqueness.rb
@@ -35,8 +35,14 @@ module ActiveRecord
relation = relation.and(table[scope_item].eq(scope_value))
end
- if finder_class.unscoped.where(relation).exists?
- record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope).merge(:value => value))
+ relation = finder_class.unscoped.where(relation)
+
+ if options[:conditions]
+ relation = relation.merge(options[:conditions])
+ end
+
+ if relation.exists?
+ record.errors.add(attribute, :taken, options.except(:case_sensitive, :scope, :conditions).merge(:value => value))
end
end
@@ -102,6 +108,14 @@ module ActiveRecord
# validates_uniqueness_of :teacher_id, :scope => [:semester_id, :class_id]
# end
#
+ # It is also possible to limit the uniqueness constraint to a set of records matching certain conditions.
+ # In this example archived articles are not being taken into consideration when validating uniqueness
+ # of the title attribute:
+ #
+ # class Article < ActiveRecord::Base
+ # validates_uniqueness_of :title, :conditions => where('status != ?', 'archived')
+ # end
+ #
# When the record is created, a check is performed to make sure that no record exists in the database
# with the given value for the specified attribute (that maps to a column). When the record is updated,
# the same check is made but disregarding the record itself.
@@ -109,6 +123,8 @@ module ActiveRecord
# Configuration options:
# * <tt>:message</tt> - Specifies a custom error message (default is: "has already been taken").
# * <tt>:scope</tt> - One or more columns by which to limit the scope of the uniqueness constraint.
+ # * <tt>:conditions</tt> - Specify the conditions to be included as a <tt>WHERE</tt> SQL fragment to limit
+ # the uniqueness constraint lookup. (e.g. <tt>:conditions => where('status = ?', 'active')</tt>)
# * <tt>:case_sensitive</tt> - Looks for an exact match. Ignored by non-text columns (+true+ by default).
# * <tt>:allow_nil</tt> - If set to true, skips this validation if the attribute is +nil+ (default is +false+).
# * <tt>:allow_blank</tt> - If set to true, skips this validation if the attribute is blank (default is +false+).
diff --git a/activerecord/test/cases/adapters/postgresql/explain_test.rb b/activerecord/test/cases/adapters/postgresql/explain_test.rb
index 0b61f61572..619d581d5f 100644
--- a/activerecord/test/cases/adapters/postgresql/explain_test.rb
+++ b/activerecord/test/cases/adapters/postgresql/explain_test.rb
@@ -22,6 +22,13 @@ module ActiveRecord
assert_match %(EXPLAIN for: SELECT "audit_logs".* FROM "audit_logs" WHERE "audit_logs"."developer_id" IN (1)), explain
assert_match %(Seq Scan on audit_logs), explain
end
+
+ def test_dont_explain_for_set_search_path
+ queries = Thread.current[:available_queries_for_explain] = []
+ ActiveRecord::Base.connection.schema_search_path = "public"
+ assert queries.empty?
+ end
+
end
end
end
diff --git a/activerecord/test/cases/associations/eager_load_includes_full_sti_class_test.rb b/activerecord/test/cases/associations/eager_load_includes_full_sti_class_test.rb
index 7965bb404c..3044a8c312 100644
--- a/activerecord/test/cases/associations/eager_load_includes_full_sti_class_test.rb
+++ b/activerecord/test/cases/associations/eager_load_includes_full_sti_class_test.rb
@@ -27,7 +27,6 @@ class EagerLoadIncludeFullStiClassNamesTest < ActiveRecord::TestCase
post = Namespaced::Post.find_by_title( 'Great stuff', :include => :tagging )
assert_nil post.tagging
- ActiveRecord::IdentityMap.clear
ActiveRecord::Base.store_full_sti_class = true
post = Namespaced::Post.find_by_title( 'Great stuff', :include => :tagging )
assert_instance_of Tagging, post.tagging
diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb
index b79c69bbb5..b27a93f857 100644
--- a/activerecord/test/cases/associations/eager_test.rb
+++ b/activerecord/test/cases/associations/eager_test.rb
@@ -197,7 +197,7 @@ class EagerAssociationTest < ActiveRecord::TestCase
author = authors(:david)
post = author.post_about_thinking_with_last_comment
last_comment = post.last_comment
- author = assert_queries(ActiveRecord::IdentityMap.enabled? ? 2 : 3) { Author.find(author.id, :include => {:post_about_thinking_with_last_comment => :last_comment})} # find the author, then find the posts, then find the comments
+ author = assert_queries(3) { Author.find(author.id, :include => {:post_about_thinking_with_last_comment => :last_comment})} # find the author, then find the posts, then find the comments
assert_no_queries do
assert_equal post, author.post_about_thinking_with_last_comment
assert_equal last_comment, author.post_about_thinking_with_last_comment.last_comment
@@ -208,7 +208,7 @@ class EagerAssociationTest < ActiveRecord::TestCase
post = posts(:welcome)
author = post.author
author_address = author.author_address
- post = assert_queries(ActiveRecord::IdentityMap.enabled? ? 2 : 3) { Post.find(post.id, :include => {:author_with_address => :author_address}) } # find the post, then find the author, then find the address
+ post = assert_queries(3) { Post.find(post.id, :include => {:author_with_address => :author_address}) } # find the post, then find the author, then find the address
assert_no_queries do
assert_equal author, post.author_with_address
assert_equal author_address, post.author_with_address.author_address
@@ -611,9 +611,9 @@ class EagerAssociationTest < ActiveRecord::TestCase
assert posts[1].categories.include?(categories(:general))
end
- # This is only really relevant when the identity map is off. Since the preloader for habtm
- # gets raw row hashes from the database and then instantiates them, this test ensures that
- # it only instantiates one actual object per record from the database.
+ # Since the preloader for habtm gets raw row hashes from the database and then
+ # instantiates them, this test ensures that it only instantiates one actual
+ # object per record from the database.
def test_has_and_belongs_to_many_should_not_instantiate_same_records_multiple_times
welcome = posts(:welcome)
categories = Category.includes(:posts)
@@ -1002,18 +1002,18 @@ class EagerAssociationTest < ActiveRecord::TestCase
assert_equal [posts(:welcome)], posts
assert_equal authors(:david), assert_no_queries { posts[0].author}
- posts = assert_queries(ActiveRecord::IdentityMap.enabled? ? 1 : 2) do
+ posts = assert_queries(2) do
Post.find(:all, :select => 'distinct posts.*', :include => :author, :joins => [:comments], :conditions => "comments.body like 'Thank you%'", :order => 'posts.id')
end
assert_equal [posts(:welcome)], posts
assert_equal authors(:david), assert_no_queries { posts[0].author}
- posts = assert_queries(ActiveRecord::IdentityMap.enabled? ? 1 : 2) do
+ posts = assert_queries(2) do
Post.find(:all, :include => :author, :joins => {:taggings => :tag}, :conditions => "tags.name = 'General'", :order => 'posts.id')
end
assert_equal posts(:welcome, :thinking), posts
- posts = assert_queries(ActiveRecord::IdentityMap.enabled? ? 1 : 2) do
+ posts = assert_queries(2) do
Post.find(:all, :include => :author, :joins => {:taggings => {:tag => :taggings}}, :conditions => "taggings_tags.super_tag_id=2", :order => 'posts.id')
end
assert_equal posts(:welcome, :thinking), posts
@@ -1027,7 +1027,7 @@ class EagerAssociationTest < ActiveRecord::TestCase
assert_equal [posts(:welcome)], posts
assert_equal authors(:david), assert_no_queries { posts[0].author}
- posts = assert_queries(ActiveRecord::IdentityMap.enabled? ? 1 : 2) do
+ posts = assert_queries(2) do
Post.find(:all, :select => 'distinct posts.*', :include => :author, :joins => ["INNER JOIN comments on comments.post_id = posts.id"], :conditions => "comments.body like 'Thank you%'", :order => 'posts.id')
end
assert_equal [posts(:welcome)], posts
@@ -1116,7 +1116,7 @@ class EagerAssociationTest < ActiveRecord::TestCase
def test_preloading_empty_belongs_to_polymorphic
t = Tagging.create!(:taggable_type => 'Post', :taggable_id => Post.maximum(:id) + 1, :tag => tags(:general))
- tagging = assert_queries(ActiveRecord::IdentityMap.enabled? ? 1 : 2) { Tagging.preload(:taggable).find(t.id) }
+ tagging = assert_queries(2) { Tagging.preload(:taggable).find(t.id) }
assert_no_queries { assert_nil tagging.taggable }
end
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb
index 02a7f6af78..6a4f972356 100644
--- a/activerecord/test/cases/associations/has_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_associations_test.rb
@@ -378,6 +378,13 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert_equal 1, Firm.find(:first, :order => "id").clients_using_sql.size
end
+ def test_finding_using_sql_take_into_account_only_uniq_ids
+ firm = Firm.find(:first, :order => "id")
+ client = firm.clients_using_sql.first
+ assert_equal client, firm.clients_using_sql.find(client.id, client.id)
+ assert_equal client, firm.clients_using_sql.find(client.id, client.id.to_s)
+ end
+
def test_counting_using_sql
assert_equal 1, Firm.find(:first, :order => "id").clients_using_counter_sql.size
assert Firm.find(:first, :order => "id").clients_using_counter_sql.any?
diff --git a/activerecord/test/cases/associations/identity_map_test.rb b/activerecord/test/cases/associations/identity_map_test.rb
deleted file mode 100644
index 9b8635774c..0000000000
--- a/activerecord/test/cases/associations/identity_map_test.rb
+++ /dev/null
@@ -1,137 +0,0 @@
-require "cases/helper"
-require 'models/author'
-require 'models/post'
-
-if ActiveRecord::IdentityMap.enabled?
-class InverseHasManyIdentityMapTest < ActiveRecord::TestCase
- fixtures :authors, :posts
-
- def test_parent_instance_should_be_shared_with_every_child_on_find
- m = Author.first
- is = m.posts
- is.each do |i|
- assert_equal m.name, i.author.name, "Name of man should be the same before changes to parent instance"
- m.name = 'Bongo'
- assert_equal m.name, i.author.name, "Name of man should be the same after changes to parent instance"
- i.author.name = 'Mungo'
- assert_equal m.name, i.author.name, "Name of man should be the same after changes to child-owned instance"
- end
- end
-
- def test_parent_instance_should_be_shared_with_eager_loaded_children
- m = Author.find(:first, :include => :posts)
- is = m.posts
- is.each do |i|
- assert_equal m.name, i.author.name, "Name of man should be the same before changes to parent instance"
- m.name = 'Bongo'
- assert_equal m.name, i.author.name, "Name of man should be the same after changes to parent instance"
- i.author.name = 'Mungo'
- assert_equal m.name, i.author.name, "Name of man should be the same after changes to child-owned instance"
- end
-
- m = Author.find(:first, :include => :posts, :order => 'posts.id')
- is = m.posts
- is.each do |i|
- assert_equal m.name, i.author.name, "Name of man should be the same before changes to parent instance"
- m.name = 'Bongo'
- assert_equal m.name, i.author.name, "Name of man should be the same after changes to parent instance"
- i.author.name = 'Mungo'
- assert_equal m.name, i.author.name, "Name of man should be the same after changes to child-owned instance"
- end
- end
-
- def test_parent_instance_should_be_shared_with_newly_built_child
- m = Author.first
- i = m.posts.build(:title => 'Industrial Revolution Re-enactment', :body => 'Lorem ipsum')
- assert_not_nil i.author
- assert_equal m.name, i.author.name, "Name of man should be the same before changes to parent instance"
- m.name = 'Bongo'
- assert_equal m.name, i.author.name, "Name of man should be the same after changes to parent instance"
- i.author.name = 'Mungo'
- assert_equal m.name, i.author.name, "Name of man should be the same after changes to just-built-child-owned instance"
- end
-
- def test_parent_instance_should_be_shared_with_newly_block_style_built_child
- m = Author.first
- i = m.posts.build {|ii| ii.title = 'Industrial Revolution Re-enactment'; ii.body = 'Lorem ipsum'}
- assert_not_nil i.title, "Child attributes supplied to build via blocks should be populated"
- assert_not_nil i.author
- assert_equal m.name, i.author.name, "Name of man should be the same before changes to parent instance"
- m.name = 'Bongo'
- assert_equal m.name, i.author.name, "Name of man should be the same after changes to parent instance"
- i.author.name = 'Mungo'
- assert_equal m.name, i.author.name, "Name of man should be the same after changes to just-built-child-owned instance"
- end
-
- def test_parent_instance_should_be_shared_with_newly_created_child
- m = Author.first
- i = m.posts.create(:title => 'Industrial Revolution Re-enactment', :body => 'Lorem ipsum')
- assert_not_nil i.author
- assert_equal m.name, i.author.name, "Name of man should be the same before changes to parent instance"
- m.name = 'Bongo'
- assert_equal m.name, i.author.name, "Name of man should be the same after changes to parent instance"
- i.author.name = 'Mungo'
- assert_equal m.name, i.author.name, "Name of man should be the same after changes to newly-created-child-owned instance"
- end
-
- def test_parent_instance_should_be_shared_with_newly_created_via_bang_method_child
- m = Author.first
- i = m.posts.create!(:title => 'Industrial Revolution Re-enactment', :body => 'Lorem ipsum')
- assert_not_nil i.author
- assert_equal m.name, i.author.name, "Name of man should be the same before changes to parent instance"
- m.name = 'Bongo'
- assert_equal m.name, i.author.name, "Name of man should be the same after changes to parent instance"
- i.author.name = 'Mungo'
- assert_equal m.name, i.author.name, "Name of man should be the same after changes to newly-created-child-owned instance"
- end
-
- def test_parent_instance_should_be_shared_with_newly_block_style_created_child
- m = Author.first
- i = m.posts.create {|ii| ii.title = 'Industrial Revolution Re-enactment'; ii.body = 'Lorem ipsum'}
- assert_not_nil i.title, "Child attributes supplied to create via blocks should be populated"
- assert_not_nil i.author
- assert_equal m.name, i.author.name, "Name of man should be the same before changes to parent instance"
- m.name = 'Bongo'
- assert_equal m.name, i.author.name, "Name of man should be the same after changes to parent instance"
- i.author.name = 'Mungo'
- assert_equal m.name, i.author.name, "Name of man should be the same after changes to newly-created-child-owned instance"
- end
-
- def test_parent_instance_should_be_shared_with_poked_in_child
- m = Author.first
- i = Post.create(:title => 'Industrial Revolution Re-enactment', :body => 'Lorem ipsum')
- m.posts << i
- assert_not_nil i.author
- assert_equal m.name, i.author.name, "Name of man should be the same before changes to parent instance"
- m.name = 'Bongo'
- assert_equal m.name, i.author.name, "Name of man should be the same after changes to parent instance"
- i.author.name = 'Mungo'
- assert_equal m.name, i.author.name, "Name of man should be the same after changes to newly-created-child-owned instance"
- end
-
- def test_parent_instance_should_be_shared_with_replaced_via_accessor_children
- m = Author.first
- i = Post.new(:title => 'Industrial Revolution Re-enactment', :body => 'Lorem ipsum')
- m.posts = [i]
- assert_same m, i.author
- assert_not_nil i.author
- assert_equal m.name, i.author.name, "Name of man should be the same before changes to parent instance"
- m.name = 'Bongo'
- assert_equal m.name, i.author.name, "Name of man should be the same after changes to parent instance"
- i.author.name = 'Mungo'
- assert_equal m.name, i.author.name, "Name of man should be the same after changes to replaced-child-owned instance"
- end
-
- def test_parent_instance_should_be_shared_with_replaced_via_method_children
- m = Author.first
- i = Post.new(:title => 'Industrial Revolution Re-enactment', :body => 'Lorem ipsum')
- m.posts = [i]
- assert_not_nil i.author
- assert_equal m.name, i.author.name, "Name of man should be the same before changes to parent instance"
- m.name = 'Bongo'
- assert_equal m.name, i.author.name, "Name of man should be the same after changes to parent instance"
- i.author.name = 'Mungo'
- assert_equal m.name, i.author.name, "Name of man should be the same after changes to replaced-child-owned instance"
- end
-end
-end
diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb
index 1376810adf..67b65c51d5 100644
--- a/activerecord/test/cases/autosave_association_test.rb
+++ b/activerecord/test/cases/autosave_association_test.rb
@@ -978,10 +978,7 @@ class TestAutosaveAssociationOnAHasOneAssociation < ActiveRecord::TestCase
values = [@pirate.reload.catchphrase, @pirate.ship.name, *@pirate.ship.parts.map(&:name)]
# Oracle saves empty string as NULL
if current_adapter?(:OracleAdapter)
- expected = ActiveRecord::IdentityMap.enabled? ?
- [nil, nil, '', ''] :
- [nil, nil, nil, nil]
- assert_equal expected, values
+ assert_equal [nil, nil, nil, nil], values
else
assert_equal ['', '', '', ''], values
end
@@ -1077,8 +1074,7 @@ class TestAutosaveAssociationOnABelongsToAssociation < ActiveRecord::TestCase
@ship.save(:validate => false)
# Oracle saves empty string as NULL
if current_adapter?(:OracleAdapter)
- expected = ActiveRecord::IdentityMap.enabled? ? [nil, ''] : [nil, nil]
- assert_equal expected, [@ship.reload.name, @ship.pirate.catchphrase]
+ assert_equal [nil, nil], [@ship.reload.name, @ship.pirate.catchphrase]
else
assert_equal ['', ''], [@ship.reload.name, @ship.pirate.catchphrase]
end
diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb
index 01f647b261..bddaf6afa8 100644
--- a/activerecord/test/cases/base_test.rb
+++ b/activerecord/test/cases/base_test.rb
@@ -1139,7 +1139,7 @@ class BasicsTest < ActiveRecord::TestCase
assert g.save
# Reload and check that we have all the geometric attributes.
- h = ActiveRecord::IdentityMap.without { Geometric.find(g.id) }
+ h = Geometric.find(g.id)
assert_equal '(5,6.1)', h.a_point
assert_equal '[(2,3),(5.5,7)]', h.a_line_segment
@@ -1168,7 +1168,7 @@ class BasicsTest < ActiveRecord::TestCase
assert g.save
# Reload and check that we have all the geometric attributes.
- h = ActiveRecord::IdentityMap.without { Geometric.find(g.id) }
+ h = Geometric.find(g.id)
assert_equal '(5,6.1)', h.a_point
assert_equal '[(2,3),(5.5,7)]', h.a_line_segment
@@ -1503,6 +1503,16 @@ class BasicsTest < ActiveRecord::TestCase
assert_equal before_seq, after_seq unless before_seq.blank? && after_seq.blank?
end
+ def test_dont_clear_inheritnce_column_when_setting_explicitly
+ Joke.inheritance_column = "my_type"
+ before_inherit = Joke.inheritance_column
+
+ Joke.reset_column_information
+ after_inherit = Joke.inheritance_column
+
+ assert_equal before_inherit, after_inherit unless before_inherit.blank? && after_inherit.blank?
+ end
+
def test_set_table_name_symbol_converted_to_string
Joke.table_name = :cold_jokes
assert_equal 'cold_jokes', Joke.table_name
diff --git a/activerecord/test/cases/connection_pool_test.rb b/activerecord/test/cases/connection_pool_test.rb
index 2c69bfde5b..da93500ce3 100644
--- a/activerecord/test/cases/connection_pool_test.rb
+++ b/activerecord/test/cases/connection_pool_test.rb
@@ -3,6 +3,8 @@ require "cases/helper"
module ActiveRecord
module ConnectionAdapters
class ConnectionPoolTest < ActiveRecord::TestCase
+ attr_reader :pool
+
def setup
super
@@ -25,6 +27,67 @@ module ActiveRecord
@pool.disconnect!
end
+ def active_connections(pool)
+ pool.connections.find_all(&:in_use?)
+ end
+
+ def test_checkout_after_close
+ connection = pool.connection
+ assert connection.in_use?
+
+ connection.close
+ assert !connection.in_use?
+
+ assert pool.connection.in_use?
+ end
+
+ def test_released_connection_moves_between_threads
+ thread_conn = nil
+
+ Thread.new {
+ pool.with_connection do |conn|
+ thread_conn = conn
+ end
+ }.join
+
+ assert thread_conn
+
+ Thread.new {
+ pool.with_connection do |conn|
+ assert_equal thread_conn, conn
+ end
+ }.join
+ end
+
+ def test_with_connection
+ assert_equal 0, active_connections(pool).size
+
+ main_thread = pool.connection
+ assert_equal 1, active_connections(pool).size
+
+ Thread.new {
+ pool.with_connection do |conn|
+ assert conn
+ assert_equal 2, active_connections(pool).size
+ end
+ assert_equal 1, active_connections(pool).size
+ }.join
+
+ main_thread.close
+ assert_equal 0, active_connections(pool).size
+ end
+
+ def test_active_connection_in_use
+ assert !pool.active_connection?
+ main_thread = pool.connection
+
+ assert pool.active_connection?
+
+ main_thread.close
+
+ assert !pool.active_connection?
+ end
+
def test_full_pool_exception
assert_raises(PoolFullError) do
(@pool.size + 1).times do
diff --git a/activerecord/test/cases/dynamic_finder_match_test.rb b/activerecord/test/cases/dynamic_finder_match_test.rb
index e576870317..db619faa83 100644
--- a/activerecord/test/cases/dynamic_finder_match_test.rb
+++ b/activerecord/test/cases/dynamic_finder_match_test.rb
@@ -83,6 +83,14 @@ module ActiveRecord
assert_equal :create, m.instantiator
end
+ def test_find_or_create!
+ m = DynamicFinderMatch.match(:find_or_create_by_foo!)
+ assert_equal :first, m.finder
+ assert m.bang?, 'should be banging'
+ assert_equal %w{ foo }, m.attribute_names
+ assert_equal :create, m.instantiator
+ end
+
def test_find_or_initialize
m = DynamicFinderMatch.match(:find_or_initialize_by_foo)
assert_equal :first, m.finder
diff --git a/activerecord/test/cases/finder_respond_to_test.rb b/activerecord/test/cases/finder_respond_to_test.rb
index 235805a67c..810c1500cc 100644
--- a/activerecord/test/cases/finder_respond_to_test.rb
+++ b/activerecord/test/cases/finder_respond_to_test.rb
@@ -56,6 +56,16 @@ class FinderRespondToTest < ActiveRecord::TestCase
assert_respond_to Topic, :find_or_create_by_title_and_author_name
end
+ def test_should_respond_to_find_or_create_from_one_attribute_bang
+ ensure_topic_method_is_not_cached(:find_or_create_by_title!)
+ assert_respond_to Topic, :find_or_create_by_title!
+ end
+
+ def test_should_respond_to_find_or_create_from_two_attributes_bang
+ ensure_topic_method_is_not_cached(:find_or_create_by_title_and_author_name!)
+ assert_respond_to Topic, :find_or_create_by_title_and_author_name!
+ end
+
def test_should_not_respond_to_find_by_one_missing_attribute
assert !Topic.respond_to?(:find_by_undertitle)
end
diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb
index 76c041397a..96c8eb6417 100644
--- a/activerecord/test/cases/finder_test.rb
+++ b/activerecord/test/cases/finder_test.rb
@@ -862,6 +862,28 @@ class FinderTest < ActiveRecord::TestCase
assert another.persisted?
end
+ def test_find_or_create_from_one_attribute_bang
+ number_of_companies = Company.count
+ assert_raises(ActiveRecord::RecordInvalid) { Company.find_or_create_by_name!("") }
+ assert_equal number_of_companies, Company.count
+ sig38 = Company.find_or_create_by_name!("38signals")
+ assert_equal number_of_companies + 1, Company.count
+ assert_equal sig38, Company.find_or_create_by_name!("38signals")
+ assert sig38.persisted?
+ end
+
+ def test_find_or_create_from_two_attributes_bang
+ number_of_companies = Company.count
+ assert_raises(ActiveRecord::RecordInvalid) { Company.find_or_create_by_name_and_firm_id!("", 17) }
+ assert_equal number_of_companies, Company.count
+ sig38 = Company.find_or_create_by_name_and_firm_id!("38signals", 17)
+ assert_equal number_of_companies + 1, Company.count
+ assert_equal sig38, Company.find_or_create_by_name_and_firm_id!("38signals", 17)
+ assert sig38.persisted?
+ assert_equal "38signals", sig38.name
+ assert_equal 17, sig38.firm_id
+ end
+
def test_find_or_create_from_two_attributes_with_one_being_an_aggregate
number_of_customers = Customer.count
created_customer = Customer.find_or_create_by_balance_and_name(Money.new(123), "Elizabeth")
@@ -1185,6 +1207,10 @@ class FinderTest < ActiveRecord::TestCase
end
end
+ def test_finder_with_offset_string
+ assert_nothing_raised(ActiveRecord::StatementInvalid) { Topic.find(:all, :offset => "3") }
+ end
+
protected
def bind(statement, *vars)
if vars.first.is_a?(Hash)
diff --git a/activerecord/test/cases/helper.rb b/activerecord/test/cases/helper.rb
index 9f5f012073..5c3560a33b 100644
--- a/activerecord/test/cases/helper.rb
+++ b/activerecord/test/cases/helper.rb
@@ -19,9 +19,6 @@ require 'support/connection'
# Show backtraces for deprecated behavior for quicker cleanup.
ActiveSupport::Deprecation.debug = true
-# Enable Identity Map only when ENV['IM'] is set to "true"
-ActiveRecord::IdentityMap.enabled = (ENV['IM'] == "true")
-
# Avoid deprecation warning setting dependent_restrict_raises to false. The default is true
ActiveRecord::Base.dependent_restrict_raises = false
diff --git a/activerecord/test/cases/identity_map/middleware_test.rb b/activerecord/test/cases/identity_map/middleware_test.rb
deleted file mode 100644
index 5b32a1c6d2..0000000000
--- a/activerecord/test/cases/identity_map/middleware_test.rb
+++ /dev/null
@@ -1,74 +0,0 @@
-require "cases/helper"
-require "rack"
-
-module ActiveRecord
- module IdentityMap
- class MiddlewareTest < ActiveRecord::TestCase
- def setup
- super
- @enabled = IdentityMap.enabled
- IdentityMap.enabled = false
- end
-
- def teardown
- super
- IdentityMap.enabled = @enabled
- IdentityMap.clear
- end
-
- def test_delegates
- called = false
- mw = Middleware.new lambda { |env|
- called = true
- [200, {}, nil]
- }
- mw.call({})
- assert called, 'middleware delegated'
- end
-
- def test_im_enabled_during_delegation
- mw = Middleware.new lambda { |env|
- assert IdentityMap.enabled?, 'identity map should be enabled'
- [200, {}, nil]
- }
- mw.call({})
- end
-
- class Enum < Struct.new(:iter)
- def each(&b)
- iter.call(&b)
- end
- end
-
- def test_im_enabled_during_body_each
- mw = Middleware.new lambda { |env|
- [200, {}, Enum.new(lambda { |&b|
- assert IdentityMap.enabled?, 'identity map should be enabled'
- b.call "hello"
- })]
- }
- body = mw.call({}).last
- body.each { |x| assert_equal 'hello', x }
- end
-
- def test_im_disabled_after_body_close
- mw = Middleware.new lambda { |env| [200, {}, []] }
- body = mw.call({}).last
- assert IdentityMap.enabled?, 'identity map should be enabled'
- body.close
- assert !IdentityMap.enabled?, 'identity map should be disabled'
- end
-
- def test_im_cleared_after_body_close
- mw = Middleware.new lambda { |env| [200, {}, []] }
- body = mw.call({}).last
-
- IdentityMap.repository['hello'] = 'world'
- assert !IdentityMap.repository.empty?, 'repo should not be empty'
-
- body.close
- assert IdentityMap.repository.empty?, 'repo should be empty'
- end
- end
- end
-end
diff --git a/activerecord/test/cases/identity_map_test.rb b/activerecord/test/cases/identity_map_test.rb
deleted file mode 100644
index 3efc8bf559..0000000000
--- a/activerecord/test/cases/identity_map_test.rb
+++ /dev/null
@@ -1,439 +0,0 @@
-require "cases/helper"
-
-require 'models/developer'
-require 'models/project'
-require 'models/company'
-require 'models/topic'
-require 'models/reply'
-require 'models/computer'
-require 'models/customer'
-require 'models/order'
-require 'models/post'
-require 'models/author'
-require 'models/tag'
-require 'models/tagging'
-require 'models/comment'
-require 'models/sponsor'
-require 'models/member'
-require 'models/essay'
-require 'models/subscriber'
-require "models/pirate"
-require "models/bird"
-require "models/parrot"
-
-if ActiveRecord::IdentityMap.enabled?
-class IdentityMapTest < ActiveRecord::TestCase
- fixtures :accounts, :companies, :developers, :projects, :topics,
- :developers_projects, :computers, :authors, :author_addresses,
- :posts, :tags, :taggings, :comments, :subscribers
-
- ##############################################################################
- # Basic tests checking if IM is functioning properly on basic find operations#
- ##############################################################################
-
- def test_find_id
- assert_same(Client.find(3), Client.find(3))
- end
-
- def test_find_id_without_identity_map
- ActiveRecord::IdentityMap.without do
- assert_not_same(Client.find(3), Client.find(3))
- end
- end
-
- def test_find_id_use_identity_map
- ActiveRecord::IdentityMap.enabled = false
- ActiveRecord::IdentityMap.use do
- assert_same(Client.find(3), Client.find(3))
- end
- ActiveRecord::IdentityMap.enabled = true
- end
-
- def test_find_pkey
- assert_same(
- Subscriber.find('swistak'),
- Subscriber.find('swistak')
- )
- end
-
- def test_find_by_id
- assert_same(
- Client.find_by_id(3),
- Client.find_by_id(3)
- )
- end
-
- def test_find_by_string_and_numeric_id
- assert_same(
- Client.find_by_id("3"),
- Client.find_by_id(3)
- )
- end
-
- def test_find_by_pkey
- assert_same(
- Subscriber.find_by_nick('swistak'),
- Subscriber.find_by_nick('swistak')
- )
- end
-
- def test_find_first_id
- assert_same(
- Client.find(:first, :conditions => {:id => 1}),
- Client.find(:first, :conditions => {:id => 1})
- )
- end
-
- def test_find_first_pkey
- assert_same(
- Subscriber.find(:first, :conditions => {:nick => 'swistak'}),
- Subscriber.find(:first, :conditions => {:nick => 'swistak'})
- )
- end
-
- def test_queries_are_not_executed_when_finding_by_id
- Post.find 1
- assert_no_queries do
- Post.find 1
- end
- end
-
- ##############################################################################
- # Tests checking if IM is functioning properly on more advanced finds #
- # and associations #
- ##############################################################################
-
- def test_owner_object_is_associated_from_identity_map
- post = Post.find(1)
- comment = post.comments.first
-
- assert_no_queries do
- comment.post
- end
- assert_same post, comment.post
- end
-
- def test_associated_object_are_assigned_from_identity_map
- post = Post.find(1)
-
- post.comments.each do |comment|
- assert_same post, comment.post
- assert_equal post.object_id, comment.post.object_id
- end
- end
-
- def test_creation
- t1 = Topic.create("title" => "t1")
- t2 = Topic.find(t1.id)
- assert_same(t1, t2)
- end
-
- ##############################################################################
- # Tests checking if IM is functioning properly on classes with multiple #
- # types of inheritance #
- ##############################################################################
-
- def test_inherited_without_type_attribute_without_identity_map
- ActiveRecord::IdentityMap.without do
- p1 = DestructivePirate.create!(:catchphrase => "I'm not a regular Pirate")
- p2 = Pirate.find(p1.id)
- assert_not_same(p1, p2)
- end
- end
-
- def test_inherited_with_type_attribute_without_identity_map
- ActiveRecord::IdentityMap.without do
- c = comments(:sub_special_comment)
- c1 = SubSpecialComment.find(c.id)
- c2 = Comment.find(c.id)
- assert_same(c1.class, c2.class)
- end
- end
-
- def test_inherited_without_type_attribute
- p1 = DestructivePirate.create!(:catchphrase => "I'm not a regular Pirate")
- p2 = Pirate.find(p1.id)
- assert_not_same(p1, p2)
- end
-
- def test_inherited_with_type_attribute
- c = comments(:sub_special_comment)
- c1 = SubSpecialComment.find(c.id)
- c2 = Comment.find(c.id)
- assert_same(c1, c2)
- end
-
- ##############################################################################
- # Tests checking dirty attribute behavior with IM #
- ##############################################################################
-
- def test_loading_new_instance_should_not_update_dirty_attributes
- swistak = Subscriber.find(:first, :conditions => {:nick => 'swistak'})
- swistak.name = "Swistak Sreberkowiec"
- assert_equal(["name"], swistak.changed)
- assert_equal({"name" => ["Marcin Raczkowski", "Swistak Sreberkowiec"]}, swistak.changes)
-
- assert swistak.name_changed?
- assert_equal("Swistak Sreberkowiec", swistak.name)
- end
-
- def test_loading_new_instance_should_change_dirty_attribute_original_value
- swistak = Subscriber.find(:first, :conditions => {:nick => 'swistak'})
- swistak.name = "Swistak Sreberkowiec"
-
- Subscriber.update_all({:name => "Raczkowski Marcin"}, {:name => "Marcin Raczkowski"})
-
- assert_equal({"name"=>["Marcin Raczkowski", "Swistak Sreberkowiec"]}, swistak.changes)
- assert_equal("Swistak Sreberkowiec", swistak.name)
- end
-
- def test_loading_new_instance_should_remove_dirt
- swistak = Subscriber.find(:first, :conditions => {:nick => 'swistak'})
- swistak.name = "Swistak Sreberkowiec"
-
- assert_equal({"name" => ["Marcin Raczkowski", "Swistak Sreberkowiec"]}, swistak.changes)
-
- Subscriber.update_all({:name => "Swistak Sreberkowiec"}, {:name => "Marcin Raczkowski"})
-
- assert_equal("Swistak Sreberkowiec", swistak.name)
- assert_equal({"name"=>["Marcin Raczkowski", "Swistak Sreberkowiec"]}, swistak.changes)
- assert swistak.name_changed?
- end
-
- def test_has_many_associations
- pirate = Pirate.create!(:catchphrase => "Don' botharrr talkin' like one, savvy?")
- pirate.birds.create!(:name => 'Posideons Killer')
- pirate.birds.create!(:name => 'Killer bandita Dionne')
-
- posideons, _ = pirate.birds
-
- pirate.reload
-
- pirate.birds_attributes = [{ :id => posideons.id, :name => 'Grace OMalley' }]
- assert_equal 'Grace OMalley', pirate.birds.to_a.find { |r| r.id == posideons.id }.name
- end
-
- def test_changing_associations
- post1 = Post.create("title" => "One post", "body" => "Posting...")
- post2 = Post.create("title" => "Another post", "body" => "Posting... Again...")
- comment = Comment.new("body" => "comment")
-
- comment.post = post1
- assert comment.save
-
- assert_same(post1.comments.first, comment)
-
- comment.post = post2
- assert comment.save
-
- assert_same(post2.comments.first, comment)
- assert_equal(0, post1.comments.size)
- end
-
- def test_im_with_polymorphic_has_many_going_through_join_model_with_custom_select_and_joins
- tag = posts(:welcome).tags.first
- tag_with_joins_and_select = posts(:welcome).tags.add_joins_and_select.first
- assert_same(tag, tag_with_joins_and_select)
- assert_nothing_raised(NoMethodError, "Joins/select was not loaded") { tag.author_id }
- end
-
- ##############################################################################
- # Tests checking Identity Map behavior with preloaded associations, joins, #
- # includes etc. #
- ##############################################################################
-
- def test_find_with_preloaded_associations
- assert_queries(2) do
- posts = Post.preload(:comments).order('posts.id')
- assert posts.first.comments.first
- end
-
- # With IM we'll retrieve post object from previous query, it'll have comments
- # already preloaded from first call
- assert_queries(1) do
- posts = Post.preload(:comments).order('posts.id')
- assert posts.first.comments.first
- end
-
- assert_queries(2) do
- posts = Post.preload(:author).order('posts.id')
- assert posts.first.author
- end
-
- # With IM we'll retrieve post object from previous query, it'll have comments
- # already preloaded from first call
- assert_queries(1) do
- posts = Post.preload(:author).order('posts.id')
- assert posts.first.author
- end
-
- assert_queries(1) do
- posts = Post.preload(:author, :comments).order('posts.id')
- assert posts.first.author
- assert posts.first.comments.first
- end
- end
-
- def test_find_with_included_associations
- assert_queries(2) do
- posts = Post.includes(:comments).order('posts.id')
- assert posts.first.comments.first
- end
-
- assert_queries(1) do
- posts = Post.scoped.includes(:comments).order('posts.id')
- assert posts.first.comments.first
- end
-
- assert_queries(2) do
- posts = Post.includes(:author).order('posts.id')
- assert posts.first.author
- end
-
- assert_queries(1) do
- posts = Post.includes(:author, :comments).order('posts.id')
- assert posts.first.author
- assert posts.first.comments.first
- end
- end
-
- def test_eager_loading_with_conditions_on_joined_table_preloads
- posts = Post.find(:all, :select => 'distinct posts.*', :include => :author, :joins => [:comments], :conditions => "comments.body like 'Thank you%'", :order => 'posts.id')
- assert_equal [posts(:welcome)], posts
- assert_equal authors(:david), assert_no_queries { posts[0].author}
- assert_same posts.first.author, Author.first
-
- posts = Post.find(:all, :select => 'distinct posts.*', :include => :author, :joins => [:comments], :conditions => "comments.body like 'Thank you%'", :order => 'posts.id')
- assert_equal [posts(:welcome)], posts
- assert_equal authors(:david), assert_no_queries { posts[0].author}
- assert_same posts.first.author, Author.first
-
- posts = Post.find(:all, :include => :author, :joins => {:taggings => :tag}, :conditions => "tags.name = 'General'", :order => 'posts.id')
- assert_equal posts(:welcome, :thinking), posts
- assert_same posts.first.author, Author.first
-
- posts = Post.find(:all, :include => :author, :joins => {:taggings => {:tag => :taggings}}, :conditions => "taggings_tags.super_tag_id=2", :order => 'posts.id')
- assert_equal posts(:welcome, :thinking), posts
- assert_same posts.first.author, Author.first
- end
-
- def test_eager_loading_with_conditions_on_string_joined_table_preloads
- posts = assert_queries(2) do
- Post.find(:all, :select => 'distinct posts.*', :include => :author, :joins => "INNER JOIN comments on comments.post_id = posts.id", :conditions => "comments.body like 'Thank you%'", :order => 'posts.id')
- end
- assert_equal [posts(:welcome)], posts
- assert_equal authors(:david), assert_no_queries { posts[0].author}
-
- posts = assert_queries(1) do
- Post.find(:all, :select => 'distinct posts.*', :include => :author, :joins => ["INNER JOIN comments on comments.post_id = posts.id"], :conditions => "comments.body like 'Thank you%'", :order => 'posts.id')
- end
- assert_equal [posts(:welcome)], posts
- assert_equal authors(:david), assert_no_queries { posts[0].author}
- end
-
- ##############################################################################
- # Behaviour related to saving failures
- ##############################################################################
-
- def test_reload_object_if_save_failed
- developer = Developer.first
- developer.salary = 0
-
- assert !developer.save
-
- same_developer = Developer.first
-
- assert_not_same developer, same_developer
- assert_not_equal 0, same_developer.salary
- assert_not_equal developer.salary, same_developer.salary
- end
-
- def test_reload_object_if_forced_save_failed
- developer = Developer.first
- developer.salary = 0
-
- assert_raise(ActiveRecord::RecordInvalid) { developer.save! }
-
- same_developer = Developer.first
-
- assert_not_same developer, same_developer
- assert_not_equal 0, same_developer.salary
- assert_not_equal developer.salary, same_developer.salary
- end
-
- def test_reload_object_if_update_attributes_fails
- developer = Developer.first
- developer.salary = 0
-
- assert !developer.update_attributes(:salary => 0)
-
- same_developer = Developer.first
-
- assert_not_same developer, same_developer
- assert_not_equal 0, same_developer.salary
- assert_not_equal developer.salary, same_developer.salary
- end
-
- ##############################################################################
- # Behaviour of readonly, frozen, destroyed
- ##############################################################################
-
- def test_find_using_identity_map_respects_readonly_when_loading_associated_object_first
- author = Author.first
- readonly_comment = author.readonly_comments.first
-
- comment = Comment.first
- assert !comment.readonly?
-
- assert readonly_comment.readonly?
-
- assert_raise(ActiveRecord::ReadOnlyRecord) {readonly_comment.save}
- assert comment.save
- end
-
- def test_find_using_identity_map_respects_readonly
- comment = Comment.first
- assert !comment.readonly?
-
- author = Author.first
- readonly_comment = author.readonly_comments.first
-
- assert readonly_comment.readonly?
-
- assert_raise(ActiveRecord::ReadOnlyRecord) {readonly_comment.save}
- assert comment.save
- end
-
- def test_find_using_select_and_identity_map
- author_id, author = Author.select('id').first, Author.first
-
- assert_equal author_id, author
- assert_same author_id, author
- assert_not_nil author.name
-
- post, post_id = Post.first, Post.select('id').first
-
- assert_equal post_id, post
- assert_same post_id, post
- assert_not_nil post.title
- end
-
-# Currently AR is not allowing changing primary key (see Persistence#update)
-# So we ignore it. If this changes, this test needs to be uncommented.
-# def test_updating_of_pkey
-# assert client = Client.find(3),
-# client.update_attribute(:id, 666)
-#
-# assert Client.find(666)
-# assert_same(client, Client.find(666))
-#
-# s = Subscriber.find_by_nick('swistak')
-# assert s.update_attribute(:nick, 'swistakTheJester')
-# assert_equal('swistakTheJester', s.nick)
-#
-# assert stj = Subscriber.find_by_nick('swistakTheJester')
-# assert_same(s, stj)
-# end
-
-end
-end
diff --git a/activerecord/test/cases/log_subscriber_test.rb b/activerecord/test/cases/log_subscriber_test.rb
index d1f0ace184..acd2fcdad4 100644
--- a/activerecord/test/cases/log_subscriber_test.rb
+++ b/activerecord/test/cases/log_subscriber_test.rb
@@ -11,8 +11,6 @@ class LogSubscriberTest < ActiveRecord::TestCase
def setup
@old_logger = ActiveRecord::Base.logger
- @using_identity_map = ActiveRecord::IdentityMap.enabled?
- ActiveRecord::IdentityMap.enabled = false
Developer.primary_key
super
ActiveRecord::LogSubscriber.attach_to(:active_record)
@@ -22,7 +20,6 @@ class LogSubscriberTest < ActiveRecord::TestCase
super
ActiveRecord::LogSubscriber.log_subscribers.pop
ActiveRecord::Base.logger = @old_logger
- ActiveRecord::IdentityMap.enabled = @using_identity_map
end
def set_logger(logger)
@@ -103,13 +100,4 @@ class LogSubscriberTest < ActiveRecord::TestCase
def test_initializes_runtime
Thread.new { assert_equal 0, ActiveRecord::LogSubscriber.runtime }.join
end
-
- def test_log
- ActiveRecord::IdentityMap.use do
- Post.find 1
- Post.find 1
- end
- wait
- assert_match(/From Identity Map/, @logger.logged(:debug).last)
- end
end
diff --git a/activerecord/test/cases/multiple_db_test.rb b/activerecord/test/cases/multiple_db_test.rb
index 917a03bb34..06d6596725 100644
--- a/activerecord/test/cases/multiple_db_test.rb
+++ b/activerecord/test/cases/multiple_db_test.rb
@@ -86,7 +86,13 @@ class MultipleDbTest < ActiveRecord::TestCase
end
def test_arel_table_engines
- assert_equal Entrant.arel_engine, Bird.arel_engine
+ assert_not_equal Entrant.arel_engine, Bird.arel_engine
+ assert_not_equal Entrant.arel_engine, Course.arel_engine
+ end
+
+ def test_connection
+ assert_equal Entrant.arel_engine.connection, Bird.arel_engine.connection
+ assert_not_equal Entrant.arel_engine.connection, Course.arel_engine.connection
end
unless in_memory_db?
diff --git a/activerecord/test/cases/query_cache_test.rb b/activerecord/test/cases/query_cache_test.rb
index f36f4237b1..d93478513b 100644
--- a/activerecord/test/cases/query_cache_test.rb
+++ b/activerecord/test/cases/query_cache_test.rb
@@ -107,7 +107,7 @@ class QueryCacheTest < ActiveRecord::TestCase
end
def test_find_queries
- assert_queries(ActiveRecord::IdentityMap.enabled? ? 1 : 2) { Task.find(1); Task.find(1) }
+ assert_queries(2) { Task.find(1); Task.find(1) }
end
def test_find_queries_with_cache
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index 7b1d65c6db..63d47f5162 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -323,7 +323,7 @@ class RelationTest < ActiveRecord::TestCase
assert posts.first.comments.first
end
- assert_queries(ActiveRecord::IdentityMap.enabled? ? 1 : 2) do
+ assert_queries(2) do
posts = Post.preload(:comments).order('posts.id')
assert posts.first.comments.first
end
@@ -333,12 +333,12 @@ class RelationTest < ActiveRecord::TestCase
assert posts.first.author
end
- assert_queries(ActiveRecord::IdentityMap.enabled? ? 1 : 2) do
+ assert_queries(2) do
posts = Post.preload(:author).order('posts.id')
assert posts.first.author
end
- assert_queries(ActiveRecord::IdentityMap.enabled? ? 1 : 3) do
+ assert_queries(3) do
posts = Post.preload(:author, :comments).order('posts.id')
assert posts.first.author
assert posts.first.comments.first
@@ -351,7 +351,7 @@ class RelationTest < ActiveRecord::TestCase
assert posts.first.comments.first
end
- assert_queries(ActiveRecord::IdentityMap.enabled? ? 1 : 2) do
+ assert_queries(2) do
posts = Post.scoped.includes(:comments).order('posts.id')
assert posts.first.comments.first
end
@@ -361,7 +361,7 @@ class RelationTest < ActiveRecord::TestCase
assert posts.first.author
end
- assert_queries(ActiveRecord::IdentityMap.enabled? ? 1 : 3) do
+ assert_queries(3) do
posts = Post.includes(:author, :comments).order('posts.id')
assert posts.first.author
assert posts.first.comments.first
@@ -462,6 +462,18 @@ class RelationTest < ActiveRecord::TestCase
assert_equal authors(:david), authors.find_or_create_by_name(:name => 'David')
end
+ def test_dynamic_find_or_create_by_attributes_bang
+ authors = Author.scoped
+
+ assert_raises(ActiveRecord::RecordInvalid) { authors.find_or_create_by_name!('') }
+
+ lifo = authors.find_or_create_by_name!('Lifo')
+ assert_equal "Lifo", lifo.name
+ assert lifo.persisted?
+
+ assert_equal authors(:david), authors.find_or_create_by_name!(:name => 'David')
+ end
+
def test_find_id
authors = Author.scoped
@@ -673,10 +685,8 @@ class RelationTest < ActiveRecord::TestCase
end
def test_relation_merging_with_preload
- ActiveRecord::IdentityMap.without do
- [Post.scoped.merge(Post.preload(:author)), Post.preload(:author).merge(Post.scoped)].each do |posts|
- assert_queries(2) { assert posts.first.author }
- end
+ [Post.scoped.merge(Post.preload(:author)), Post.preload(:author).merge(Post.scoped)].each do |posts|
+ assert_queries(2) { assert posts.first.author }
end
end
@@ -1224,4 +1234,26 @@ class RelationTest < ActiveRecord::TestCase
scope = Post.order('foo(comments.body)')
assert_equal [], scope.references_values
end
+
+ def test_presence
+ topics = Topic.scoped
+
+ # the fist query is triggered because there are no topics yet.
+ assert_queries(1) { assert topics.present? }
+
+ # checking if there are topics is used before you actually display them,
+ # thus it shouldn't invoke an extra count query.
+ assert_no_queries { assert topics.present? }
+ assert_no_queries { assert !topics.blank? }
+
+ # shows count of topics and loops after loading the query should not trigger extra queries either.
+ assert_no_queries { topics.size }
+ assert_no_queries { topics.length }
+ assert_no_queries { topics.each }
+
+ # count always trigger the COUNT query.
+ assert_queries(1) { topics.count }
+
+ assert topics.loaded?
+ end
end
diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb
index 3314013cd4..15ceaa1fcc 100644
--- a/activerecord/test/cases/schema_dumper_test.rb
+++ b/activerecord/test/cases/schema_dumper_test.rb
@@ -239,7 +239,7 @@ class SchemaDumperTest < ActiveRecord::TestCase
def test_schema_dump_includes_hstores_shorthand_definition
output = standard_dump
if %r{create_table "postgresql_hstores"} =~ output
- assert_match %r{t.hstore "hash_store", default => ""}, output
+ assert_match %r[t.hstore "hash_store", :default => {}], output
end
end
diff --git a/activerecord/test/cases/validations/uniqueness_validation_test.rb b/activerecord/test/cases/validations/uniqueness_validation_test.rb
index 79442d68b0..376c0000c7 100644
--- a/activerecord/test/cases/validations/uniqueness_validation_test.rb
+++ b/activerecord/test/cases/validations/uniqueness_validation_test.rb
@@ -325,4 +325,16 @@ class UniquenessValidationTest < ActiveRecord::TestCase
assert w6.errors[:city].any?, "Should have errors for city"
assert_equal ["has already been taken"], w6.errors[:city], "Should have uniqueness message for city"
end
+
+ def test_validate_uniqueness_with_conditions
+ Topic.validates_uniqueness_of(:title, :conditions => Topic.where('approved = ?', true))
+ t1 = Topic.create("title" => "I'm a topic", "approved" => true)
+ t2 = Topic.create("title" => "I'm an unapproved topic", "approved" => false)
+
+ t3 = Topic.new("title" => "I'm a topic", "approved" => true)
+ assert !t3.valid?, "t3 shouldn't be valid"
+
+ t4 = Topic.new("title" => "I'm an unapproved topic", "approved" => false)
+ assert t4.valid?, "t4 should be valid"
+ end
end
diff --git a/activerecord/test/support/connection.rb b/activerecord/test/support/connection.rb
index 11154c3797..c176316a05 100644
--- a/activerecord/test/support/connection.rb
+++ b/activerecord/test/support/connection.rb
@@ -12,7 +12,7 @@ module ARTest
end
def self.connect
- puts "Using #{connection_name} with Identity Map #{ActiveRecord::IdentityMap.enabled? ? 'on' : 'off'}"
+ puts "Using #{connection_name}"
ActiveRecord::Model.logger = ActiveSupport::Logger.new("debug.log")
ActiveRecord::Model.configurations = connection_config
ActiveRecord::Model.establish_connection 'arunit'