Commit Graph

22713 Commits

Author SHA1 Message Date
Gannon McGibbon
4d04775920 Use _read_attribute when autosaving has_one associations
In models with composite primary keys, the #id accessor will return an
array, where we actually want the id column value.
2023-06-15 15:52:09 -05:00
Eileen M. Uchitelle
8a4543d739
Merge pull request #48484 from adrianna-chang-shopify/ac-composite-primary-key-associations-inverse
Fix setting inverses for composite primary key associations
2023-06-15 14:03:18 -04:00
Adrianna Chang
4e53fd0aad
Fix setting inverses for composite primary key associations
Checking whether a record has a foreign key for a composite primary key
association requires us to check whether all parts of the foreign key
are present. Otherwise, the inverse association will not be set.
2023-06-15 13:30:19 -04:00
Ray Faddis
ce6047f84f Adding PG enum rename, add value, and rename value migration helpers 2023-06-15 10:03:23 -04:00
Eileen M. Uchitelle
3d279657bc
Merge pull request #48474 from Shopify/fix-destroy-all-for-through-associations-on-cpk-models
Fix `destroy_all` for `has_many through:` associations that points to a CPK model
2023-06-14 14:36:27 -04:00
Nikita Vasilevsky
892e4ae24c
Fix destroy_all for has_many :through associations that points to a CPK model 2023-06-14 17:48:13 +00:00
eileencodes
7cfcf53d66
Fix flaky primary key test
I noticed in a build that this test was flaky and causing subsequent
tests to fail with a no connection pool error. I don't entirely get why
this test was failing, it's not the only one where we remove a
connection. When this test fails, `@automatic_reconnect` is `false`.
After spending far too much time trying to figure out the combination of
tests that break this I decided to fix it by making a brand new
connection that we can remove. It's also a more accurate test because
we're asserting the connection is actually not `connected?`. Note that I
had to use the connection handler to remove the connection because there
is an inconsitency in remove_connection on the model. Unfortunately,
`NoConnection.remove_connection` will cause `connected?` to fall back to
`ActiveRecord::Base` which is contrary to the behavior of all other
methods on the active record models. I'm going to send a PR to fix that
but for now this should fix the (rare) flaky test for a primary key with
no connection.

To reproduce I used this seed: `SEED=29535 bundle exec rake test:mysql2`
2023-06-14 12:06:51 -04:00
Eileen M. Uchitelle
a14c5f07e1
Merge pull request #48436 from gmcgibbon/cpk_malformed_key
Add better TypeError when assigning CPK
2023-06-12 12:40:16 -04:00
Eileen M. Uchitelle
716baea69f
Merge pull request #48440 from fatkodima/fix-batching-using-cpk
Fix ActiveRecord batching over composite primary keys
2023-06-12 09:24:09 -04:00
a5-stable
6538d65b04 wrap Arel.sql to avoid error 2023-06-12 00:43:35 +09:00
fatkodima
14cfad3137 Fix ActiveRecord batching over composite primary keys 2023-06-10 18:20:25 +03:00
Gannon McGibbon
13b495bfd0 Add better TypeError when assigning CPK
Raises a more readable error when assigning a singular ID to a
model with a composite primary key.
2023-06-09 13:16:14 -05:00
Jenny Shen
b202d5be99
Handle ER_DBACCESS_DENIED_ERROR code in mysql2 adapter
During the process of adding adapter tests to test the different types of connection errors,
the ER_DBACCESS_DENIED_ERROR code was discovered to be not handled. It is the same as
ER_ACCESS_DENIED_ERROR except that a database has been given.

https://dev.mysql.com/doc/mysql-errors/8.0/en/server-error-reference.html#error_er_dbaccess_denied_error

Co-authored-by: Adrianna Chang <adrianna.chang@shopify.com>
2023-06-09 10:26:36 -04:00
Jean Boussier
ec1aa50a89 Revert "Merge pull request #48406 from rails/revert-46790-singular-association-refactor"
This reverts commit f6f2b2d53f244138df58c8b5659d132fcf0d1832, reversing
changes made to 3e01b26a16857c83ba0507e1533f02bda4a5d516.
2023-06-07 10:02:59 +02:00
Jean Boussier
3255411981 Add tests around has_one changing the target and validators
Ref: https://github.com/rails/rails/issues/48330

When replacing the has_one target, it seems more correct to first
delete the old record, so that if the associated model has a uniqueness
validator, it won't fail.

Both the delete and the insert are in a single transaction, so if the new
record fail to be saved, the transaction will be rolled back, which seems
correct.

If `dependent: :destroy` isn't set, Active Record will try to orphan the
record, which may or may not be valid depending on the schema and validators.

