aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
Commit message (Collapse)AuthorAgeFilesLines
...
* | | | | | | | | Refactor `remove_foreign_key` to delete the foreign key before `alter_table`Ryuta Kamizono2019-02-161-4/+2
|/ / / / / / / /
* | | | | | | | Merge pull request #35286 from matthewdunbar/masterRyuta Kamizono2019-02-161-0/+6
|\ \ \ \ \ \ \ \ | | | | | | | | | | | | | | | | | | | | | | | | | | | Properly handle cached queries with too many bind parameters
| * | | | | | | | Properly handle cached queries with too many bind parametersMatthew Dunbar2019-02-141-0/+6
| | | | | | | | |
* | | | | | | | | Deprecate using class level querying methods if the receiver scope regarded ↵Ryuta Kamizono2019-02-153-7/+48
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | as leaked This deprecates using class level querying methods if the receiver scope regarded as leaked, since #32380 and #35186 may cause that silently leaking information when people upgrade the app. We need deprecation first before making those.
* | | | | | | | | Revert "Merge pull request #35186 from ↵Ryuta Kamizono2019-02-152-19/+2
|/ / / / / / / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | kamipo/fix_leaking_scope_on_relation_create" This reverts commit b67d5c6dedbf033515a96a95d24d085bf99a0d07, reversing changes made to 2e018361c7c51e36d1d98bf770b7456d78dee68b. Reason: #35186 may cause that silently leaking information when people upgrade the app. We need deprecation first before making this.
* | | | | | | | Minor changes to deprecation warning message after 35242Abhay Nikam2019-02-152-4/+4
| | | | | | | |
* | | | | | | | Merge pull request #35242 from ↵Eileen M. Uchitelle2019-02-141-6/+24
|\ \ \ \ \ \ \ \ | |/ / / / / / / |/| | | | | | | | | | | | | | | | | | | | | | | eileencodes/add-setter-and-deprecation-for-configurations-hashes Add setter and deprecation for configurations hashes
| * | | | | | | Improve errors and handling of hashes for database configurationseileencodes2019-02-141-6/+24
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | In chat Sam Saffron asked how to use the setter now that configurations is no longer a hash and you need to do AR::Base.configurations["test"]=. Technically you can do `ActiveRecord::Base.configurations = { the hash }` but I realized the old way throws an error and is unintuitive. To aid in the transition from hashes to objects this PR makes a few changes: 1) Re-adds a deprecated hash setter `[]=` that will add a new hash to the configurations list OR replace an existing hash if that environment is already present. This won't be supported in future Rails versions but a good error is important. 2) Changed to throw deprecation warnings on the methods we decided to support for hash conversion and raise on the methods we don't support. 3) Refactored the setter/getter hash deprecation warnings messages and rewrote them. Getters message: ``` DEPRECATION WARNING: `ActiveRecord::Base.configurations` no longer returns a hash. Methods that act on the hash like `values` are deprecated and will be removed in Rails 6.1. Use the `configs_for` method to collect and iterate over the database configurations. ``` Setter message: ``` DEPRECATION WARNING: Setting `ActiveRecord::Base.configurations` with `[]=` is deprecated. Use `ActiveRecord::Base.configurations=` directly to set the configurations instead. ``` 4) Rewrote the legacy configurations test file to test all the public methods in the DatabaseConfigurations class.
* | | | | | | | Revert "Chaining named scope is no longer leaking to class level querying ↵Ryuta Kamizono2019-02-142-3/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | methods" This reverts #32380, since this may cause that silently leaking information when people upgrade the app. We need deprecation first before making this.
* | | | | | | | Merge pull request #35244 from palkan/fix/activerecord-database-uri-resolveEileen M. Uchitelle2019-02-131-1/+1
|\ \ \ \ \ \ \ \ | | | | | | | | | | | | | | | | | | Fix database configurations building when DATABASE_URL present
| * | | | | | | | Fix database configurations building when DATABASE_URL presentVladimir Dementyev2019-02-121-1/+1
| | | | | | | | |
* | | | | | | | | Revert "Merge pull request #35127 from bogdan/counter-cache-loading"Ryuta Kamizono2019-02-132-11/+6
|/ / / / / / / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This reverts commit eec3e28a1abf75676dcee58308ee5721bb53c325, reversing changes made to 5588fb4802328a2183f4a55c36d6703ee435f85c. Reason: Marking as loaded without actual loading is too greedy optimization. See more context #35239. Closes #35239. [Edouard CHIN & Ryuta Kamizono]
* | | | | | | | Should respect attribute_types over column_typesRyuta Kamizono2019-02-131-2/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Fixed the CI failure https://travis-ci.org/rails/rails/jobs/492291248#L1185-L1191.
* | | | | | | | Fix `pluck` and `select` with custom attributesRyuta Kamizono2019-02-132-9/+19
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Currently custom attributes are always qualified by the table name in the generated SQL wrongly even if the table doesn't have the named column, it would cause an invalid SQL error. Custom attributes should only be qualified if the table has the same named column.
* | | | | | | | Avoid implicit delegation in the migration compatibilityRyuta Kamizono2019-02-121-12/+14
|/ / / / / / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The implicit delegation in the migration class is to be logged. It is not intended in the migration compatibility, so it should be avoided. Fixes #35224.
* | | | | | | Merge pull request #35196 from gmcgibbon/clarify_collection_proxy_docsGannon McGibbon2019-02-112-11/+25
|\ \ \ \ \ \ \ | | | | | | | | | | | | | | | | Clarify collection proxy docs
| * | | | | | | Clarify collection proxy docsGannon McGibbon2019-02-082-11/+25
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The docs for `ActiveRecord::Associations::CollectionProxy` describe `ActiveRecord::Associations::Association`. I've moved them to association and rewrote collection proxy's docs to be more applicable to what the class actually does.` [ci skip]
* | | | | | | | Add `remove_foreign_key` for `change_table`Ryuta Kamizono2019-02-111-0/+11
| | | | | | | |
* | | | | | | | Fix typo a -> an, an -> a [ci skip]Ryuta Kamizono2019-02-112-2/+2
| | | | | | | |
* | | | | | | | SQLite3: Implement `add_foreign_key` and `remove_foreign_key`Ryuta Kamizono2019-02-115-6/+34
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | I implemented Foreign key create in `create_table` for SQLite3 at #24743. This follows #24743 to implement `add_foreign_key` and `remove_foreign_key`. Unfortunately SQLite3 has one limitation that `PRAGMA foreign_key_list(table-name)` doesn't have constraint name. So we couldn't implement find/remove foreign key by name for now. Fixes #35207. Closes #31343.
* | | | | | | | More exercise table name prefix and suffix testsRyuta Kamizono2019-02-114-8/+16
| | | | | | | |
* | | | | | | | Merge pull request #35203 from chiastolite/add_column_without_column_namesRyuta Kamizono2019-02-101-0/+1
|\ \ \ \ \ \ \ \ | | | | | | | | | | | | | | | | | | | | | | | | | | | Do not allow to add column without column name
| * | | | | | | | Do not allow to add column without column nameHiroyuki Morita2019-02-101-0/+1
|/ / / / / / / /
* | | | | | | | Refactor to extract defining column methods as `define_column_methods`Ryuta Kamizono2019-02-093-142/+161
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | It makes to ease to handle all short-hand methods (e.g. validates arguments etc).
* | | | | | | | Refactor to just use `Association#target=` in `associate_records_to_owner`Ryuta Kamizono2019-02-091-3/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | `Association#target=` invokes `loaded!`, so we no longer need to call the `loaded!` explicitly. Since Preloader is private API, we don't guarantee that it behaves like older version as long as using Preloader directly. But this refactoring fortunately also fix the Preloader compatibility issue #35195. Closes #35195.
* | | | | | | | Merge pull request #34618 from bogdanvlviv/fix-elapsed-time-calculationsKasper Timm Hansen2019-02-082-6/+6
|\ \ \ \ \ \ \ \ | | | | | | | | | | | | | | | | | | Fix elapsed time calculations
| * | | | | | | | Fix elapsed time calculationsbogdanvlviv2019-02-082-6/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | I've found a few places in Rails code base where I think it makes sense to calculate elapsed time more precisely by using `Concurrent.monotonic_time`: - Fix calculation of elapsed time in `ActiveSupport::Cache::MemoryStore#prune` - Fix calculation of elapsed time in `ActiveRecord::ConnectionAdapters::ConnectionPool::Queue#wait_poll` - Fix calculation of elapsed time in `ActiveRecord::ConnectionAdapters::ConnectionPool#attempt_to_checkout_all_existing_connections` - Fix calculation of elapsed time in `ActiveRecord::ConnectionAdapters::Mysql2Adapter#explain` See https://docs.ruby-lang.org/en/2.5.0/Process.html#method-c-clock_gettime https://blog.dnsimple.com/2018/03/elapsed-time-with-ruby-the-right-way Related to 7c4542146f0dde962205e5a90839349631ae60fb
* | | | | | | | | Merge pull request #35193 from kamipo/fix_exists_with_distinct_and_offsetRyuta Kamizono2019-02-081-4/+8
|\ \ \ \ \ \ \ \ \ | | | | | | | | | | | | | | | | | | | | Fix `relation.exists?` with giving both `distinct` and `offset`
| * | | | | | | | | Fix `relation.exists?` with giving both `distinct` and `offset`Ryuta Kamizono2019-02-081-4/+8
| | |_|/ / / / / / | |/| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The `distinct` affects (reduces) rows of the result, so it is important part when both `distinct` and `offset` are given. Replacing SELECT clause to `1 AS one` and removing `distinct` and `order` is just optimization for the `exists?`, we should not apply the optimization for that case. Fixes #35191.
* | | | | | | | | Merge pull request #35178 from bogdan/has-many-sizeRyuta Kamizono2019-02-081-2/+2
|\ \ \ \ \ \ \ \ \ | |/ / / / / / / / |/| | | | | | | | Bugfix has_many association #size when ids reader is cached and assoc…
| * | | | | | | | Bugfix has_many association #size when ids reader is cached and association ↵Bogdan Gusiev2019-02-081-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | is changed
* | | | | | | | | Refactor extracting `current_scope_restoring_block` into the scoping classRyuta Kamizono2019-02-082-5/+9
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Relation is not best place to do this.
* | | | | | | | | Rename database_operations config to *_contextJohn Hawthorn2019-02-071-1/+1
| |_|/ / / / / / |/| | | | | | |
* | | | | | | | Merge pull request #35182 from jhawthorn/db-selection-refactorEileen M. Uchitelle2019-02-073-19/+19
|\ \ \ \ \ \ \ \ | |_|/ / / / / / |/| | | | | | | Improve naming in DatabaseSelector
| * | | | | | | Rename database selector operations to contextJohn Hawthorn2019-02-072-18/+18
| | | | | | | |
| * | | | | | | Rename resolver ivar to operations in ResolverJohn Hawthorn2019-02-061-7/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | We're already in the resolver, we call this class "operations" in the middleware, so we should use the same naming here.
| * | | | | | | Rename Session.build to Session.callJohn Hawthorn2019-02-062-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is more consistent with Resolver, which has build called. This allows using a Proc instead of a class, which could be nice if you need to vary switching logic based on the request in a more ad-hoc way (ie. check if it is an API request).
* | | | | | | | Fix `relation.create` to avoid leaking scope to initialization block and ↵Ryuta Kamizono2019-02-071-2/+15
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | callbacks `relation.create` populates scope attributes to new record by `scoping`, it is necessary to assign the scope attributes to the record and to find STI subclass from the scope attributes. But the effect of `scoping` is class global, it was caused undesired behavior that pollute all class level querying methods in initialization block and callbacks (`after_initialize`, `before_validation`, `before_save`, etc), which are user provided code. To avoid the leaking scope issue, restore the original current scope before initialization block and callbacks are invoked. Fixes #9894. Fixes #17577. Closes #31526.
* | | | | | | | Refactor around scopingRyuta Kamizono2019-02-075-18/+16
|/ / / / / / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Don't use `false` as special value to skip to find inherited scope, we could use `skip_inherited_scope = true`, and move `_scoping` back on Relation.
* | | | | | | Fix `CollectionProxy#concat` to return self by alias it to `#<<`Yuya Tanaka2019-02-062-33/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | Formerly it was returning arguments (`records` array).
* | | | | | | Merge pull request #35171 from rails/speed-up-partialsAaron Patterson2019-02-051-1/+1
|\ \ \ \ \ \ \ | | | | | | | | | | | | | | | | Speed up partial rendering by caching "variable" calculation
| * | | | | | | Speed up partial rendering by caching "variable" calculationAaron Patterson2019-02-051-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This commit speeds up rendering partials by caching the variable name calculation on the template. The variable name is based on the "virtual path" used for looking up the template. The same virtual path information lives on the template, so we can just ask the cached template object for the variable. This benchmark takes a couple files, so I'll cat them below: ``` [aaron@TC ~/g/r/actionview (speed-up-partials)]$ cat render_benchmark.rb require "benchmark/ips" require "action_view" require "action_pack" require "action_controller" class TestController < ActionController::Base end TestController.view_paths = [File.expand_path("test/benchmarks")] controller_view = TestController.new.view_context result = Benchmark.ips do |x| x.report("render") do controller_view.render("many_partials") end end [aaron@TC ~/g/r/actionview (speed-up-partials)]$ cat test/benchmarks/test/_many_partials.html.erb Looping: <ul> <% 100.times do |i| %> <%= render partial: "list_item", locals: { i: i } %> <% end %> </ul> [aaron@TC ~/g/r/actionview (speed-up-partials)]$ cat test/benchmarks/test/_list_item.html.erb <li>Number: <%= i %></li> ``` Benchmark results (master): ``` [aaron@TC ~/g/r/actionview (master)]$ be ruby render_benchmark.rb Warming up -------------------------------------- render 41.000 i/100ms Calculating ------------------------------------- render 424.269 (± 3.5%) i/s - 2.132k in 5.031455s ``` Benchmark results (this branch): ``` [aaron@TC ~/g/r/actionview (speed-up-partials)]$ be ruby render_benchmark.rb Warming up -------------------------------------- render 50.000 i/100ms Calculating ------------------------------------- render 521.862 (± 3.8%) i/s - 2.650k in 5.085885s ```
* | | | | | | | Relation no longer respond to Arel methodsRyuta Kamizono2019-02-061-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This follows up d97980a16d76ad190042b4d8578109714e9c53d0.
* | | | | | | | Chaining named scope is no longer leaking to class level querying methodsRyuta Kamizono2019-02-064-8/+9
| |/ / / / / / |/| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Active Record uses `scoping` to delegate to named scopes from relations for propagating the chaining source scope. It was needed to restore the source scope in named scopes, but it was caused undesired behavior that pollute all class level querying methods. Example: ```ruby class Topic < ActiveRecord::Base scope :toplevel, -> { where(parent_id: nil) } scope :children, -> { where.not(parent_id: nil) } scope :has_children, -> { where(id: Topic.children.select(:parent_id)) } end # Works as expected. Topic.toplevel.where(id: Topic.children.select(:parent_id)) # Doesn't work due to leaking `toplevel` to `Topic.children`. Topic.toplevel.has_children ``` Since #29301, the receiver in named scopes has changed from the model class to the chaining source scope, so the polluting class level querying methods is no longer required for that purpose. Fixes #14003.
* | | | | | | Merge pull request #35127 from bogdan/counter-cache-loadingRyuta Kamizono2019-02-052-16/+13
|\ \ \ \ \ \ \ | | | | | | | | | | | | | | | | Bugfix association loading behavior when counter cache is zero
| * | | | | | | Bugfix association loading behavior when counter cache is zeroBogdan Gusiev2019-02-052-16/+13
| | | | | | | |
* | | | | | | | Merge pull request #35154 from sponomarev/chore/sqlite1.4Aaron Patterson2019-02-041-1/+1
|\ \ \ \ \ \ \ \ | |_|/ / / / / / |/| | | | | | | Relax sqlite3 version dependency
| * | | | | | | Relax sqlite3 version dependencySergey Ponomarev2019-02-041-1/+1
| | | | | | | |
* | | | | | | | Improve performance of blank? and present? in an ActiveRecord::Base instanceRafael Mendonça França2019-02-041-0/+8
|/ / / / / / / | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | With this benchmark: require "bundler/setup" require "active_record" require "benchmark/ips" # This connection will do for database-independent bug reports. ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:") ActiveRecord::Schema.define do create_table :posts, force: true do |t| end end class Post < ActiveRecord::Base end new_post = Post.new Benchmark.ips do |b| b.report("present?") do new_post.present? end b.report("blank?") do new_post.blank? end end Before: Warming up -------------------------------------- present? 52.147k i/100ms blank? 53.077k i/100ms Calculating ------------------------------------- present? 580.184k (±21.8%) i/s - 2.555M in 5.427085s blank? 601.537k (± 9.2%) i/s - 2.972M in 5.003503s After: Warming up -------------------------------------- present? 378.235k i/100ms blank? 375.476k i/100ms Calculating ------------------------------------- present? 17.381M (± 7.5%) i/s - 86.238M in 5.001815s blank? 17.877M (± 6.4%) i/s - 88.988M in 5.004634s This improvement is mostly because those methods were hitting `method_missing` on a lot of levels to be able to return the value. To avoid all this stack walking we are short-circuiting those methods. Closes #35059.
* | | | | | | Merge pull request #35089 from ↵Eileen M. Uchitelle2019-02-042-1/+10
|\ \ \ \ \ \ \ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | eileencodes/fix-query-cache-for-database-switching Invalidate all query caches for current thread