Commit Graph

22926 Commits

Author SHA1 Message Date
emily.qiu
3af0c0108d Allow KILL queries on read-only connections for mysql 2023-08-30 14:58:56 -05:00
Ian Candy
eae26caec3 Clarify deprecation warning for alias_attribute
We ran into a few cases at GitHub where we were using alias_attribute
incorrectly and the new behavior either didn't warn or raised an unclear
deprecation warning. This attempts to add clarity to the deprecation reason
when you try to alias something that isn't actually an attribute.

Previously, trying to alias a generated attribute method, such as `attribute_in_database`, would
still hit `define_proxy_call`, because we were only checking the owner of the target method.

In the future, we should probably raise if you try to use alias_attribute for a non-attribute.

Note that we don't raise the warning for abstract classes, because the attribute may be implemented
by a child class. We could potentially figure out a way to raise in these cases as well, but this
hopefully is good enough for now.

Finally, I also updated the way we're setting `local_alias_attributes` when `alias_attribute` is
first called. This was causing problems since we now use `alias_attribute` early in the
`load_schema!` call for some models: https://buildkite.com/rails/rails/builds/98910
2023-08-30 13:19:58 -04:00
HParker
451053eca5 Add changelog entry for batching using loaded relations
This backfills a changelog entry for PR #48876. This is potentially something Rails users
should be aware of as in very specific situations it can be a change in behavior
2023-08-28 17:29:52 -07:00
Greg Molnar
c775fa4db5 upsert should accept update_only parameter like upsert_all does 2023-08-28 16:59:24 +00:00
paulreece
4026aba1f1 This branch fixes the issues raised in #48862. By adding the key and it's values to self.references_values as Rails does when building a where clause, it prevents unneccessary aliasing from taking place and allows a User to successfully use a table_alias in their select query using a Hash. 2023-08-25 17:53:59 -04:00
Étienne Barrié
0810752188 Use Object#with 2023-08-25 17:06:14 +02:00
Étienne Barrié
7e11402e72 Fix test leak 2023-08-25 17:01:22 +02:00
fatkodima
df12ca09a0 Do not write to debug.log in activerecord tests on CI 2023-08-24 22:37:07 +03:00
fatkodima
46bd163ee6 Fix slow ActiveRecord::QueryCache test 2023-08-24 22:37:07 +03:00
Rafael Mendonça França
d70707d9af
Merge pull request #49020 from Shopify/allow-redefining-to-param-delimiter
Allow redefining `to_param` delimiter using `param_delimiter`
2023-08-23 18:11:14 -04:00
Rafael Mendonça França
59542e86c4
Merge pull request #49019 from adrianna-chang-shopify/ac-deprecate-read-attribute-id-pk
Deprecate `read_attribute(:id)` returning the primary key
2023-08-23 18:02:36 -04:00
Rafael Mendonça França
c70ac1d9eb
Merge pull request #49012 from skipkayhil/hm-migration-table-def-methods
Ensure all migration versions use TableDefinition
2023-08-23 17:59:41 -04:00
Nikita Vasilevsky
564a3d6d97
Support composite primary key in AR::Base#to_param 2023-08-23 21:53:47 +00:00
Adrianna Chang
ac83311bae
Deprecate read_attribute(:id) returning the primary key
This commit deprecates `read_attribute(:id)` returning the primary key
if the model's primary key is not the id column. Starting in Rails 7.2,
`read_attribute(:id)` will always return the value of the id column.

