aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/scoping
Commit message (Collapse)AuthorAgeFilesLines
* Association loading isn't to be affected by null relation scopingRyuta Kamizono2019-04-061-0/+24
| | | | | | Follow up of #35868. Closes #19349.
* Association loading isn't to be affected by scoping consistentlyRyuta Kamizono2019-04-051-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Follow-up of 5c71000, #29834, and #30271. Currently, preloading and eager loading are not to be affected by scoping, with the exception of `unscoped`. But non eager loaded association access is still affected by scoping. Although this is a breaking change, the association loading will work consistently whether preloaded / eager loaded or not. Before: ```ruby Post.where("1=0").scoping do Comment.find(1).post # => nil Comment.preload(:post).find(1).post # => #<Post id: 1, ...> Comment.eager_load(:post).find(1).post # => #<Post id: 1, ...> end ``` After: ```ruby Post.where("1=0").scoping do Comment.find(1).post # => #<Post id: 1, ...> Comment.preload(:post).find(1).post # => #<Post id: 1, ...> Comment.eager_load(:post).find(1).post # => #<Post id: 1, ...> end ``` Fixes #34638. Fixes #35398.
* Add Relation#annotate for SQL commentingMatt Yoho2019-03-212-0/+48
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This patch has two main portions: 1. Add SQL comment support to Arel via Arel::Nodes::Comment. 2. Implement a Relation#annotate method on top of that. == Adding SQL comment support Adds a new Arel::Nodes::Comment node that represents an optional SQL comment and teachers the relevant visitors how to handle it. Comment nodes may be added to the basic CRUD statement nodes and set through any of the four (Select|Insert|Update|Delete)Manager objects. For example: manager = Arel::UpdateManager.new manager.table table manager.comment("annotation") manager.to_sql # UPDATE "users" /* annotation */ This new node type will be used by ActiveRecord::Relation to enable query annotation via SQL comments. == Implementing the Relation#annotate method Implements `ActiveRecord::Relation#annotate`, which accepts a comment string that will be appeneded to any queries generated by the relation. Some examples: relation = Post.where(id: 123).annotate("metadata string") relation.first # SELECT "posts".* FROM "posts" WHERE "posts"."id" = 123 # LIMIT 1 /* metadata string */ class Tag < ActiveRecord::Base scope :foo_annotated, -> { annotate("foo") } end Tag.foo_annotated.annotate("bar").first # SELECT "tags".* FROM "tags" LIMIT 1 /* foo */ /* bar */ Also wires up the plumbing so this works with `#update_all` and `#delete_all` as well. This feature is useful for instrumentation and general analysis of queries generated at runtime.
* Fix indentation [ci skip]Ryuta Kamizono2019-02-271-2/+2
|
* Fix random CI failure due to non-deterministic sorting orderRyuta Kamizono2019-02-271-5/+5
| | | | https://travis-ci.org/rails/rails/jobs/499061043#L1187-L1193
* Deprecate using class level querying methods if the receiver scope regarded ↵Ryuta Kamizono2019-02-151-2/+4
| | | | | | | | | | 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 "Chaining named scope is no longer leaking to class level querying ↵Ryuta Kamizono2019-02-141-3/+2
| | | | | | | | | methods" This reverts #32380, since this may cause that silently leaking information when people upgrade the app. We need deprecation first before making this.
* Chaining named scope is no longer leaking to class level querying methodsRyuta Kamizono2019-02-061-0/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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.
* Remove delegation of missing methods in a relation to private methods of the ↵Rafael Mendonça França2019-01-171-7/+0
| | | | class
* Fix the scoping with query methods in the scope blockRyuta Kamizono2018-11-301-1/+6
| | | | | | | | | Follow up #33394. #33394 only fixes the case of scoping with klass methods in the scope block which invokes `klass.all`. Query methods in the scope block also need to invoke `klass.all` to be affected by the scoping.
* Change the empty block style to have space inside of the blockRafael Mendonça França2018-09-251-1/+1
|
* Don't expose `current_scope` for internal useRyuta Kamizono2018-09-111-2/+2
|
* Fixes #33610Darwin D Wu2018-09-111-0/+16
| | | | | | | | | | | | In order to avoid double assignments of nested_attributes for `has_many` relations during record initialization, nested_attributes in `create_with` should not be passed into `klass.new` and have them populate during `initialize_internals_callback` with scope attributes. However, `create_with` keys should always have precedence over where clauses, so if there are same keys in both `create_with` and `where_values_hash`, the value in `create_with` should be the one that's used.
* Avoid extra scoping in delegating to klass methods in the `scope` blockRyuta Kamizono2018-07-191-0/+5
| | | | | | | | | | Since #29301, delegating to klass methods in the `scope` block would cause extra scoping by the receiver itself. The extra scoping would always override intermediate scoping like `unscoped` and caused the regression #33387. To keep the original scoping behavior, should avoid the extra scoping in the `scope` block. Fixes #33387.
* assert_calledutilum2018-04-261-2/+3
|
* Use assert_no_match for test_order_to_unscope_reorderingYasuo Honda2018-04-201-1/+1
|
* Replace `assert !` with `assert_not`Daniel Colson2018-04-193-3/+3
| | | | | This autocorrects the violations after adding a custom cop in 3305c78dcd.
* Deprecate accessibility of private/protected class methods in named scopeRyuta Kamizono2018-03-301-0/+7
|
* Use assert_empty and assert_not_emptyDaniel Colson2018-01-252-12/+12
|
* Use assert_predicate and assert_not_predicateDaniel Colson2018-01-253-40/+40
|
* Use respond_to test helpersDaniel Colson2018-01-251-3/+3
|
* Merge pull request #28313 from sandrew/masterRyuta Kamizono2018-01-151-0/+12
|\ | | | | | | Allow unscoping of left_outer_joins
* | Prevent scope named same as a ActiveRecord::Relation instance method.Chen Kinnrot2017-11-281-0/+16
| | | | | | | | | | | | | | | | | | Due to inconsistent behavior when chaining scopes and one scope named after a Relation method Validation code added in 2 places: - scope, to prevent problematic scope names. - enum, cause it tries to auto define scope.
* | Merge pull request #30980 from sobrinho/sobrinho/arel-star-ignored-columnsRafael França2017-11-131-15/+15
|\ \ | | | | | | Do not use `Arel.star` when `ignored_columns`
| * | Fix postgres ordering issue on default scoping testGabriel Sobrinho2017-11-131-15/+15
| | |
* | | `scoping` should respect current class and STI constraint (#29199)Ryuta Kamizono2017-11-061-0/+14
|/ / | | | | | | | | | | | | | | A relation includes `klass`, so it can not be used as it is if current class is different from `current_scope.klass`. It should be created new relation by current class to respect the klass and STI constraint. Fixes #17603. Fixes #23576.
* | Fix random CI failure due to non-deterministic sorting orderRyuta Kamizono2017-08-131-2/+4
| | | | | | | | https://travis-ci.org/rails/rails/jobs/263617099#L769-L775
* | Merge remote-tracking branch 'origin/master' into unlock-minitestRafael Mendonça França2017-08-013-1/+40
|\ \
| * | Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-193-0/+6
| | |
| * | Fix unscoping `default_scope` for `Preloader`Ryuta Kamizono2017-07-191-0/+2
| | |
| * | Fix unscoping `default_scope` in STI associationsRyuta Kamizono2017-07-191-0/+16
| | | | | | | | | | | | | | | | | | | | | Since 5c71000, it has lost to be able to unscope `default_scope` in STI associations. This change will use `.empty_scope?` instead of `.values.empty?` to regard as an empty scope if only have `type_condition`.
| * | Post.joins(:users) should not be affected by `User.current_scope`Sean Griffin2017-07-171-0/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This change was introduced by #18109. The intent of that change was to specifically apply `unscoped`, not to allow all changes to `current_scope` to affect the join. The idea of allowing `current_scope` to affect joins is interesting and potentially more consistent, but has sever problems associated with it. The fact that we're specifically stripping out joins indicates one such problem (and potentially leads to invalid queries). Ultimately it's difficult to reason about what `Posts.joins(:users)` actually means if it's affected by `User.current_scope`, and it's difficult to specifically control what does or doesn't get added. If we were starting from scratch, I don't think I'd have `joins` be affected by `default_scope` either, but that's too big of a breaking change to make at this point. With this change, we no longer apply `current_scope` when bringing in joins, with the singular exception of the motivating use case which introduced this bug, which is providing a way to *opt-out* of having the default scope apply to joins. Fixes #29338.
| * | Enable `Layout/FirstParameterIndentation` copRyuta Kamizono2017-07-171-1/+1
| | | | | | | | | | | | | | | | | | | | | We have some indentation cops. But now there is a little inconsistent params indentations. Enable `Layout/FirstParameterIndentation` cop to prevent newly inconsistent indentation added and auto-correct to existing violations.
| * | Fix `create_with` using both string and symbolRyuta Kamizono2017-07-161-0/+5
| | | | | | | | | | | | | | | | | | | | | This is related with #27680. Since `where_values_hash` keys constructed by `where` are string, so we need `stringify_keys` to `create_with_value` before merging it.
* | | Merge branch 'master' into unlock-minitestKasper Timm Hansen2017-07-151-0/+9
|\| |
| * | Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"Matthew Draper2017-07-023-3/+0
| | | | | | | | | | | | | | | This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
| * | Enforce frozen string in RubocopKir Shatrov2017-07-013-0/+3
| | |
| * | Fix to scoping is correctly restoredRyuta Kamizono2017-06-291-0/+9
| | | | | | | | | | | | | | | | | | | | | | | | This regression was caused by #23004. If STI subclass is using scoping in parent class scoping, `current_scope` in subclass is never restored. I fixed to restore `current_scope` to previous value correctly.
* | | Merge branch 'master' into unlock-minitestRafael Mendonça França2017-06-021-0/+6
|\| |
| * | Add missing `delegate :extending, to: :all`Ryuta Kamizono2017-06-011-0/+6
| | |
* | | Merge branch 'master' into unlock-minitestKasper Timm Hansen2017-05-291-4/+11
|\| |
| * | Fix crashing on circular left join references with scopingRyuta Kamizono2017-05-241-2/+9
| | | | | | | | | | | | Follow up of #25702.
| * | Restore `fixtures :author_addresses`Ryuta Kamizono2017-04-271-2/+2
| | | | | | | | | | | | | | | This change reverted in eac6f369 but it is needed for data integrity. See #25328.
* | | Explicitly create necessary data for testyuuji.yaginuma2017-05-131-0/+4
|/ / | | | | | | | | | | `DefaultScopingWithThreadTest` expects that there are two or more of `developers` data, but have not created data in the test. Therefore, tests may fail depending on execution order.
* | Revert "Merge pull request #27636 from ↵Rafael Mendonça França2017-04-261-2/+2
| | | | | | | | | | | | | | | | | | mtsmfm/disable-referential-integrity-without-superuser-privilege-take-2" This reverts commit c1faca6333abe4b938b98fedc8d1f47b88209ecf, reversing changes made to 8c658a0ecc7f2b5fc015d424baf9edf6f3eb2b0b. See https://github.com/rails/rails/pull/27636#issuecomment-297534129
* | Fix a failed AR test when running with OracleAdapterKoichi ITO2017-03-311-0/+2
| |
* | Use `SET CONSTRAINTS` for `disable_referential_integrity` without superuser ↵Fumiaki MATSUSHIMA2017-03-261-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | privileges (take 2) Re-create https://github.com/rails/rails/pull/21233 eeac6151a5 was reverted (127509c071b4) because it breaks tests. ---------------- ref: 72c1557254 - We must use `authors` fixture with `author_addresses` because of its foreign key constraint. - Tests require PostgreSQL >= 9.4.2 because it had a bug about `ALTER CONSTRAINTS` and fixed in 9.4.2.
* | Use `load` rather than `collect` for force loadingRyuta Kamizono2017-03-191-9/+9
| | | | | | | | | | | | | | Since b644964b `ActiveRecord::Relation` includes `Enumerable` so delegating `collect`, `all?`, and `include?` are also unneeded. `collect` without block returns `Enumerable` without preloading by that. We should use `load` rather than `collect` for force loading.
* | Fix fragile test (`AssociationProxyTest#test_save_on_parent_saves_children`)Fumiaki MATSUSHIMA2017-03-161-36/+34
|/ | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | If we run only following tests: - test/cases/scoping/default_scoping_test.rb - test/cases/associations_test.rb ``` $ cat Rakefile.test require "rake/testtask" ENV["ARCONN"] = "postgresql" Rake::TestTask.new do |t| t.libs << "test" t.test_files = %w( test/cases/scoping/default_scoping_test.rb test/cases/associations_test.rb ) end ``` a test will fail: ``` $ bundle exec rake test -f Rakefile.test /app/activesupport/lib/active_support/core_ext/enumerable.rb:20: warning: method redefined; discarding old sum Using postgresql Run options: --seed 11830 # Running: .........................................................................................F................ Finished in 6.939055s, 15.2759 runs/s, 27.9577 assertions/s. 1) Failure: AssociationProxyTest#test_save_on_parent_saves_children [/app/activerecord/test/cases/associations_test.rb:185]: Expected: 1 Actual: 2 106 runs, 194 assertions, 1 failures, 0 errors, 0 skips rake aborted! Command failed with status (1) /usr/local/bin/bundle:22:in `load' /usr/local/bin/bundle:22:in `<main>' Tasks: TOP => test (See full trace by running task with --trace) ``` In #28083, change `self.use_transactional_tests` to `false` but we forget to clean-up fixture. However we don't have to disable transaction except a few tests.
* Order array contents to match Relation#firstMatthew Draper2017-02-251-1/+1
|