Co-Authored-By: zzak <zzakscott@gmail.com>
2023-06-07 09:58:02 +02:00
Yasuo Honda
655278edeb
Merge pull request #48413 from fatkodima/fix-autosave-association-with-validation-on-save
Fix autosave associations with validations added on `:base` of the associated objects
2023-06-07 11:14:05 +09:00
fatkodima
d01c4a7045 Fix autosave associations with validations added on :base of the associated objects 2023-06-07 02:07:53 +03:00
Rafael Mendonça França
43852db088
Merge pull request #48409 from abaldwin88/test_postgres_auto_populate_virtual_stored_columns
Test virtual stored columns on create for Postgres
2023-06-06 18:26:15 -04:00
Nikita Vasilevsky
e583f15a4e
Use native assert_raises instead of custom assert_raises_with_message.
`assert_raises` has been extended with a `match:` keyword argument to
assert the error message so there is no reason for Rails itself to keep
maintaining custom implementations of the same feature.
2023-06-06 20:10:23 +00:00
Alex Baldwin
9b57b83c76 Test virtual stored columns on create for Postgres
Closes: https://github.com/rails/rails/issues/45736

Codify example from #45736 into test suite
2023-06-06 14:58:57 -04:00
Nikita Vasilevsky
257f0124fc
Allow composite primary key to be derived from schema
This commit stops issuing the
"Active Record does not support composite primary key" warning
and allows `ActiveRecord::Base#primary_key` to be derived as an `Array`
2023-06-06 14:39:04 +00:00
Eileen M. Uchitelle
e7be1e76bc
Merge pull request #48400 from adrianna-chang-shopify/ac-scope-for-create-on-cpk
Fix `#scope_for_create` for belongs_to associations with CPK
2023-06-06 09:04:22 -04:00
Jean Boussier
7f1c93bd19
Revert "Small refactor on build singular association" 2023-06-06 14:33:01 +02:00
Matthew Draper
3e01b26a16 Merge pull request #48295 from luanzeba/connection_attr_reader
Store `connection_pool` in database-related exceptions
2023-06-06 20:33:55 +09:30
Adrianna Chang
e684588e53
Fix #scope_for_create for belongs_to associations with CPK
`#scope_for_create` for singular associations removes the primary key
from the scope so that we don't assign PK columns when building an
association.

However, removing the primary key from the scope doesn't currently
handle composite primary keys. This commit fixes that.
2023-06-05 17:06:12 -04:00
Nikita Vasilevsky
d75508a73c
Remove unnecessary disabling of use_transactional_tests 2023-06-05 20:08:44 +00:00
eileencodes
30aee98e60
Fix flaky build
These tests have `use_transcational_tests` set to false so we need to
delete the `Cpk::Book` and `Cpk::Order` in the teardown to avoid flaky
builds.
2023-06-05 15:56:26 -04:00
Eileen M. Uchitelle
768ebf9985
Merge pull request #48385 from adrianna-chang-shopify/ac-autosave-belongs-to-destroy
Fix destroying belongs_to associations for CPK
2023-06-05 13:33:01 -04:00
takuyaK
9452b59506 Support batching using composite primary keys and multiple column ordering
When find_each/find_in_batches/in_batches are performed on a table with composite primary keys, ascending or descending order can be selected for each key.

    ```ruby
    Person.find_each(order: [:desc, :asc]) do |person|
      person.party_all_night!
    end
    ```