This commit also changes `read_attribute(:id)` for composite primary
key models to return the value of the id column, not the composite
primary key.
2023-08-23 16:36:32 -04:00
Rafael Mendonça França
3a680cd647
Merge pull request #49015 from Shopify/update-changelong-about-undefine-attribute-methods
Add new behavior of `undefine_attribute_methods` to CHANGELOG
2023-08-23 14:38:39 -04:00
Nikita Vasilevsky
e50fce0fa9
Add new behavior of undefine_attribute_methods to CHANGELOG
In `1818beb3a3ea5fdb498095d4885f8a7e512f24ca` Rails changed the target
where alias attribute methods are defined. It lead to
`undefine_attribute_methods` to clean alias attribute methods along with
the attribute methods. It was an intended behavior change but it wasn't
properly documented and tested. This commit clarifies the new behavior
in the Active Model changelog along with covering the behavior with tests.
2023-08-23 14:41:10 +00:00
Adrianna Chang
a5f64bf29d
Merge pull request #49004 from andrewn617/improve-base-class-documentation
Improve the documentation for ActiveRecord::Inheritance#base_class
2023-08-23 09:36:12 -04:00
Andrew Novoselac
46279956cf Improve the documentation for ActiveRecord::Inheritance#base_class 2023-08-23 09:19:40 -04:00
Jean Boussier
be2c6320f1
Merge pull request #48996 from Shopify/check-for-parent-being-an-owner-of-the-target-method
Allow parent being the owner of an alias attribute method
2023-08-23 14:33:00 +02:00
Nikita Vasilevsky
0c99588919
Allow parent being the owner of an alias attribute method
When an abstract class inherited from an Active Record model
defines an alias attribute Rails should expect the original methods
of the aliased attribute to be defined in the parent class and avoid
raising deprecation warning.
2023-08-23 11:35:45 +00:00
Hartley McGuire
c793cdc665
Ensure all migration versions use TableDefinition
This is similar to a [previous commit][1] which ensures that versioned
migrations always call `super` in `compatible_table_definition`. In this
case, these methods are being pulled up to `Current` so that all
subclasses will use a `TableDefinition` class and future developers do
not have to remember to add all of these methods to new versioned
classes when a new one is created.

[1]: 16f8bd79444a512dfebf2d77bd2fd3075041475b
2023-08-23 02:03:31 -04:00
Hartley McGuire
9b07b2d6ca
Fix 6.1 change_table setting datetime precision
While working on #48969, I found that some of the Compatibility test
cases were not working correctly. The tests removed in this commit were
never running the `change_table` migration and so were not actually
testing that `change_table` works correctly. The issue is that the two
migrations created in these tests both have `nil` versions, and so the
Migrator only runs the first one.

This commit refactors the tests so that its easier to test the behavior
of each Migration class version (and I think the rest of the tests
should be updated to use this strategy as well). Additionally, since the
tests are fixed it exposed that `t.change` in a `change_table` is not
behaving as expected so that is fixed as well.
2023-08-22 14:01:52 -04:00
Rafael Mendonça França
ed5af00459
Merge pull request #48998 from Shopify/to_key-supports-composite-primary-key
Support composite identifiers in `to_key`
2023-08-22 13:53:19 -04:00
Nikita Vasilevsky
8a5cf4cf44
Support composite identifiers in to_key
This commit adds support for composite identifiers in `to_key`.
Rails 7.1 adds support for composite primary key which means that
composite primary key models' `#id` method returns an `Array` and
`to_key` needs to avoid double-wrapping the value.
2023-08-22 16:13:23 +00:00
Jean Boussier
5f6c404853
Merge pull request #48983 from JoeDupuis/fix-regression-on-strict-has-one-through
Fix regression blocking creation of some strict association
2023-08-22 17:20:56 +02:00
Hartley McGuire
c2f838e80c
Fix 6.1 change_column setting datetime precision
This is already the case for add_column and create_table, but
change_column was missed
2023-08-21 19:10:14 -04:00
Rafael Mendonça França
ccee593fed
Merge pull request #48930 from adrianna-chang-shopify/ac-id-value
Use `alias_attribute` to provide `id_value` alias for `id` attribute
2023-08-21 14:33:39 -04:00
Adrianna Chang
e90b11e77e
Use alias_attribute to provide #id_value alias for #id
This allows access to the raw id column value on records for which an
id column exists but is not the primary key. This is common amongst
models with composite primary keys.
2023-08-21 14:12:53 -04:00
Rafael Mendonça França
6beb3482cd
Merge pull request #48991 from andrewn617/fix-alias-attribute-on-superclass
Fix defect where aliased attribute methods on abstract classes were not being defined
2023-08-21 13:55:46 -04:00
Andrew Novoselac
211146b845 Fix defect where aliased attribute methods on abstract classes were being not defined
rails/rails@a88f47d fixed an issue where subclasses were regenerating aliased attribute methods defined on parent classes. Part of the solution was to call #generate_alias_attributes recursively on a class's superclass to generate all the alias attributes in the inheritance hierarchy. However, the implementation relies on #base_class to determine if we should call #generate_alias_attributes on the superclass. Since all models that inherit from abstract classes are base classes, this means that #generate_alias_attributes will never be called on abstract classes, meaning no method will be generated for any alias attributes defined on them.

