aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
Commit message (Collapse)AuthorAgeFilesLines
...
* | | | Place class level `update`, `destroy`, and `delete` in ↵Ryuta Kamizono2017-09-183-95/+93
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `Persistence::ClassMethods` The docs are obviously for class level `update`, `destroy`, and `delete`. It should be placed in `Persistence::ClassMethods` rather than `Relation`. And also, these methods are not dependent on relation. So it is not needed to delegate to `all` (plus, `klass.find` is faster than `relation.find`).
* | | | Early return if `records.empty?` in `Preloader#preload`Ryuta Kamizono2017-09-181-3/+3
| | | |
* | | | Don't pass `reflection_scope` to `preload_scope` if `reflection.scope` isn't ↵Ryuta Kamizono2017-09-181-1/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | given Related 2b5f5cdd7c1d95716de6a206b6d09ccbb006dc17. If `reflection.scope` isn't given, `reflection_scope` is always empty scope. It is unnecessary to merge it.
* | | | Return `through_scope` only if the scope is not empty scopeRyuta Kamizono2017-09-181-4/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Related 2b5f5cdd7c1d95716de6a206b6d09ccbb006dc17. If `through_scope` is empty scope, it is unnecessary to merge it. And also, comparing relations is a little expensive (will cause `build_arel`). It is enough to use `empty_scope?` to determine whether empty scope.
* | | | Remove useless condition in `reset_association`Ryuta Kamizono2017-09-181-2/+1
| | | | | | | | | | | | | | | | `through_scope` is not empty scope if `options[:source_type]` is given.
* | | | Fix docs describing rollback [ci skip]dixpac2017-09-171-2/+2
| |/ / |/| | | | | | | | | | | | | | | | | * `rails db:migrate STEP = 2` will not rollback the migrations, instead `rails db:rollback STEP = 2` will do the rollback. * Also, rewritten `rails db:migrate VERSION` => `rails db:rollback VERSION` for consistency.
* | | Remove unused explicit delegation to `klass` in `relation`Ryuta Kamizono2017-09-143-8/+2
| | | | | | | | | | | | | | | | | | | | | It is only used `primary_key` and `connection` in the internal, so it is not needed to delegate others to `klass` explicitly. This doesn't change public behavior because `relation` will delegate missing method to `klass`.
* | | Don't use `collection.table_name` in `collection_cache_key`Ryuta Kamizono2017-09-142-1/+19
| | | | | | | | | | | | | | | Because `collection.table_name` doesn't respect table alias. Use `collection.arel_attribute` instead.
* | | Don't use `quoted_table_name` in `limited_ids_for`Ryuta Kamizono2017-09-142-2/+8
| | | | | | | | | | | | | | | | | | Because `quoted_table_name` doesn't respect table alias. We should use `arel_attribute` for that, so I added `column_name_from_arel_node` to generate column name from an arel node.
* | | Merge pull request #30596 from yahonda/address_test_or_with_bind_params_failureRyuta Kamizono2017-09-141-1/+1
|\ \ \ | | | | | | | | Address random `test_or_with_bind_params` failures
| * | | Address random `test_or_with_bind_params` failuresYasuo Honda2017-09-131-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Reported at https://travis-ci.org/rails/rails/jobs/274370258 - `Post.find([1, 2])` generates this query below: ```sql SELECT "posts".* FROM "posts" WHERE "posts"."id" IN ($1, $2) [["id", 1], ["id", 2]] ``` - `Post.where(id: 1).or(Post.where(id: 2)).to_a` generates this query below: ```sql SELECT "posts".* FROM "posts" WHERE ("posts"."id" = $1 OR "posts"."id" = $2) [["id", 1], ["id", 2]] ``` Most of the time these two queries return the same result but the order of records are not guaranteed from SQL point of view then added `sort` before comparing them.
* | | | `quoted_table_name` doesn't respect table aliasRyuta Kamizono2017-09-142-1/+12
|/ / / | | | | | | | | | So using `arel_attribute(primary_key).asc` in `batch_order` instead.
* | | Make `in_batches` queries to preparableRyuta Kamizono2017-09-141-3/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Before: ```sql SELECT "posts".* FROM "posts" ORDER BY "posts"."id" ASC LIMIT ? [["LIMIT", 2]] SELECT "posts".* FROM "posts" WHERE "posts"."id" > 2 ORDER BY "posts"."id" ASC LIMIT ? [["LIMIT", 2]] SELECT "posts".* FROM "posts" WHERE "posts"."id" > 4 ORDER BY "posts"."id" ASC LIMIT ? [["LIMIT", 2]] SELECT "posts".* FROM "posts" WHERE "posts"."id" > 6 ORDER BY "posts"."id" ASC LIMIT ? [["LIMIT", 2]] SELECT "posts".* FROM "posts" WHERE "posts"."id" > 8 ORDER BY "posts"."id" ASC LIMIT ? [["LIMIT", 2]] SELECT "posts".* FROM "posts" WHERE "posts"."id" > 10 ORDER BY "posts"."id" ASC LIMIT ? [["LIMIT", 2]] ``` After: ```sql SELECT "posts".* FROM "posts" ORDER BY "posts"."id" ASC LIMIT ? [["LIMIT", 2]] SELECT "posts".* FROM "posts" WHERE "posts"."id" > ? ORDER BY "posts"."id" ASC LIMIT ? [["id", 2], ["LIMIT", 2]] SELECT "posts".* FROM "posts" WHERE "posts"."id" > ? ORDER BY "posts"."id" ASC LIMIT ? [["id", 4], ["LIMIT", 2]] SELECT "posts".* FROM "posts" WHERE "posts"."id" > ? ORDER BY "posts"."id" ASC LIMIT ? [["id", 6], ["LIMIT", 2]] SELECT "posts".* FROM "posts" WHERE "posts"."id" > ? ORDER BY "posts"."id" ASC LIMIT ? [["id", 8], ["LIMIT", 2]] SELECT "posts".* FROM "posts" WHERE "posts"."id" > ? ORDER BY "posts"."id" ASC LIMIT ? [["id", 10], ["LIMIT", 2]] ```
* | | PERF: Incorrect memoization in ↵Guo Xiang Tan2017-09-111-1/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `ActiveRecord::Associations::Preloader::Association`. ``` require 'active_record' require 'benchmark/ips' ActiveRecord::Base.establish_connection(ENV.fetch('DATABASE_URL')) ActiveRecord::Migration.verbose = false ActiveRecord::Schema.define do create_table :users, force: true do |t| t.string :name, :email t.integer :topic_id t.timestamps null: false end create_table :topics, force: true do |t| t.string :title t.timestamps null: false end end attributes = { name: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', email: 'foobar@email.com' } class Topic < ActiveRecord::Base has_many :users end class User < ActiveRecord::Base belongs_to :topic end 100.times do User.create!(attributes) end users = User.first(50) 100.times do Topic.create!(title: 'This is a topic', users: users) end Benchmark.ips do |x| x.config(time: 10, warmup: 5) x.report("preload") do User.includes(:topic).all.to_a end end ``` ``` Calculating ------------------------------------- preload 25.000 i/100ms ------------------------------------------------- preload 251.772 (± 1.2%) i/s - 2.525k ``` ``` Calculating ------------------------------------- preload 26.000 i/100ms ------------------------------------------------- preload 270.392 (± 1.1%) i/s - 2.704k ```
* | | Merge pull request #30524 from tgxworld/recover_plucK_performanceSean Griffin2017-09-071-14/+17
|\ \ \ | | | | | | | | PERF: Recover `ActiveRecord::pluck` performance.
| * | | PERF: Recover `ActiveRecord::pluck` performance.Guo Xiang Tan2017-09-061-14/+17
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | ```ruby require 'active_record' require 'benchmark/ips' ActiveRecord::Base.establish_connection(ENV.fetch('DATABASE_URL')) ActiveRecord::Migration.verbose = false ActiveRecord::Schema.define do create_table :users, force: true do |t| t.string :name, :email t.timestamps null: false end end attributes = { name: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', email: 'foobar@email.com' } class User < ActiveRecord::Base; end 1000.times do User.create!(attributes) end Benchmark.ips do |x| x.config(time: 10, warmup: 2) x.report('pluck 1 column') do User.pluck(:id) end x.report('pluck 2 columns') do User.pluck(:id, :email) end x.report('pluck 1 column with scope') do User.where(id: 1000).pluck(:id) end x.report('pluck 2 columns with scope') do User.where(id: 1000).pluck(:id, :email) end end ``` ``` Calculating ------------------------------------- pluck 1 column 122.000 i/100ms pluck 2 columns 74.000 i/100ms pluck 1 column with scope 615.000 i/100ms pluck 2 columns with scope 515.000 i/100ms ------------------------------------------------- pluck 1 column 1.272k (± 3.9%) i/s - 12.810k pluck 2 columns 750.096 (± 3.3%) i/s - 7.548k pluck 1 column with scope 6.074k (± 4.1%) i/s - 60.885k pluck 2 columns with scope 5.158k (± 2.7%) i/s - 52.015k ``` ``` Calculating ------------------------------------- pluck 1 column 126.000 i/100ms pluck 2 columns 78.000 i/100ms pluck 1 column with scope 457.000 i/100ms pluck 2 columns with scope 434.000 i/100ms ------------------------------------------------- pluck 1 column 1.266k (± 2.1%) i/s - 12.726k pluck 2 columns 795.061 (± 3.0%) i/s - 7.956k pluck 1 column with scope 4.660k (± 2.1%) i/s - 46.614k pluck 2 columns with scope 4.355k (± 2.3%) i/s - 43.834k ``` ``` Calculating ------------------------------------- pluck 1 column 126.000 i/100ms pluck 2 columns 78.000 i/100ms pluck 1 column with scope 539.000 i/100ms pluck 2 columns with scope 481.000 i/100ms ------------------------------------------------- pluck 1 column 1.308k (± 3.4%) i/s - 13.104k pluck 2 columns 798.604 (± 2.8%) i/s - 8.034k pluck 1 column with scope 5.530k (± 3.4%) i/s - 55.517k pluck 2 columns with scope 4.914k (± 2.7%) i/s - 49.543k ``` ``` Calculating ------------------------------------- pluck 1 column 139.000 i/100ms pluck 2 columns 79.000 i/100ms pluck 1 column with scope 580.000 i/100ms pluck 2 columns with scope 526.000 i/100ms ------------------------------------------------- pluck 1 column 1.337k (± 3.0%) i/s - 13.483k pluck 2 columns 806.776 (± 2.7%) i/s - 8.137k pluck 1 column with scope 5.924k (± 4.1%) i/s - 59.160k pluck 2 columns with scope 5.276k (± 3.1%) i/s - 53.126k ```
* | | | Remove unnecessary `join_type` in `AssociationScope`Ryuta Kamizono2017-09-081-5/+1
| | | | | | | | | | | | | | | | | | | | | | | | This method was moved from `JoinHelper` in 0fddc3c1, but it is only used for `table.create_join` in the internal and `Nodes::InnerJoin` is default join klass. So it is not needed to pass it explicitly.
* | | | Remove duplicated `klass` method in `AssociationReflection`Ryuta Kamizono2017-09-081-16/+11
| | | | | | | | | | | | | | | | | | | | The superclass (`MacroReflection`) already have the same method definition.
* | | | Remove unused `primary_key_type` and `quoted_table_name` in `Reflection`Ryuta Kamizono2017-09-081-8/+0
| | | | | | | | | | | | | | | | | | | | `primary_key_type` is no longer used since #26718. `quoted_table_name` is no longer used since Rails 3.1.
* | | | Add an extra assertion to ensure dumping schema default as expectedRyuta Kamizono2017-09-081-1/+4
| | | |
* | | | Fix `quote_default_expression` for UUID with array defaultRyuta Kamizono2017-09-082-1/+11
| | | | | | | | | | | | | | | | Fixes #30539.
* | | | Don't pass `table` to `last_chain_scope` and `next_chain_scope`Ryuta Kamizono2017-09-072-19/+17
| | | | | | | | | | | | | | | | | | | | | | | | Because `table` is part of `reflection`, don't need to pass it explicitly. And also, naming `alias_name` to `table` is a little confusing. `aliased_table` is preferable than `alias_name`.
* | | | `RuntimeReflection` is not a subclass of `PolymorphicReflection`Ryuta Kamizono2017-09-071-26/+6
| | | | | | | | | | | | | | | | | | | | | | | | `PolymorphicReflection` is an internal class that is used in `ThroughReflection`. But `RuntimeReflection` is used for the head of chain in `AssociationScope`. These are totally different things.
* | | | `has_many :through` with unscope should affect to through scopeRyuta Kamizono2017-09-074-23/+19
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The order of scope evaluation should be from through scope to the association's own scope. Otherwise the association's scope cannot affect to through scope. Fixes #13677. Closes #28449.
* | | | Don't pass unneeded `reflection` to `add_constraints`Ryuta Kamizono2017-09-051-3/+3
| | | | | | | | | | | | | | | | Because `refl.scope` is the same meaning with `chain_head.scope`.
* | | | Assigning `values` is only necessary when `reflection_scope.where_clause` is ↵Ryuta Kamizono2017-09-051-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | not empty Because `reflection_scope.values` will create extra new hash.
* | | | Don't merge `reflection_scope` if `reflection.scope` isn't givenRyuta Kamizono2017-09-051-1/+1
| | | | | | | | | | | | | | | | | | | | If `reflection.scope` isn't given, `reflection_scope` is always `klass.unscoped`. it is unnecessary to merge it.
* | | | Should quote composite primary key namesRyuta Kamizono2017-09-042-2/+12
|/ / / | | | | | | | | | | | | | | | | | | Otherwise using reserved words as composite primary key names will be failed as an invalid SQL. Fixes #30518.
* | | Scope in associations should treat nil as `all`Ryuta Kamizono2017-09-046-9/+11
| | | | | | | | | | | | | | | | | | | | | | | | Defined scope treats nil as `all`, but scope in associations isn't so. If the result of the scope is nil, most features on associations will be broken. It should treat nil as `all` like defined scope. Fixes #20823.
* | | Don't expose `find_all_ordered` utility method in testsRyuta Kamizono2017-09-021-4/+5
| | | | | | | | | | | | Because this is not a test case.
* | | Fix preloading through association with custom scopeRyuta Kamizono2017-09-022-1/+11
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | If `reflection_scope.where_clause` is not empty, `through_scope` should be joined the source association. But if `values[:references]` in `reflection_scope` is empty, the source association will not be joined. It should use `source_reflection.table_name` in that case. Fixes #22535. Closes #28763.
* | | Add a test case for preloading through association with implicit sourceRyuta Kamizono2017-09-021-0/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | If `reflection_scope.where_clause` is not empty, `through_scope` should be joined the source association. But if the through association doesn't have explicit `:source`, `options[:source]` will be nil and `scope.includes_values` will also be empty. It should use `source_reflection.name` rather than `options[:source]`. Fixed by a26cff3c1235c61cd0135bed4ef63d7be452b458. Fixes #11078. Fixes #26129. Closes #14312. Closes #29155. Closes #29841.
* | | `values[:includes]` in `reflection_scope` is not compatible with `through_scope`Ryuta Kamizono2017-09-022-2/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Without this fix, preloading `:comments_with_include` will cause the following error: ``` % bundle exec ruby -w -Itest test/cases/associations/eager_test.rb -n test_eager_with_has_many_through_join_model_with_include Using sqlite3 Run options: -n test_eager_with_has_many_through_join_model_with_include --seed 1502 E Error: EagerAssociationTest#test_eager_with_has_many_through_join_model_with_include: ActiveRecord::AssociationNotFoundError: Association named 'post' was not found on Post; perhaps you misspelled it? ```
* | | Don't call `scope.eager_loading?` when `reflection_scope.where_clause` is emptyRyuta Kamizono2017-09-021-10/+10
| | | | | | | | | | | | | | | If `reflection_scope.where_clause` is empty, `scope` isn't changed. So `scope.eager_loading?` is always false.
* | | sqlite3 adapter returns integer value which used to be stringYasuo Honda2017-09-012-9/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `to_i` was added for SQLite3 adapter which did not handle number but sqlite3 gem already supports it then `to_i` is unnecessary. else condition is kept for adapters which return string, i.e. mysql(not mysql2) and sqlserver. Renamed `test_cache_does_not_wrap_string_results_in_arrays` to `test_cache_does_not_wrap_results_in_arrays` to explain the current behavior. most of adapters return integer, not only string. * Refer these commits: "future proofing the sqlite3 adapter code" https://github.com/rails/rails/commit/beda2d43d6ac5c3435fc2fba0cbd108c20fe1c67 "Refactor calculation test to remove unneeded SQLite special case." https://github.com/rails/rails/commit/47d568ed3fc701934ebe80b276f3d8bf6951c93f "no need to to_i, sqlite does that for us" https://github.com/rails/rails/commit/6cf44a1bd64ba10497742d70ad78fe68faa16e99
* | | Merge pull request #29850 from yahonda/test_with_mariadb_102_on_trustyRyuta Kamizono2017-09-011-0/+4
|\ \ \ | | | | | | | | CI with the latest stable(GA) version of MariaDB 10.2
| * | | Skip `test_remove_column_with_multi_column_index`Yasuo Honda2017-09-011-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | when tested with MariaDB 10.2.8 or higher Refer #30485 https://mariadb.com/kb/en/the-mariadb-library/alter-table/#drop-column-if-exists-col_name-cascaderestrict > MariaDB starting with 10.2.8 > Dropping a column that is part of a multi-column UNIQUE constraint is not permitted.
* | | | `add_reference` should respect column position for both reference id and ↵Ryuta Kamizono2017-09-012-1/+11
|/ / / | | | | | | | | | | | | | | | type columns Fixes #30496.
* | | :warning: assigned but unused variable - messageAkira Matsuda2017-09-011-6/+6
| | |
* | | Clarify intentions around method redefinitionsMatthew Draper2017-09-011-3/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | Don't use remove_method or remove_possible_method just before a new definition: at best the purpose is unclear, and at worst it creates a race condition. Instead, prefer redefine_method when practical, and silence_redefinition_of_method otherwise.
* | | Merge pull request #30377 from keepcosmos/delegate-missing-methodsMatthew Draper2017-08-312-4/+4
|\ \ \ | | | | | | | | Delegate :rindex, :slice, :rotate(missing) to 'records'
| * | | Delegate :rindex, :slice, :rotate to 'records'keepcosmos2017-08-242-4/+4
| | | |
* | | | Merge pull request #30392 from koic/unify_source_control_keep_file_nameMatthew Draper2017-08-311-0/+0
|\ \ \ \ | | | | | | | | | | Unify the internal source control .keep file name
| * | | | Unify the internal source control .keep file nameKoichi ITO2017-08-241-0/+0
| |/ / /
* | | / Fix `can't modify frozen String` error in `DatabaseTasks`yuuji.yaginuma2017-08-304-2/+37
| |_|/ |/| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Without this, `db:structure:dump` task raises an error as follwing: ``` can't modify frozen String activerecord/lib/active_record/tasks/sqlite_database_tasks.rb:77:in `run_cmd_error' activerecord/lib/active_record/tasks/sqlite_database_tasks.rb:72:in `run_cmd' activerecord/lib/active_record/tasks/sqlite_database_tasks.rb:52:in `structure_dump' activerecord/lib/active_record/tasks/database_tasks.rb:219:in `structure_dump' activerecord/lib/active_record/railties/databases.rake:279:in `block (3 levels) in <main>' railties/lib/rails/commands/rake/rake_command.rb:23:in `block in perform' railties/lib/rails/commands/rake/rake_command.rb:20:in `perform' railties/lib/rails/command.rb:48:in `invoke' railties/lib/rails/commands.rb:18:in `<main>' ```
* | | Merge pull request #30439 from prathamesh-sonpatki/changelog-30360Ryuta Kamizono2017-08-291-0/+5
|\ \ \ | | | | | | | | Added CHANGELOG for #30360 [ci skip]
| * | | Added CHANGELOG for #30360 [ci skip]Prathamesh Sonpatki2017-08-281-0/+5
| | | |
* | | | Merge pull request #30445 from prathamesh-sonpatki/fix-30441Kasper Timm Hansen2017-08-281-0/+2
|\ \ \ \ | | | | | | | | | | Clarify that bulk option is supported only by MySQL
| * | | | Clarify that bulk option is supported only by MySQL [ci skip]Prathamesh Sonpatki2017-08-291-0/+2
| |/ / / | | | | | | | | | | | | - Closes #30441
* | | | "models/reader" is no longer used in `autosave_association_test.rb`Ryuta Kamizono2017-08-281-1/+0
| | | |