diff options
Diffstat (limited to 'activerecord')
35 files changed, 560 insertions, 136 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 130d0f05d2..0a2e7a127c 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,8 +1,74 @@ ## Rails 4.0.0 (unreleased) ## +* Allow `Relation#where` with no arguments to be chained with new `not` query method. + + Example: + + Developer.where.not(name: 'Aaron') + + *Akira Matsuda* + +* Unscope `update_column(s)` query to ignore default scope. + + When applying `default_scope` to a class with a where clause, using + `update_column(s)` could generate a query that would not properly update + the record due to the where clause from the `default_scope` being applied + to the update query. + + class User < ActiveRecord::Base + default_scope where(active: true) + end + + user = User.first + user.active = false + user.save! + + user.update_column(:active, true) # => false + + In this situation we want to skip the default_scope clause and just + update the record based on the primary key. With this change: + + user.update_column(:active, true) # => true + + Fixes #8436. + + *Carlos Antonio da Silva* + +* SQLite adapter no longer corrupts binary data if the data contains `%00`. + + *Chris Feist* + +* Add migration history to `schema.rb` dump. Loading `schema.rb` with full migration + history restores the exact list of migrations that created that schema (including names + and fingerprints). This avoids possible mistakes caused by assuming all migrations with + a lower version have been run when loading `schema.rb`. Old `schema.rb` files without + migration history but with the `:version` setting still work as before. + + *Josh Susser* + +* Add metadata columns to `schema_migrations` table. New columns are: + + * `migrated_at`: timestamp + * `fingerprint`: md5 hash of migration source + * `name`: filename minus version and extension + + *Josh Susser* + +* Fix performance problem with `primary_key` method in PostgreSQL adapter when having many schemas. + Uses `pg_constraint` table instead of `pg_depend` table which has many records in general. + Fix #8414 + + *kennyj* + +* Do not instantiate intermediate Active Record objects when eager loading. + These records caused `after_find` to run more than expected. + Fix #3313 + + *Yves Senn* + * Add STI support to init and building associations. - Allows you to do BaseClass.new(:type => "SubClass") as well as - parent.children.build(:type => "SubClass") or parent.build_child + Allows you to do `BaseClass.new(:type => "SubClass")` as well as + `parent.children.build(:type => "SubClass")` or `parent.build_child` to initialize an STI subclass. Ensures that the class name is a valid class and that it is in the ancestors of the super class that the association is expecting. @@ -776,7 +842,7 @@ end person.pets.delete("1") # => [#<Pet id: 1>] - person.pets.delete(2, 3) # => [#<Pet id: 2>, #<Pet id: 3>] + person.pets.delete(2, 3) # => [#<Pet id: 2>, #<Pet id: 3>] *Francesco Rodriguez* @@ -1047,11 +1113,11 @@ Note that you do not need to explicitly specify references in the following cases, as they can be automatically inferred: - Post.where(comments: { name: 'foo' }) - Post.where('comments.name' => 'foo') - Post.order('comments.name') + Post.includes(:comments).where(comments: { name: 'foo' }) + Post.includes(:comments).where('comments.name' => 'foo') + Post.includes(:comments).order('comments.name') - You also do not need to worry about this unless you are doing eager + You do not need to worry about this unless you are doing eager loading. Basically, don't worry unless you see a deprecation warning or (in future releases) an SQL error due to a missing JOIN. diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb index 7f9628499c..7c43e37cf2 100644 --- a/activerecord/lib/active_record/associations/collection_proxy.rb +++ b/activerecord/lib/active_record/associations/collection_proxy.rb @@ -101,7 +101,7 @@ module ActiveRecord # # #<Pet id: 3, name: "Choo-Choo", person_id: 1> # # ] # - # person.pets.select(:name) { |pet| pet.name =~ /oo/ } + # person.pets.select(:name) { |pet| pet.name =~ /oo/ } # # => [ # # #<Pet id: 2, name: "Spook">, # # #<Pet id: 3, name: "Choo-Choo"> @@ -824,7 +824,7 @@ module ActiveRecord # # person.pets # => [#<Pet id: 20, name: "Snoop">] # - # person.pets.include?(Pet.find(20)) # => true + # person.pets.include?(Pet.find(20)) # => true # person.pets.include?(Pet.find(21)) # => false def include?(record) @association.include?(record) @@ -971,7 +971,7 @@ module ActiveRecord # person.pets.reload # fetches pets from the database # # => [#<Pet id: 1, name: "Snoop", group: "dogs", person_id: 1>] # - # person.pets(true) # fetches pets from the database + # person.pets(true) # fetches pets from the database # # => [#<Pet id: 1, name: "Snoop", group: "dogs", person_id: 1>] def reload proxy_association.reload diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb index 907fe70522..704998301c 100644 --- a/activerecord/lib/active_record/autosave_association.rb +++ b/activerecord/lib/active_record/autosave_association.rb @@ -32,8 +32,6 @@ module ActiveRecord # autosave callbacks are executed. Placing your callbacks after # associations is usually a good practice. # - # == Examples - # # === One-to-one Example # # class Post diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb index 7ec6abbc45..73012834c9 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb @@ -190,16 +190,16 @@ module ActiveRecord # # What can be written like this with the regular calls to column: # - # create_table "products", force: true do |t| - # t.column "shop_id", :integer - # t.column "creator_id", :integer - # t.column "name", :string, default: "Untitled" - # t.column "value", :string, default: "Untitled" - # t.column "created_at", :datetime - # t.column "updated_at", :datetime + # create_table :products do |t| + # t.column :shop_id, :integer + # t.column :creator_id, :integer + # t.column :name, :string, default: "Untitled" + # t.column :value, :string, default: "Untitled" + # t.column :created_at, :datetime + # t.column :updated_at, :datetime # end # - # Can also be written as follows using the short-hand: + # can also be written as follows using the short-hand: # # create_table :products do |t| # t.integer :shop_id, :creator_id diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index f1e42dfbbe..1586e84a25 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -490,8 +490,8 @@ module ActiveRecord sm_table = ActiveRecord::Migrator.schema_migrations_table_name ActiveRecord::SchemaMigration.order('version').map { |sm| - "INSERT INTO #{sm_table} (version) VALUES ('#{sm.version}');" - }.join "\n\n" + "INSERT INTO #{sm_table} (version, migrated_at, fingerprint, name) VALUES ('#{sm.version}',CURRENT_TIMESTAMP,'#{sm.fingerprint}','#{sm.name}');" + }.join("\n\n") end # Should not be called normally, but this operation is non-destructive. @@ -512,7 +512,7 @@ module ActiveRecord end unless migrated.include?(version) - execute "INSERT INTO #{sm_table} (version) VALUES ('#{version}')" + ActiveRecord::SchemaMigration.create!(:version => version, :migrated_at => Time.now) end inserted = Set.new @@ -520,7 +520,7 @@ module ActiveRecord if inserted.include?(v) raise "Duplicate migration #{v}. Please renumber your migrations to resolve the conflict." elsif v < version - execute "INSERT INTO #{sm_table} (version) VALUES ('#{v}')" + ActiveRecord::SchemaMigration.create!(:version => v, :migrated_at => Time.now) inserted << v end end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb index 7c561b6f82..18bf14d1fb 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb @@ -267,7 +267,6 @@ module ActiveRecord FROM pg_class seq, pg_attribute attr, pg_depend dep, - pg_namespace name, pg_constraint cons WHERE seq.oid = dep.objid AND seq.relkind = 'S' @@ -306,12 +305,11 @@ module ActiveRecord # Returns just a table's primary key def primary_key(table) row = exec_query(<<-end_sql, 'SCHEMA').rows.first - SELECT DISTINCT(attr.attname) + SELECT attr.attname FROM pg_attribute attr - INNER JOIN pg_depend dep ON attr.attrelid = dep.refobjid AND attr.attnum = dep.refobjsubid INNER JOIN pg_constraint cons ON attr.attrelid = cons.conrelid AND attr.attnum = cons.conkey[1] WHERE cons.contype = 'p' - AND dep.refobjid = '#{quote_table_name(table)}'::regclass + AND cons.conrelid = '#{quote_table_name(table)}'::regclass end_sql row && row.first diff --git a/activerecord/lib/active_record/counter_cache.rb b/activerecord/lib/active_record/counter_cache.rb index c53b7b3e78..81f92db271 100644 --- a/activerecord/lib/active_record/counter_cache.rb +++ b/activerecord/lib/active_record/counter_cache.rb @@ -79,16 +79,17 @@ module ActiveRecord where(primary_key => id).update_all updates.join(', ') end - # Increment a number field by one, usually representing a count. + # Increment a numeric field by one, via a direct SQL update. # - # This is used for caching aggregate values, so that they don't need to be computed every time. - # For example, a DiscussionBoard may cache post_count and comment_count otherwise every time the board is - # shown it would have to run an SQL query to find how many posts and comments there are. + # This method is used primarily for maintaining counter_cache columns used to + # store aggregate values. For example, a DiscussionBoard may cache posts_count + # and comments_count to avoid running an SQL query to calculate the number of + # posts and comments there are each time it is displayed. # # ==== Parameters # # * +counter_name+ - The name of the field that should be incremented. - # * +id+ - The id of the object that should be incremented. + # * +id+ - The id of the object that should be incremented or an Array of ids. # # ==== Examples # @@ -98,14 +99,15 @@ module ActiveRecord update_counters(id, counter_name => 1) end - # Decrement a number field by one, usually representing a count. + # Decrement a numeric field by one, via a direct SQL update. # - # This works the same as increment_counter but reduces the column value by 1 instead of increasing it. + # This works the same as increment_counter but reduces the column value by + # 1 instead of increasing it. # # ==== Parameters # # * +counter_name+ - The name of the field that should be decremented. - # * +id+ - The id of the object that should be decremented. + # * +id+ - The id of the object that should be decremented or an Array of ids. # # ==== Examples # diff --git a/activerecord/lib/active_record/integration.rb b/activerecord/lib/active_record/integration.rb index 23c272ef12..7bdc1bd4c6 100644 --- a/activerecord/lib/active_record/integration.rb +++ b/activerecord/lib/active_record/integration.rb @@ -29,8 +29,6 @@ module ActiveRecord # Returns a cache key that can be used to identify this record. # - # ==== Examples - # # Product.new.cache_key # => "products/new" # Product.find(5).cache_key # => "products/5" (updated_at not available) # Person.find(5).cache_key # => "people/5-20071224150000" (updated_at available) diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index 22347fcaef..4ce276d4bf 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -1,5 +1,6 @@ require "active_support/core_ext/class/attribute_accessors" require 'set' +require 'digest/md5' module ActiveRecord # Exception that can be raised to stop migrations from going backwards. @@ -554,6 +555,10 @@ module ActiveRecord delegate :migrate, :announce, :write, :to => :migration + def fingerprint + @fingerprint ||= Digest::MD5.hexdigest(File.read(filename)) + end + private def migration @@ -724,7 +729,7 @@ module ActiveRecord raise UnknownMigrationVersionError.new(@target_version) if target.nil? unless (up? && migrated.include?(target.version.to_i)) || (down? && !migrated.include?(target.version.to_i)) target.migrate(@direction) - record_version_state_after_migrating(target.version) + record_version_state_after_migrating(target) end end @@ -747,7 +752,7 @@ module ActiveRecord begin ddl_transaction do migration.migrate(@direction) - record_version_state_after_migrating(migration.version) + record_version_state_after_migrating(migration) end rescue => e canceled_msg = Base.connection.supports_ddl_transactions? ? "this and " : "" @@ -805,13 +810,18 @@ module ActiveRecord raise DuplicateMigrationVersionError.new(version) if version end - def record_version_state_after_migrating(version) + def record_version_state_after_migrating(target) if down? - migrated.delete(version) - ActiveRecord::SchemaMigration.where(:version => version.to_s).delete_all + migrated.delete(target.version) + ActiveRecord::SchemaMigration.where(:version => target.version.to_s).delete_all else - migrated << version - ActiveRecord::SchemaMigration.create!(:version => version.to_s) + migrated << target.version + ActiveRecord::SchemaMigration.create!( + :version => target.version.to_s, + :migrated_at => Time.now, + :fingerprint => target.fingerprint, + :name => File.basename(target.filename,'.rb').gsub(/^\d+_/,'') + ) end end diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index 94c109e72b..4d1a9c94b7 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -259,7 +259,7 @@ module ActiveRecord verify_readonly_attribute(key.to_s) end - updated_count = self.class.where(self.class.primary_key => id).update_all(attributes) + updated_count = self.class.unscoped.where(self.class.primary_key => id).update_all(attributes) attributes.each do |k, v| raw_write_attribute(k, v) diff --git a/activerecord/lib/active_record/query_cache.rb b/activerecord/lib/active_record/query_cache.rb index 38e18b32a4..df8654e5c1 100644 --- a/activerecord/lib/active_record/query_cache.rb +++ b/activerecord/lib/active_record/query_cache.rb @@ -4,6 +4,7 @@ module ActiveRecord class QueryCache module ClassMethods # Enable the query cache within the block if Active Record is configured. + # If it's not, it will execute the given block. def cache(&block) if ActiveRecord::Base.connected? connection.cache(&block) @@ -13,6 +14,7 @@ module ActiveRecord end # Disable the query cache within the block if Active Record is configured. + # If it's not, it will execute the given block. def uncached(&block) if ActiveRecord::Base.connected? connection.uncached(&block) diff --git a/activerecord/lib/active_record/querying.rb b/activerecord/lib/active_record/querying.rb index 45f6a78428..5ddcaee6be 100644 --- a/activerecord/lib/active_record/querying.rb +++ b/activerecord/lib/active_record/querying.rb @@ -26,14 +26,13 @@ module ActiveRecord # MySQL specific terms will lock you to using that particular database engine or require you to # change your call if you switch engines. # - # ==== Examples # # A simple SQL query spanning multiple tables # Post.find_by_sql "SELECT p.title, c.author FROM posts p, comments c WHERE p.id = c.post_id" - # > [#<Post:0x36bff9c @attributes={"title"=>"Ruby Meetup", "first_name"=>"Quentin"}>, ...] + # # => [#<Post:0x36bff9c @attributes={"title"=>"Ruby Meetup", "first_name"=>"Quentin"}>, ...] # # # You can use the same string replacement techniques as you can with ActiveRecord#find # Post.find_by_sql ["SELECT title FROM posts WHERE author = ? AND created > ?", author_id, start_date] - # > [#<Post:0x36bff9c @attributes={"title"=>"The Cheap Man Buys Twice"}>, ...] + # # => [#<Post:0x36bff9c @attributes={"title"=>"The Cheap Man Buys Twice"}>, ...] def find_by_sql(sql, binds = []) logging_query_plan do result_set = connection.select_all(sanitize_sql(sql), "#{name} Load", binds) @@ -57,8 +56,6 @@ module ActiveRecord # # * +sql+ - An SQL statement which should return a count query from the database, see the example below. # - # ==== Examples - # # Product.count_by_sql "SELECT COUNT(*) FROM sales s, customers c WHERE s.customer_id = c.id" def count_by_sql(sql) logging_query_plan do diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index 99e77e007a..ccc14dddeb 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -76,18 +76,17 @@ module ActiveRecord # # values = Person.group('last_name').maximum(:age) # puts values["Drake"] - # => 43 + # # => 43 # # drake = Family.find_by_last_name('Drake') # values = Person.group(:family).maximum(:age) # Person belongs_to :family # puts values[drake] - # => 43 + # # => 43 # # values.each do |family, max_age| # ... # end # - # Examples: # Person.calculate(:count, :all) # The same as Person.count # Person.average(:age) # SELECT AVG(age) FROM people... # @@ -124,8 +123,6 @@ module ActiveRecord # the plucked column names, if they can be deduced. Plucking an SQL fragment # returns String values by default. # - # Examples: - # # Person.pluck(:id) # # SELECT people.id FROM people # # => [1, 2, 3] @@ -182,8 +179,6 @@ module ActiveRecord # Pluck all the ID's for the relation using the table's primary key # - # Examples: - # # Person.ids # SELECT people.id FROM people # Person.joins(:companies).ids # SELECT people.id FROM people INNER JOIN companies ON companies.person_id = people.id def ids diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb index eafe4a54c4..7ddaea1bb0 100644 --- a/activerecord/lib/active_record/relation/finder_methods.rb +++ b/activerecord/lib/active_record/relation/finder_methods.rb @@ -225,9 +225,11 @@ module ActiveRecord orders = relation.order_values.map { |val| val.presence }.compact values = @klass.connection.distinct("#{quoted_table_name}.#{primary_key}", orders) - relation = relation.dup + relation = relation.dup.select(values) + + id_rows = @klass.connection.select_all(relation.arel, 'SQL', relation.bind_values) + ids_array = id_rows.map {|row| row[primary_key]} - ids_array = relation.select(values).collect {|row| row[primary_key]} ids_array.empty? ? raise(ThrowResult) : table[primary_key].in(ids_array) end diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index a480ddec9e..f6d106f304 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -4,6 +4,51 @@ module ActiveRecord module QueryMethods extend ActiveSupport::Concern + # WhereChain objects act as placeholder for queries in which #where does not have any parameter. + # In this case, #where must be chained with either #not, #like, or #not_like to return a new relation. + class WhereChain + def initialize(scope) + @scope = scope + end + + # Returns a new relation expressing WHERE + NOT condition + # according to the conditions in the arguments. + # + # #not accepts conditions in one of these formats: String, Array, Hash. + # See #where for more details on each format. + # + # User.where.not("name = 'Jon'") + # # SELECT * FROM users WHERE name <> 'Jon' + # + # User.where.not(["name = ?", "Jon"]) + # # SELECT * FROM users WHERE name <> 'Jon' + # + # User.where.not(name: "Jon") + # # SELECT * FROM users WHERE name <> 'Jon' + # + # User.where.not(name: nil) + # # SELECT * FROM users WHERE name IS NOT NULL + # + # User.where.not(name: %w(Ko1 Nobu)) + # # SELECT * FROM users WHERE name NOT IN ('Ko1', 'Nobu') + def not(opts, *rest) + where_value = @scope.send(:build_where, opts, rest).map do |rel| + case rel + when Arel::Nodes::In + Arel::Nodes::NotIn.new(rel.left, rel.right) + when Arel::Nodes::Equality + Arel::Nodes::NotEqual.new(rel.left, rel.right) + when String + Arel::Nodes::Not.new(Arel::Nodes::SqlLiteral.new(rel)) + else + Arel::Nodes::Not.new(rel) + end + end + @scope.where_values += where_value + @scope + end + end + Relation::MULTI_VALUE_METHODS.each do |name| class_eval <<-CODE, __FILE__, __LINE__ + 1 def #{name}_values # def select_values @@ -370,18 +415,47 @@ module ActiveRecord # User.joins(:posts).where({ "posts.published" => true }) # User.joins(:posts).where({ posts: { published: true } }) # - # === empty condition + # === no argument or :chain + # + # If no argument or :chain is passed, #where returns a new instance of WhereChain which, when + # chained with either #not, #like, or #not_like, returns a new relation. + # + # User.where.not(name: "Jon") + # # SELECT * FROM users WHERE name <> 'Jon' + # + # Book.where.like(title: "Rails%") + # # SELECT * FROM books WHERE title LIKE 'Rails%' + # + # Conference.where.not_like(name: "%Kaigi") + # # SELECT * FROM conferences WHERE name NOT LIKE '%Kaigi' # - # If the condition returns true for blank?, then where is a no-op and returns the current relation. - def where(opts, *rest) - opts.blank? ? self : spawn.where!(opts, *rest) + # See WhereChain for more details on #not, #like, and #not_like. + # + # === blank condition + # + # If the condition is any blank-ish object, then #where is a no-op and returns + # the current relation. + def where(opts = :chain, *rest) + if opts == :chain + WhereChain.new(spawn) + elsif opts.blank? + self + else + spawn.where!(opts, *rest) + end end - def where!(opts, *rest) # :nodoc: - references!(PredicateBuilder.references(opts)) if Hash === opts + # #where! is identical to #where, except that instead of returning a new relation, it adds + # the condition to the existing relation. + def where!(opts = :chain, *rest) # :nodoc: + if opts == :chain + WhereChain.new(self) + else + references!(PredicateBuilder.references(opts)) if Hash === opts - self.where_values += build_where(opts, rest) - self + self.where_values += build_where(opts, rest) + self + end end # Allows to specify a HAVING clause. Note that you can't use HAVING diff --git a/activerecord/lib/active_record/relation/spawn_methods.rb b/activerecord/lib/active_record/relation/spawn_methods.rb index 352dee3826..d417e82548 100644 --- a/activerecord/lib/active_record/relation/spawn_methods.rb +++ b/activerecord/lib/active_record/relation/spawn_methods.rb @@ -12,9 +12,6 @@ module ActiveRecord # Merges in the conditions from <tt>other</tt>, if <tt>other</tt> is an <tt>ActiveRecord::Relation</tt>. # Returns an array representing the intersection of the resulting records with <tt>other</tt>, if <tt>other</tt> is an array. - # - # ==== Examples - # # Post.where(published: true).joins(:comments).merge( Comment.where(spam: false) ) # # Performs a single join query with both where conditions. # @@ -29,7 +26,6 @@ module ActiveRecord # # => Post.where(published: true).joins(:comments) # # This is mainly intended for sharing common conditions between multiple associations. - # def merge(other) if other.is_a?(Array) to_a & other @@ -51,11 +47,8 @@ module ActiveRecord # Removes from the query the condition(s) specified in +skips+. # - # Example: - # # Post.order('id asc').except(:order) # discards the order condition # Post.where('id > 10').order('id asc').except(:where) # discards the where condition but keeps the order - # def except(*skips) result = Relation.new(klass, table, values.except(*skips)) result.default_scoped = default_scoped @@ -65,11 +58,8 @@ module ActiveRecord # Removes any condition from the query other than the one(s) specified in +onlies+. # - # Example: - # # Post.order('id asc').only(:where) # discards the order condition # Post.order('id asc').only(:where, :order) # uses the specified order - # def only(*onlies) result = Relation.new(klass, table, values.slice(*onlies)) result.default_scoped = default_scoped diff --git a/activerecord/lib/active_record/schema.rb b/activerecord/lib/active_record/schema.rb index eaa4aa7086..44b7eb424b 100644 --- a/activerecord/lib/active_record/schema.rb +++ b/activerecord/lib/active_record/schema.rb @@ -29,32 +29,55 @@ module ActiveRecord # ActiveRecord::Schema is only supported by database adapters that also # support migrations, the two features being very similar. class Schema < Migration + + # Returns the migrations paths. + # + # ActiveRecord::Schema.new.migrations_paths + # # => ["db/migrate"] # Rails migration path by default. def migrations_paths ActiveRecord::Migrator.migrations_paths end - def define(info, &block) + def define(info, &block) # :nodoc: + @using_deprecated_version_setting = info[:version].present? + SchemaMigration.drop_table + initialize_schema_migrations_table + instance_eval(&block) - unless info[:version].blank? - initialize_schema_migrations_table - assume_migrated_upto_version(info[:version], migrations_paths) - end + # handle files from pre-4.0 that used :version option instead of dumping migration table + assume_migrated_upto_version(info[:version], migrations_paths) if @using_deprecated_version_setting end # Eval the given block. All methods available to the current connection # adapter are available within the block, so you can easily use the # database definition DSL to build up your schema (+create_table+, # +add_index+, etc.). - # - # The +info+ hash is optional, and if given is used to define metadata - # about the current schema (currently, only the schema's version): - # - # ActiveRecord::Schema.define(version: 20380119000001) do - # ... - # end def self.define(info={}, &block) new.define(info, &block) end + + # Create schema migration history. Include migration statements in a block to this method. + # + # migrations do + # migration 20121128235959, "44f1397e3b92442ca7488a029068a5ad", "add_horn_color_to_unicorns" + # migration 20121129235959, "4a1eb3965d94406b00002b370854eae8", "add_magic_power_to_unicorns" + # end + def migrations + raise(ArgumentError, "Can't set migrations while using :version option") if @using_deprecated_version_setting + yield + end + + # Add a migration to the ActiveRecord::SchemaMigration table. + # + # The +version+ argument is an integer. + # The +fingerprint+ and +name+ arguments are required but may be empty strings. + # The migration's +migrated_at+ attribute is set to the current time, + # instead of being set explicitly as an argument to the method. + # + # migration 20121129235959, "4a1eb3965d94406b00002b370854eae8", "add_magic_power_to_unicorns" + def migration(version, fingerprint, name) + SchemaMigration.create!(version: version, migrated_at: Time.now, fingerprint: fingerprint, name: name) + end end end diff --git a/activerecord/lib/active_record/schema_dumper.rb b/activerecord/lib/active_record/schema_dumper.rb index 36bde44e7c..194a77ca16 100644 --- a/activerecord/lib/active_record/schema_dumper.rb +++ b/activerecord/lib/active_record/schema_dumper.rb @@ -24,6 +24,7 @@ module ActiveRecord def dump(stream) header(stream) + migrations(stream) tables(stream) trailer(stream) stream @@ -38,13 +39,11 @@ module ActiveRecord end def header(stream) - define_params = @version ? "version: #{@version}" : "" - if stream.respond_to?(:external_encoding) && stream.external_encoding stream.puts "# encoding: #{stream.external_encoding.name}" end - stream.puts <<HEADER + header_text = <<HEADER_RUBY # This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. @@ -57,15 +56,27 @@ module ActiveRecord # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(#{define_params}) do +ActiveRecord::Schema.define do -HEADER +HEADER_RUBY + stream.puts header_text end def trailer(stream) stream.puts "end" end + def migrations(stream) + all_migrations = ActiveRecord::SchemaMigration.all.to_a + if all_migrations.any? + stream.puts(" migrations do") + all_migrations.each do |migration| + stream.puts(migration.schema_line(" ")) + end + stream.puts(" end\n\n") + end + end + def tables(stream) @connection.tables.sort.each do |tbl| next if ['schema_migrations', ignore_tables].flatten.any? do |ignored| diff --git a/activerecord/lib/active_record/schema_migration.rb b/activerecord/lib/active_record/schema_migration.rb index 9830abe7d8..6c3cd5b6ba 100644 --- a/activerecord/lib/active_record/schema_migration.rb +++ b/activerecord/lib/active_record/schema_migration.rb @@ -14,17 +14,38 @@ module ActiveRecord end def self.create_table - unless connection.table_exists?(table_name) + if connection.table_exists?(table_name) + cols = connection.columns(table_name).collect { |col| col.name } + unless cols.include?("migrated_at") + connection.add_column(table_name, "migrated_at", :datetime) + q_table_name = connection.quote_table_name(table_name) + q_timestamp = connection.quoted_date(Time.now) + connection.update("UPDATE #{q_table_name} SET migrated_at = '#{q_timestamp}' WHERE migrated_at IS NULL") + connection.change_column(table_name, "migrated_at", :datetime, :null => false) + end + unless cols.include?("fingerprint") + connection.add_column(table_name, "fingerprint", :string, :limit => 32) + end + unless cols.include?("name") + connection.add_column(table_name, "name", :string) + end + else connection.create_table(table_name, :id => false) do |t| t.column :version, :string, :null => false + t.column :migrated_at, :datetime, :null => false + t.column :fingerprint, :string, :limit => 32 + t.column :name, :string end - connection.add_index table_name, :version, :unique => true, :name => index_name + connection.add_index(table_name, "version", :unique => true, :name => index_name) end + reset_column_information end def self.drop_table if connection.table_exists?(table_name) - connection.remove_index table_name, :name => index_name + if connection.index_exists?(table_name, "version", :unique => true, :name => index_name) + connection.remove_index(table_name, :name => index_name) + end connection.drop_table(table_name) end end @@ -32,5 +53,17 @@ module ActiveRecord def version super.to_i end + + # Construct ruby source to include in schema.rb dump for this migration. + # Pass a string of spaces as +indent+ to allow calling code to control how deeply indented the line is. + # The generated line includes the migration version, fingerprint, and name. Either fingerprint or name + # can be an empty string. + # + # Example output: + # + # migration 20121129235959, "ee4be703f9e6e2fc0f4baddebe6eb8f7", "add_magic_power_to_unicorns" + def schema_line(indent) + %Q(#{indent}migration %s, "%s", "%s") % [version, fingerprint, name] + end end end diff --git a/activerecord/lib/active_record/scoping/named.rb b/activerecord/lib/active_record/scoping/named.rb index fb5f5b5be0..8b7eda6eee 100644 --- a/activerecord/lib/active_record/scoping/named.rb +++ b/activerecord/lib/active_record/scoping/named.rb @@ -116,7 +116,7 @@ module ActiveRecord # Scopes can also be used while creating/building a record. # # class Article < ActiveRecord::Base - # scope :published, -> { where(published: true) } + # scope :published, -> { where(published: true) } # end # # Article.published.new.published # => true @@ -126,7 +126,7 @@ module ActiveRecord # on scopes. Assuming the following setup: # # class Article < ActiveRecord::Base - # scope :published, -> { where(published: true) } + # scope :published, -> { where(published: true) } # scope :featured, -> { where(featured: true) } # # def self.latest_article diff --git a/activerecord/test/cases/ar_schema_test.rb b/activerecord/test/cases/ar_schema_test.rb index b2eac0349b..bd47ba8741 100644 --- a/activerecord/test/cases/ar_schema_test.rb +++ b/activerecord/test/cases/ar_schema_test.rb @@ -46,4 +46,55 @@ if ActiveRecord::Base.connection.supports_migrations? end end + class ActiveRecordSchemaMigrationsTest < ActiveRecordSchemaTest + def setup + super + ActiveRecord::SchemaMigration.delete_all + end + + def test_migration_adds_row_to_migrations_table + schema = ActiveRecord::Schema.new + schema.migration(1001, "", "") + schema.migration(1002, "123456789012345678901234567890ab", "add_magic_power_to_unicorns") + + migrations = ActiveRecord::SchemaMigration.all.to_a + assert_equal 2, migrations.length + + assert_equal 1001, migrations[0].version + assert_match %r{^2\d\d\d-}, migrations[0].migrated_at.to_s(:db) + assert_equal "", migrations[0].fingerprint + assert_equal "", migrations[0].name + + assert_equal 1002, migrations[1].version + assert_match %r{^2\d\d\d-}, migrations[1].migrated_at.to_s(:db) + assert_equal "123456789012345678901234567890ab", migrations[1].fingerprint + assert_equal "add_magic_power_to_unicorns", migrations[1].name + end + + def test_define_clears_schema_migrations + assert_nothing_raised do + ActiveRecord::Schema.define do + migrations do + migration(123001, "", "") + end + end + ActiveRecord::Schema.define do + migrations do + migration(123001, "", "") + end + end + end + end + + def test_define_raises_if_both_version_and_explicit_migrations + assert_raise(ArgumentError) do + ActiveRecord::Schema.define(version: 123001) do + migrations do + migration(123001, "", "") + end + end + end + end + end + end diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 79612c8dee..94437380a4 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -944,6 +944,12 @@ class EagerAssociationTest < ActiveRecord::TestCase assert_equal 3, Developer.all.merge!(:includes => 'projects', :where => { 'developers_projects.access_level' => 1 }, :limit => 5).to_a.size end + def test_dont_create_temporary_active_record_instances + Developer.instance_count = 0 + developers = Developer.all.merge!(:includes => 'projects', :where => { 'developers_projects.access_level' => 1 }, :limit => 5).to_a + assert_equal developers.count, Developer.instance_count + end + def test_order_on_join_table_with_include_and_limit assert_equal 5, Developer.all.merge!(:includes => 'projects', :order => 'developers_projects.joined_on DESC', :limit => 5).to_a.size end diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index d25aca760f..7e6c7d5862 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -298,12 +298,6 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal 2, Firm.order(:id).find{|f| f.id > 0}.clients.length end - def test_find_with_blank_conditions - [[], {}, nil, ""].each do |blank| - assert_equal 2, Firm.all.merge!(:order => "id").first.clients.where(blank).to_a.size - end - end - def test_find_many_with_merged_options assert_equal 1, companies(:first_firm).limited_clients.size assert_equal 1, companies(:first_firm).limited_clients.to_a.size diff --git a/activerecord/test/cases/migration/logger_test.rb b/activerecord/test/cases/migration/logger_test.rb index ee0c20747e..c2fdc50e52 100644 --- a/activerecord/test/cases/migration/logger_test.rb +++ b/activerecord/test/cases/migration/logger_test.rb @@ -10,6 +10,8 @@ module ActiveRecord def migrate direction # do nothing end + def filename; "anon.rb"; end + def fingerprint; "123456789012345678901234567890ab"; end end def setup diff --git a/activerecord/test/cases/migration/table_and_index_test.rb b/activerecord/test/cases/migration/table_and_index_test.rb deleted file mode 100644 index 8fd770abd1..0000000000 --- a/activerecord/test/cases/migration/table_and_index_test.rb +++ /dev/null @@ -1,24 +0,0 @@ -require "cases/helper" - -module ActiveRecord - class Migration - class TableAndIndexTest < ActiveRecord::TestCase - def test_add_schema_info_respects_prefix_and_suffix - conn = ActiveRecord::Base.connection - - conn.drop_table(ActiveRecord::Migrator.schema_migrations_table_name) if conn.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name) - # Use shorter prefix and suffix as in Oracle database identifier cannot be larger than 30 characters - ActiveRecord::Base.table_name_prefix = 'p_' - ActiveRecord::Base.table_name_suffix = '_s' - conn.drop_table(ActiveRecord::Migrator.schema_migrations_table_name) if conn.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name) - - conn.initialize_schema_migrations_table - - assert_equal "p_unique_schema_migrations_s", conn.indexes(ActiveRecord::Migrator.schema_migrations_table_name)[0][:name] - ensure - ActiveRecord::Base.table_name_prefix = "" - ActiveRecord::Base.table_name_suffix = "" - end - end - end -end diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index c155f29973..3a861d887f 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -59,12 +59,21 @@ class MigrationTest < ActiveRecord::TestCase def test_migrator_versions migrations_path = MIGRATIONS_ROOT + "/valid" ActiveRecord::Migrator.migrations_paths = migrations_path + m0_path = File.join(migrations_path, "1_valid_people_have_last_names.rb") + m0_fingerprint = Digest::MD5.hexdigest(File.read(m0_path)) ActiveRecord::Migrator.up(migrations_path) assert_equal 3, ActiveRecord::Migrator.current_version assert_equal 3, ActiveRecord::Migrator.last_version assert_equal false, ActiveRecord::Migrator.needs_migration? + rows = connection.select_all("SELECT * FROM #{connection.quote_table_name(ActiveRecord::Migrator.schema_migrations_table_name)}") + assert_equal m0_fingerprint, rows[0]["fingerprint"] + assert_equal "valid_people_have_last_names", rows[0]["name"] + rows.each do |row| + assert_match(/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/, row["migrated_at"].to_s, "missing migrated_at") # sometimes a String, sometimes a Time + end + ActiveRecord::Migrator.down(MIGRATIONS_ROOT + "/valid") assert_equal 0, ActiveRecord::Migrator.current_version assert_equal 3, ActiveRecord::Migrator.last_version @@ -337,7 +346,7 @@ class MigrationTest < ActiveRecord::TestCase assert_nothing_raised { Person.connection.create_table :binary_testings do |t| - t.column "data", :binary, :null => false + t.column :data, :binary, :null => false end } diff --git a/activerecord/test/cases/migrator_test.rb b/activerecord/test/cases/migrator_test.rb index 1e16addcf3..0f0384382f 100644 --- a/activerecord/test/cases/migrator_test.rb +++ b/activerecord/test/cases/migrator_test.rb @@ -18,6 +18,9 @@ module ActiveRecord def up; @went_up = true; end def down; @went_down = true; end + # also used in place of a MigrationProxy + def filename; "anon.rb"; end + def fingerprint; "123456789012345678901234567890ab"; end end def setup @@ -102,7 +105,7 @@ module ActiveRecord end def test_finds_pending_migrations - ActiveRecord::SchemaMigration.create!(:version => '1') + ActiveRecord::SchemaMigration.create!(:version => '1', :name => "anon", :migrated_at => Time.now) migration_list = [ Migration.new('foo', 1), Migration.new('bar', 3) ] migrations = ActiveRecord::Migrator.new(:up, migration_list).pending_migrations @@ -152,7 +155,7 @@ module ActiveRecord end def test_current_version - ActiveRecord::SchemaMigration.create!(:version => '1000') + ActiveRecord::SchemaMigration.create!(:version => '1000', :name => "anon", :migrated_at => Time.now) assert_equal 1000, ActiveRecord::Migrator.current_version end @@ -320,7 +323,7 @@ module ActiveRecord def test_only_loads_pending_migrations # migrate up to 1 - ActiveRecord::SchemaMigration.create!(:version => '1') + ActiveRecord::SchemaMigration.create!(:version => '1', :name => "anon", :migrated_at => Time.now) calls, migrator = migrator_class(3) migrator.migrate("valid", nil) diff --git a/activerecord/test/cases/persistence_test.rb b/activerecord/test/cases/persistence_test.rb index 02034c87b4..9e0423ab52 100644 --- a/activerecord/test/cases/persistence_test.rb +++ b/activerecord/test/cases/persistence_test.rb @@ -512,6 +512,14 @@ class PersistencesTest < ActiveRecord::TestCase assert_equal 'super_title', t.title end + def test_update_column_with_default_scope + developer = DeveloperCalledDavid.first + developer.name = 'John' + developer.save! + + assert developer.update_column(:name, 'Will'), 'did not update record due to default scope' + end + def test_update_columns topic = Topic.find(1) topic.update_columns({ "approved" => true, title: "Sebastian Topic" }) @@ -616,6 +624,14 @@ class PersistencesTest < ActiveRecord::TestCase assert_equal true, topic.update_columns(title: "New title") end + def test_update_columns_with_default_scope + developer = DeveloperCalledDavid.first + developer.name = 'John' + developer.save! + + assert developer.update_columns(name: 'Will'), 'did not update record due to default scope' + end + def test_update_attributes topic = Topic.find(1) assert !topic.approved? diff --git a/activerecord/test/cases/relation/where_chain_test.rb b/activerecord/test/cases/relation/where_chain_test.rb new file mode 100644 index 0000000000..8ce44636b4 --- /dev/null +++ b/activerecord/test/cases/relation/where_chain_test.rb @@ -0,0 +1,75 @@ +require 'cases/helper' +require 'models/post' +require 'models/comment' + +module ActiveRecord + class WhereChainTest < ActiveRecord::TestCase + fixtures :posts + + def test_not_eq + expected = Arel::Nodes::NotEqual.new(Post.arel_table[:title], 'hello') + relation = Post.where.not(title: 'hello') + assert_equal([expected], relation.where_values) + end + + def test_not_null + expected = Arel::Nodes::NotEqual.new(Post.arel_table[:title], nil) + relation = Post.where.not(title: nil) + assert_equal([expected], relation.where_values) + end + + def test_not_in + expected = Arel::Nodes::NotIn.new(Post.arel_table[:title], %w[hello goodbye]) + relation = Post.where.not(title: %w[hello goodbye]) + assert_equal([expected], relation.where_values) + end + + def test_association_not_eq + expected = Arel::Nodes::NotEqual.new(Comment.arel_table[:title], 'hello') + relation = Post.joins(:comments).where.not(comments: {title: 'hello'}) + assert_equal(expected.to_sql, relation.where_values.first.to_sql) + end + + def test_not_eq_with_preceding_where + relation = Post.where(title: 'hello').where.not(title: 'world') + + expected = Arel::Nodes::Equality.new(Post.arel_table[:title], 'hello') + assert_equal(expected, relation.where_values.first) + + expected = Arel::Nodes::NotEqual.new(Post.arel_table[:title], 'world') + assert_equal(expected, relation.where_values.last) + end + + def test_not_eq_with_succeeding_where + relation = Post.where.not(title: 'hello').where(title: 'world') + + expected = Arel::Nodes::NotEqual.new(Post.arel_table[:title], 'hello') + assert_equal(expected, relation.where_values.first) + + expected = Arel::Nodes::Equality.new(Post.arel_table[:title], 'world') + assert_equal(expected, relation.where_values.last) + end + + def test_not_eq_with_string_parameter + expected = Arel::Nodes::Not.new("title = 'hello'") + relation = Post.where.not("title = 'hello'") + assert_equal([expected], relation.where_values) + end + + def test_not_eq_with_array_parameter + expected = Arel::Nodes::Not.new("title = 'hello'") + relation = Post.where.not(['title = ?', 'hello']) + assert_equal([expected], relation.where_values) + end + + def test_chaining_multiple + relation = Post.where.not(author_id: [1, 2]).where.not(title: 'ruby on rails') + + expected = Arel::Nodes::NotIn.new(Post.arel_table[:author_id], [1, 2]) + assert_equal(expected, relation.where_values[0]) + + expected = Arel::Nodes::NotEqual.new(Post.arel_table[:title], 'ruby on rails') + assert_equal(expected, relation.where_values[1]) + end + end +end diff --git a/activerecord/test/cases/relation/where_test.rb b/activerecord/test/cases/relation/where_test.rb index 9c0b139dbf..297e865308 100644 --- a/activerecord/test/cases/relation/where_test.rb +++ b/activerecord/test/cases/relation/where_test.rb @@ -85,5 +85,11 @@ module ActiveRecord def test_where_with_empty_hash_and_no_foreign_key assert_equal 0, Edge.where(:sink => {}).count end + + def test_where_with_blank_conditions + [[], {}, nil, ""].each do |blank| + assert_equal 4, Edge.where(blank).order("sink_id").to_a.size + end + end end end diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index 7ff0044bd4..264846eedb 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -1,6 +1,5 @@ require "cases/helper" - class SchemaDumperTest < ActiveRecord::TestCase def setup super @@ -18,11 +17,15 @@ class SchemaDumperTest < ActiveRecord::TestCase def test_dump_schema_information_outputs_lexically_ordered_versions versions = %w{ 20100101010101 20100201010101 20100301010101 } versions.reverse.each do |v| - ActiveRecord::SchemaMigration.create!(:version => v) + ActiveRecord::SchemaMigration.create!( + :version => v, :migrated_at => Time.now, + :fingerprint => "123456789012345678901234567890ab", :name => "anon") end schema_info = ActiveRecord::Base.connection.dump_schema_information assert_match(/20100201010101.*20100301010101/m, schema_info) + target_line = %q{INSERT INTO schema_migrations (version, migrated_at, fingerprint, name) VALUES ('20100101010101',CURRENT_TIMESTAMP,'123456789012345678901234567890ab','anon');} + assert_match target_line, schema_info end def test_magic_comment @@ -36,6 +39,16 @@ class SchemaDumperTest < ActiveRecord::TestCase assert_no_match %r{create_table "schema_migrations"}, output end + def test_schema_dump_includes_migrations + ActiveRecord::SchemaMigration.delete_all + ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/always_safe") + + output = standard_dump + assert_match %r{migrations do}, output, "Missing migrations block" + assert_match %r{migration 1001, "[0-9a-f]{32}", "always_safe"}, output, "Missing migration line" + assert_match %r{migration 1002, "[0-9a-f]{32}", "still_safe"}, output, "Missing migration line" + end + def test_schema_dump_excludes_sqlite_sequence output = standard_dump assert_no_match %r{create_table "sqlite_sequence"}, output diff --git a/activerecord/test/cases/schema_migration_test.rb b/activerecord/test/cases/schema_migration_test.rb new file mode 100644 index 0000000000..882067a7d4 --- /dev/null +++ b/activerecord/test/cases/schema_migration_test.rb @@ -0,0 +1,54 @@ +require "cases/helper" + +class SchemaMigrationTest < ActiveRecord::TestCase + def sm_table_name + ActiveRecord::SchemaMigration.table_name + end + + def connection + ActiveRecord::Base.connection + end + + def test_add_schema_info_respects_prefix_and_suffix + connection.drop_table(sm_table_name) if connection.table_exists?(sm_table_name) + # Use shorter prefix and suffix as in Oracle database identifier cannot be larger than 30 characters + ActiveRecord::Base.table_name_prefix = 'p_' + ActiveRecord::Base.table_name_suffix = '_s' + connection.drop_table(sm_table_name) if connection.table_exists?(sm_table_name) + + ActiveRecord::SchemaMigration.create_table + + assert_equal "p_unique_schema_migrations_s", connection.indexes(sm_table_name)[0][:name] + ensure + ActiveRecord::Base.table_name_prefix = "" + ActiveRecord::Base.table_name_suffix = "" + end + + def test_add_metadata_columns_to_exisiting_schema_migrations + # creates the old table schema from pre-Rails4.0, so we can test adding to it below + if connection.table_exists?(sm_table_name) + connection.drop_table(sm_table_name) + end + connection.create_table(sm_table_name, :id => false) do |schema_migrations_table| + schema_migrations_table.column("version", :string, :null => false) + end + + connection.insert "INSERT INTO #{connection.quote_table_name(sm_table_name)} (version) VALUES (100)" + connection.insert "INSERT INTO #{connection.quote_table_name(sm_table_name)} (version) VALUES (200)" + + ActiveRecord::SchemaMigration.create_table + + rows = connection.select_all("SELECT * FROM #{connection.quote_table_name(sm_table_name)}") + assert rows[0].has_key?("migrated_at"), "missing column `migrated_at`" + assert_match(/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/, rows[0]["migrated_at"].to_s) # sometimes a String, sometimes a Time + assert rows[0].has_key?("fingerprint"), "missing column `fingerprint`" + assert rows[0].has_key?("name"), "missing column `name`" + end + + def test_schema_migrations_columns + ActiveRecord::SchemaMigration.create_table + + columns = connection.columns(sm_table_name).collect(&:name) + %w[version migrated_at fingerprint name].each { |col| assert columns.include?(col), "missing column `#{col}`" } + end +end diff --git a/activerecord/test/migrations/always_safe/1001_always_safe.rb b/activerecord/test/migrations/always_safe/1001_always_safe.rb new file mode 100644 index 0000000000..454b972507 --- /dev/null +++ b/activerecord/test/migrations/always_safe/1001_always_safe.rb @@ -0,0 +1,5 @@ +class AlwaysSafe < ActiveRecord::Migration + def change + # do nothing to avoid side-effect conflicts from running multiple times + end +end diff --git a/activerecord/test/migrations/always_safe/1002_still_safe.rb b/activerecord/test/migrations/always_safe/1002_still_safe.rb new file mode 100644 index 0000000000..7398ae27a2 --- /dev/null +++ b/activerecord/test/migrations/always_safe/1002_still_safe.rb @@ -0,0 +1,5 @@ +class StillSafe < ActiveRecord::Migration + def change + # do nothing to avoid side-effect conflicts from running multiple times + end +end diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb index 622dd75aeb..1c048bb6b4 100644 --- a/activerecord/test/models/developer.rb +++ b/activerecord/test/models/developer.rb @@ -57,6 +57,16 @@ class Developer < ActiveRecord::Base def log=(message) audit_logs.build :message => message end + + after_find :track_instance_count + cattr_accessor :instance_count + + def track_instance_count + self.class.instance_count ||= 0 + self.class.instance_count += 1 + end + private :track_instance_count + end class AuditLog < ActiveRecord::Base |