2023-06-05 21:59:38 +09:00
Rafael Mendonça França
80271fe5a4
Cleanup CHANGELOG of feature that will not be released 2023-06-02 21:07:24 +00:00
Rafael Mendonça França
fb919dac93
Merge pull request #47647 from fatkodima/remove_unique_key-by-options
Consider options when removing UNIQUE KEYs
2023-06-02 16:56:01 -04:00
Adrianna Chang
be231a7f22
Fix destroying belongs_to associations for CPK
This commit handles destroying CPK associations when autosave is set
and the parent association is marked for destruction. It does so
by ensuring that all parts of the foreign key (the parent's CPK)
are set to nil before destroying the parent record.
2023-06-02 15:39:33 -04:00
fatkodima
9ee4ea980e Require missing models in ActiveRecord tests 2023-06-02 21:34:54 +03:00
Gannon McGibbon
87c2ad0a81 Fix has_one autosaving for CPK associations
Adds support for autosaving has_one associations with composite primary keys.
2023-06-02 11:20:53 -05:00
Luan Vieira
7d4c88dde3
Store connection_pool in DB exceptions
Anytime an exception is raised from an adapter we now provide a
`connection_pool` along for the application to further debug what went
wrong. This is an important feature when running a multi-database Rails
application.

We chose to provide the `connection_pool` as it has relevant context
like connection, role and shard. We wanted to avoid providing the
`connection` directly as it might accidentally be used after it's
returned to the pool and been handed to another thread.
The `ConnectionAdapters::PoolConfig` would also have been a reasonable
option except it's `:nodoc:`.
2023-06-02 10:44:28 -04:00
Eileen M. Uchitelle
254f1d8ded
Merge pull request #48357 from gmcgibbon/belongs_to_cpk
Add composite primary key validity check on belongs_to associations.
2023-06-02 08:25:16 -04:00
Gannon McGibbon
f9a8f9c976 Add composite primary key validity check to associations.
Raise ActiveRecord::CompositePrimaryKeyMismatchError when a belongs_to,
has_one, or has_many foreign key and primary key don't have the same length.
2023-06-02 01:32:59 -05:00
Nixon
aa41938e3a Fix polymorphic association subquery 2023-06-02 02:50:23 -03:00
Yasuo Honda
95af5fce71
Merge pull request #45783 from shhavel/fix/anonymous_pg_columns_of_different_type_from_json
[AR] Fix result with anonymous PG columns of different type from json
2023-06-02 08:33:08 +09:00
eileencodes
a6efde6cf6
Minor changelog edits 2023-06-01 16:13:33 -04:00
eileencodes
2c39149e99
Revert deprecation message for prepared statements
While we had hoped to turn prepared statements on for Rails 7.2, the bug
that's preventing us from doing that is still present. See #43005.

Until this bug is fixed we should not be encouraging applications
running mysql to change the `prepared_statements` in the config to
`true`. In addition to this bug being present, Trilogy does not yet
support `prepared_statements` (although work is in progress).

It will be better to implement this deprecation when mysql2 and trilogy
can both handle `prepared_statements` without major bugs.
2023-06-01 16:11:55 -04:00
Nikita Vasilevsky
c92933265e
Assign auto populated columns on Active Record object creation
This commit extends Active Record creation logic to allow for a database
auto-populated attributes to be assigned on object creation.

Given a `Post` model represented by the following schema:
```ruby
create_table :posts, id: false do |t|
  t.integer :sequential_number, auto_increment: true
  t.string :title, primary_key: true
  t.string :ruby_on_rails, default: -> { "concat('R', 'o', 'R')" }
end
```
where `title` is being used as a primary key, the table has an
integer `sequential_number` column populated by a sequence and
`ruby_on_rails` column has a default function - creation of
`Post` records should populate the `sequential_number` and
`ruby_on_rails` attributes:

```ruby
new_post = Post.create(title: 'My first post')
new_post.sequential_number # => 1
new_post.ruby_on_rails # => 'RoR'
```

* At this moment MySQL and SQLite adapters are limited to only one
column being populated and the column must be the `auto_increment`
while PostgreSQL adapter supports any number of auto-populated
columns through `RETURNING` statement.
2023-06-01 14:39:12 +00:00
eileencodes
7d60710559
Add role to connection payload
It doesn't make sense to include shard if we're also not going to
include role.
2023-05-31 15:59:21 -04:00
Eileen M. Uchitelle
ced5e77936
Merge pull request #48353 from eileencodes/set-default-shard-from-connects_to-hash
Set default_shard from connects_to hash
2023-05-31 15:29:57 -04:00
eileencodes
0d41bfd3cc
Set default_shard from connects_to hash
If an application is using sharding, they may not want to use `default`
as the `default_shard`. Unfortunately Rails expects there to be a shard
named `default` for certain actions internally. This leads to some
errors on boot and the application is left manually setting
`default_shard=` in their model or updating their shards in
`connects_to` to name `shard_one` to `default`. Neither are a great
solution, especially if Rails can do this for you. Changes to Active
Record are:

* Simplify `connects_to` by merging `database` into `shards` kwarg so we
can do a single loop through provided options.
* Set the `self.default_shard` to the first keys in the shards kwarg.
* Add a test for this behavior
* Update existing test that wasn't testing this to use `default`. I
could have left this test but it really messes with connections in the
other tests and since this isn't testing shard behavior specifically, I
updated it to use `default` as the default shard name.

This is a slight change in behavior from existing applications but
arguably this is fixing a bug because without this an application won't
boot. I originally thought that this would require a huge refactoring to
fix but realized that it makes a lot of sense to take the first shard as
they default. They should all have the same schema so we can assume it's
fine to take the first one.

Fixes: #45390
2023-05-31 12:52:12 -04:00
fatkodima
c3b0878f2e Preserve timestamp when setting an ActiveSupport::TimeWithZone value to timestamptz attribute 2023-05-31 00:35:29 +03:00
fatkodima
1c469c5a74 Consider options when removing UNIQUE KEYs 2023-05-30 15:15:26 +03:00
Jean Boussier
ae02cd6539
Merge pull request #48322 from fatkodima/fix-locking-with-counter-caches
Fix decrementing counter caches for parent records using optimistic locking
2023-05-30 09:45:56 +02:00
zzak
dd9f3ff64a
Fix headings for AR::QueryMethods#where
* Use capital case for headings
* Escape String, Hash, Array, Module from RDoc autolinking
2023-05-30 13:45:46 +09:00
zzak
8a3e5d2448
Fixed-width font fixes for AR::QueryMethods 2023-05-30 13:44:54 +09:00