To fix this issue, we should always call #generate_alias_attributes on the superclass unless the superclass is ActiveRecord::Base.
2023-08-21 13:32:26 -04:00
Joé Dupuis
4b3571803b Fix regression blocking creation of some strict association
PR #48606 introduced a regression preventing the creation of a chain
of models using a has_one through association when strict_loading is
activated.

The association relies on the initial stale_state being `nil`
to skip loading the target.
755c13f167/activerecord/lib/active_record/associations/association.rb (L175)

PR #48606 initializes the stale_state to `[nil]`
https://github.com/rails/rails/pull/48606/files#diff-39c3da3c5f3fbff01b0a32d3b7ec613fda6bc6225fdbe2629134d91babe37786R84-R86

This change converts the `[nil]` state back to `nil`.
2023-08-21 10:32:08 -07:00
Rafael Mendonça França
77080d5e17
Initialize @has_query_constraints to false in the inherited hook
`has_query_constraints?` is a class method so doing it in init_internals
would not define the right variable.
2023-08-21 15:58:58 +00:00
Guillermo Iguaran
4ec3a986d5
Merge pull request #48959 from skipkayhil/hm-clean-filters-requires
Remove uneeded requires of core_ext/string/filters
2023-08-18 16:03:44 -07:00
Rafael Mendonça França
ce36426c98
Initialize the variable in the init_internals method
This will avoid that the conditional assignment is executed every time
has_query_constraints? is called.
2023-08-18 22:13:14 +00:00
eileencodes
3b5a4a56fb
Fix uninitialized ivar 2023-08-18 13:30:51 -04:00
eileencodes
784ca88173
Add method for accessing only query constraints
I'm working on an enhancement to query constraints that will require me
to know when we have query constraints but not a composite key.
Currently if you have a composite key it will be included in the query
constraints list. There's not a way to differentiate between the two
which means that we're forced into setting the query constraints on the
associations for the primary key.

This change adds a `has_query_constraints?` method so we can check the
class for query constraints. The options still work as well but we can
be sure we're always picking up the query constraints when they're
present.
2023-08-18 13:21:06 -04:00
Eileen M. Uchitelle
ac5f9dec53
Merge pull request #48947 from eileencodes/use-IN-sql-over-OR-with-query-constraints
Use IN sql instead of OR sql for query constraints
2023-08-17 15:34:31 -04:00
eileencodes
0bbf4e86cb
Use IN sql intead of OR sql for query constraints
We originally implemented this project to use OR queries instead of IN
because it seemed like there was no way to do that without a row
constructor. While working on implementing this feature in our vitess
gem I noticed that we can actually get the queries we wanted, and the
new code is more performant than generating an OR.

SQL Before:

```sql
SELECT "sharded_comments".*
FROM "sharded_comments"
WHERE ("sharded_comments"."blog_id" = 969142904
AND ("sharded_comments"."blog_post_id" = 357271355
OR "sharded_comments"."blog_post_id" = 756811794)
OR "sharded_comments"."blog_id" = 308674288
AND "sharded_comments"."blog_post_id" = 1055755181)
```

SQL After:

```sql
SELECT "sharded_comments".*
FROM "sharded_comments"
WHERE "sharded_comments"."blog_id"
IN (969142904, 308674288)
AND "sharded_comments"."blog_post_id"
IN (357271355, 756811794, 1055755181)
```

Using one of the tests that utilizes this code path, I benchmarked the
queries. The new implementation is faster:

Before:

```
Warming up --------------------------------------
             queries   147.000  i/100ms
Calculating -------------------------------------
             queries      1.486k (± 3.2%) i/s -      7.497k in   5.050742s
```

After:

```
Warming up --------------------------------------
             queries   179.000  i/100ms
Calculating -------------------------------------
             queries      1.747k (± 4.5%) i/s -      8.771k in   5.031424s
```

