Commit Graph

124 Commits

Author SHA1 Message Date
Matt Yoho
f41825809c Add Relation#annotate for SQL commenting
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.
2019-03-21 20:30:56 -07:00
Rafael Mendonça França
f679933daa
Change the empty block style to have space inside of the block 2018-09-25 13:19:35 -04:00
Ryuta Kamizono
1dc17e7b2e Fix CustomCops/AssertNot to allow it to have failure message
Follow up of #32605.
2018-05-13 11:32:47 +09:00
Daniel Colson
94333a4c31 Use assert_predicate and assert_not_predicate 2018-01-25 23:32:59 -05:00
Ryuta Kamizono
ca2a66abef Fix random CI failure due to non-deterministic sorting order
https://travis-ci.org/rails/rails/jobs/268599781#L784
2017-08-26 17:21:22 +09:00
Kir Shatrov
831be98f9a Use frozen-string-literal in ActiveRecord 2017-07-19 22:27:07 +03:00
Matthew Draper
87b3e226d6 Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"
This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing
changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
2017-07-02 02:15:17 +09:30
Kir Shatrov
cfade1ec7e Enforce frozen string in Rubocop 2017-07-01 02:11:03 +03:00
Matthew Draper
5d9d6a4fbf Add an extra test showing why collections are cached
The assert_same above obviously guarantees this will pass, but this
seems less likely to be deleted just because the implementation changed.
2017-05-30 18:21:10 +09:30
Ryuta Kamizono
c45c9cfb01 Cache the association proxy object
Some third party modules expects that association returns same proxy
object each time (e.g. for stubbing collection methods:
https://github.com/rspec/rspec-rails/issues/1817).
So I decided that cache the proxy object and reset scope in the proxy
object each time.

Related context: c86a32d745 (commitcomment-2784312)
2017-05-28 01:19:24 +09:00
Ryuta Kamizono
86e74a083a Restore fixtures :author_addresses
This change reverted in eac6f369 but it is needed for data integrity.
See #25328.
2017-04-27 15:44:25 +09:00
Rafael Mendonça França
eac6f3690f
Revert "Merge pull request #27636 from 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
2017-04-26 13:39:05 -07:00
Fumiaki MATSUSHIMA
2a129380e8 Use SET CONSTRAINTS for disable_referential_integrity without superuser 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.
2017-03-26 17:12:13 +09:00
Matthew Draper
eb41ac40cb Merge pull request #25877 from kamipo/delegate_to_scope_rather_than_merge
Delegate to `scope` rather than `merge!` for collection proxy
2017-02-21 05:24:18 +10:30
Rafael Mendonça França
127509c071
Revert "Merge pull request #21233 from mtsmfm/disable-referential-integrity-without-superuser-privileges"
This reverts commit eeac6151a55cb7d5f799e1ae33aa64a839cbc3aa, reversing
changes made to 5c40239d3104543e70508360d27584a3e4dc5baf.

Reason: Broke the isolated tests.
https://travis-ci.org/rails/rails/builds/188721346
2017-01-03 22:11:16 -05:00
Rafael França
eeac6151a5 Merge pull request #21233 from mtsmfm/disable-referential-integrity-without-superuser-privileges
Use `SET CONSTRAINTS` for `disable_referential_integrity` without superuser privileges
2017-01-03 20:38:50 -05:00
Rafael Mendonça França
09cac8c67a
Remove deprecated force reload argument in association readers 2016-12-29 17:53:03 -05:00
Ryuta Kamizono
f1217c605d No need to cache collection proxies separately
Because merging the association scope was removed.
2016-12-25 04:55:51 +09:00
Fumiaki MATSUSHIMA
e75fcdf3fe Use SET CONSTRAINTS for disable_referential_integrity without superuser privileges
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.
2016-12-03 15:53:22 +09:00
Michael Grosser
a9aed2ac94
improve error message when include assertions fail
assert [1, 3].includes?(2) fails with unhelpful "Asserting failed" message

assert_includes [1, 3], 2 fails with "Expected [1, 3] to include 2" which makes it easier to debug and more obvious what went wrong
2016-09-16 12:03:37 -07:00
Rafael França
f4b32cdc6c Merge pull request #25976 from kamipo/pluck_uses_loaded_target
`pluck` should use `records` (`load_target`) when `loaded?` is true
2016-08-17 00:35:25 -03:00
Rafael Mendonça França
55f9b8129a
Add three new rubocop rules
Style/SpaceBeforeBlockBraces
Style/SpaceInsideBlockBraces
Style/SpaceInsideHashLiteralBraces

Fix all violations in the repository.
2016-08-16 04:30:11 -03:00
Ryuta Kamizono
762e3f05f3 Add Style/EmptyLines in .rubocop.yml and remove extra empty lines 2016-08-07 17:50:59 +09:00
Xavier Noria
b326e82dc0 applies remaining conventions across the project 2016-08-06 20:20:22 +02:00
Xavier Noria
d22e522179 modernizes hash syntax in activerecord 2016-08-06 19:37:57 +02:00
Xavier Noria
9617db2078 applies new string literal convention in activerecord/test
The current code base is not uniform. After some discussion,
we have chosen to go with double quotes by default.
2016-08-06 18:26:53 +02:00
Ryuta Kamizono
3a8a9979ac pluck should use records (load_target) when loaded? is true 2016-08-04 09:13:13 +09:00
Rafael França
b37bd3d52f Merge pull request #25941 from kamipo/finder_methods_uses_load_target
`FinderMethods` uses `records` (`load_target`) when `loaded?` is true
2016-07-28 10:39:04 -03:00
Rafael França
519801db08 Merge pull request #25940 from kamipo/fix_collection_proxy_load
Fix to `CollectionProxy#load` does `load_target`
2016-07-28 10:36:50 -03:00
Ryuta Kamizono
fe48c97e26 FinderMethods uses records (load_target) when loaded? is true 2016-07-28 13:54:46 +09:00
Ryuta Kamizono
ceee776b5d Fix to CollectionProxy#load does load_target 2016-07-25 00:00:38 +09:00
Ryuta Kamizono
beb6b7add7 load_target is a public method
`send` is unnecessary.
2016-07-23 17:14:41 +09:00
akihiro17
715e845fa2 Remove unnecessary require in associations_test.rb 2015-09-03 04:27:07 +09:00
Ronak Jangir
592ba58208 Removed duplicate require ‘models/computer’ 2015-08-26 21:53:03 +05:30
Prem Sichanugrist
b5b8979621 Silence deprecation warning from force reload
We deprecate the support for passing an argument to force reload in
6eae366d0d2e5d5211eeaf955f56bd1dc6836758. That led to several
deprecation warning when running Active Record test suite.

This commit silence the warnings by properly calling `#reload` on the
association proxy or on the association object instead. However, there
are several places that `ActiveSupport::Deprecation.silence` are used as
those tests actually tests the force reload functionality and will be
removed once `master` is targeted next minor release (5.1).
2015-07-16 16:06:26 -04:00
Rafael Mendonça França
17e0878005 Do not test, document or use a private API method
These methods are nodoc so we should not document them.
2015-02-20 17:50:20 -02:00
Rafael Mendonça França
37bef5827f Merge pull request #16989 from Empact/reload-cache-clear
Isolate access to @associations_cache and @aggregations_cache to the Associations and Aggregations modules, respectively.
2015-02-20 17:45:10 -02:00
Sean Griffin
d26dd00854 WhereClause#predicates does not need to be public
The only place it was accessed was in tests. Many of them have another
way that they can test their behavior, that doesn't involve reaching
into internals as far as they did. `AssociationScopeTest` is testing a
situation where the where clause would have one bind param per
predicate, so it can just ignore the predicates entirely. The where
chain test was primarly duplicating the logic tested on `WhereClause`
directly, so I instead just make sure it calls the appropriate method
which is fully tested in isolation.
2015-01-27 10:30:38 -07:00
Sean Griffin
6a7ac40dab Go through normal where logic in AssociationScope
This removes the need to duplicate much of the logic in `WhereClause`
and `PredicateBuilder`, simplifies the code, removes the need for the
connection adapter to be continuously passed around, and removes one
place that cares about the internal representation of `bind_values`

Part of the larger refactoring to change how binds are represented
internally

[Sean Griffin & anthonynavarre]
2015-01-26 15:49:29 -07:00
Sean Griffin
17b1b5d773 Remove all references to where_values in tests 2015-01-25 17:50:19 -07:00
Sean Griffin
fb14aa0c1b Ensure first! and friends work on loaded associations
Fixes #18237
2014-12-29 11:56:37 -07:00
Erik Michaels-Ober
d1374f99bf Pass symbol as an argument instead of a block 2014-11-29 11:53:24 +01:00
Arun Agrawal
9ae210ba09 Build fix when running in isolation
`Computer` class needs to be require

See #17217 for more details
2014-11-14 10:24:11 +01:00
Ben Woosley
9d569585a2 Isolate access to @associations_cache and @aggregations cache to the Associations and Aggregations modules, respectively.
This includes replacing the `association_cache` accessor with a more
limited `association_cached?` accessor and making `clear_association_cache`
and `clear_aggregation_cache` private.
2014-09-28 16:17:06 -07:00
Yves Senn
d5580b91b6 Allow included modules to override association methods.
Closes #16684.

This is achieved by always generating `GeneratedAssociationMethods` when
`ActiveRecord::Base` is subclassed. When some of the included modules
of `ActiveRecord::Base` were reordered this behavior was broken as
`Core#initialize_generated_modules` was no longer called. Meaning that
the module was generated on first access.
2014-09-09 10:00:30 +02:00
Aaron Patterson
2e6625fb77 always reorder bind parameters. fixes #15920 2014-09-04 14:40:19 -07:00
Kelsey Schlarman
43675f014c Calling reset on a collection association should unload the assocation
Need to define #reset on CollectionProxy.
2014-01-21 18:24:28 -08:00
Aaron Patterson
f25251b573 do is_a? tests on assignment so runtime is faster 2013-07-31 18:19:36 -07:00
Vipul A M
4237d74213 Fix typo in test name and documentation 2013-05-20 10:35:43 +05:30
Jon Leighton
d7abe91cc7 Set the inverse when association queries are refined
Suppose Man has_many interests, and inverse_of is used.

Man.first.interests.first.man will correctly execute two queries,
avoiding the need for a third query when Interest#man is called. This is
because CollectionAssociation#first calls set_inverse_instance.

However Man.first.interests.where("1=1").first.man will execute three
queries, even though this is obviously a subset of the records in the
association.

This is because calling where("1=1") spawns a new Relation object from
the CollectionProxy object, and the Relation has no knowledge of the
association, so it cannot set the inverse instance.

This commit solves the problem by making relations spawned from
CollectionProxies return a new Relation subclass called
AssociationRelation, which does know about associations. Records loaded
from this class will get the inverse instance set properly.

Fixes #5717.

Live commit from La Conf! 
2013-05-10 10:36:26 +02:00