We can probably improve this more but I think this query is more in line
with what we want and expect while also being more performant (without
having to build a new row constructor in arel).
2023-08-17 15:04:32 -04:00
Eileen M. Uchitelle
b56f938b5f
Merge pull request #48964 from sinsoku/fix-method-name
Fix method name to `check_all_pending!`
2023-08-17 10:04:41 -04:00
Eileen M. Uchitelle
8abfcd752a
Merge pull request #48913 from ipc103/fix-parent-deprecation-warning
Allow parent to define alias method override
2023-08-17 09:46:26 -04:00
Takumi Shotoku
0e41e2cd8e
Fix method name to check_all_pending!
The method added in 03379d1f59 is `check_all_pending!`, not
`check_pending_migrations!`.
2023-08-17 16:51:39 +09:00
Ian Candy
a88f47d012 Only define attribute methods for class
Because we're using a class_attribute to track all of the attribute_aliases,
each subclass was regenerating the parent class methods, causing some unexpected
deprecation messages.

Instead, we can use a class instance variable to track the attributes aliased locally
in that particular subclass. We then walk up the chain and re-define the attribute methods
if they haven't been defined yet.
2023-08-16 18:23:02 -04:00
Hartley McGuire
ff6e885d59
Remove uneeded requires of core_ext/string/filters
`actionpack/lib/action_dispatch/routing.rb`
- added: 013745151be062aa4d0fc1f2a008a7303fdb6e04
- removed: 93034ad7fea7e00562103a7cd0acfab19bbfadf9

`activejob/lib/active_job/log_subscriber.rb`
- added: b314ab555e0d85e6efb41be94fb5f3a157bb12fe
- removed: 5ab2034730feacfc2caee418f8c0b55191d27427

`activemodel/lib/active_model/errors.rb`
- added: cf7fac7e29bb2816412c949fdaed3d61a923eb23
- removed: 9de6457ab0767ebab7f2c8bc583420fda072e2bd

`activerecord/lib/active_record/core.rb`
- added: b3bfa361c503e107aff4dee5edf79bd7fd3d3725
- removed: e1066f450d1a99c9a0b4d786b202e2ca82a4c3b3

`activesupport/lib/active_support/core_ext/module/introspection.rb`
- added: 358ac36edf1695fcbec0aa21f126a3d8b83d4b5a
- removed: 167b4153cac0069a21e0bb9689cb16f34f6abbaa

`activesupport/lib/active_support/duration.rb`
- added: 75924c4517c8f87712d3f59c11f10152ed57b9d8
- removed: a91ea1d51048342d13fc73f9b09ce4cfd086bb34

`railties/lib/rails/commands/server/server_command.rb`
- added: f2173648938b418d120f5a68d8f3862d8ae9dace
- removed: 553b86fc751c751db504bcbe2d033eb2bb5b6a0b

`railties/lib/rails/command/base.rb`
- added: 6813edc7d926965e5644cd8befaf229a35b9d8ca
- removed: b617a561d865a65cfc140caa0e3c4af4350bfcef
2023-08-16 17:39:25 -04:00
Rafael Mendonça França
b6669e5605
Merge pull request #48927 from paulreece/remove_table_alias_writer
This removes the writer method for table_alias from Arel::Table.
2023-08-11 15:34:18 -04:00
paulreece
6b56de4183 This removes the writer method for table_alias from Arel::Table. Since arel_table is a private API of the framework, no one should be modifying it. 2023-08-10 20:20:13 -04:00
zzak
81fa9136f8
Merge pull request #48924 from skipkayhil/hm-docs-fix-plusses
Fix code blocks using + instead of <tt> [ci skip]
2023-08-11 08:04:19 +09:00
Hartley McGuire
bac6d8c079
Fix code blocks using + instead of <tt>
Pluses cannot be used to create code blocks when the content includes a
space.

Found using a regular expression:

```bash
$ rg '#\s[^+]*\+[^+]*\s[^+]*\S\+'
```
2023-08-10 13:05:05 -04:00
Eileen M. Uchitelle
22fa76c125
Merge pull request #48911 from jhbabon/fix/schema-cache-settings
Ensure schema_cache_dump settings are propagated to SchemaReflection class
2023-08-10 10:57:57 -04:00
Nikita Vasilevsky
594f539324
Use ActiveRecord.deprecator for the alias attribute deprecation 2023-08-09 20:51:23 +00:00
Eileen M. Uchitelle
8ec27a4529
Merge pull request #43386 from rdimartino/dirty_store
Fix tracking previous changes for ActiveRecord::Store accessors with underlying JSON data column
2023-08-09 12:32:16 -04:00