Commit Graph

77464 Commits

Author SHA1 Message Date
Jonathan Hefner
6aa26c30e2
Identify directly-uploaded blobs before saving the associated record
An Active Storage `Blob` must be identified before it can be reliably
validated.  For direct uploads, a `Blob` is identified when it is
attached, rather than when it is created.

Before this commit, the sequence of events when attaching a `Blob` was:

1. Find the `Blob`.
2. Assign the `Blob` to an `Attachment`.
3. Save the owner record.
4. Save the `Attachment`.
5. Identify the `Blob`'s true `content_type` from its file.
6. Save the `Blob`.

This meant that the owner record's validations might not see the
`Blob`'s true `content_type`.

After this commit, the sequence of events will be:

1. Find the `Blob`.
2. Identify the `Blob`'s true `content_type` from its file.
3. Assign the `Blob` to an `Attachment`.
4. Save the owner record.
5. Save the `Attachment`.
6. Save the `Blob`.

Thus the `Blob`'s true `content_type` will be available when running the
owner record's validations.
2020-08-11 18:08:09 -04:00
Jonathan Hefner
b32a614d9a
Merge pull request #40024 from abhaynikam/document-multidb-dbconsole-command
Document dbconsole command for multiple database [ci skip]
2020-08-11 11:38:42 -05:00
Abhay Nikam
d747b53172 Document the --database/--db option for multiple database Rails application [skip ci] 2020-08-11 21:37:37 +05:30
Gannon McGibbon
23a9e29d9b
Merge pull request #39981 from gmcgibbon/mounted_root_route_assert
Fix `assert_recognizes` on mounted root routes.
2020-08-10 17:20:47 -04:00
Eileen M. Uchitelle
685683fd02
Merge pull request #40017 from eileencodes/fix-missed-handler-method
Fix missed establish_connection
2020-08-10 17:07:58 -04:00
Gannon McGibbon
8d4d0f3701 Fix assert_recognizes on mounted root routes.
Allow `assert_recognizes` routing assertions to work on mounted root routes.
2020-08-10 16:59:52 -04:00
eileencodes
919eb6dca9
Fix missed establish_connection
In `connected_to` one of the deprecated arguments wasn't well tested so
the incorrect methods signature wasn't caught by the tests.

This change updates the caller when `connected_to` uses the database
key.

I've also cleaned up a few arguments that weren't necessary. Since
the handler methods set defaults for the `shard` key, we don't need to
pass that in `establish_connection` when not using the sharding API.
2020-08-10 16:31:04 -04:00
Eileen M. Uchitelle
64915e5b7f
Merge pull request #40014 from eileencodes/move-handler-cleanup-to-helper-method
Add helper method for resetting connection handlers in tests
2020-08-10 10:41:50 -04:00
eileencodes
61ab154316
Add helper method for resetting connection handlers in tests
This change makes a helper method for resetting connection handlers in
the Active Record tests. The change here is relatively small and may
seem unnecessary. The reason we're pushing this change is for upcoming
refactoring to connection management. This change will mean that we can
update one location instead of 9+ files to reset connections. It will
reduce the diff of our refactoring and make reusing this code easier in
the future.

The method name chosen is purposefully `clean_up_connection_handler`
over `clean_up_connection_handlers` because in the future there will
only be one handler.

Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
2020-08-10 10:20:12 -04:00
Eileen M. Uchitelle
f2019462f9
Merge pull request #40006 from eileencodes/rename-pool-key-and-use-kwargs
Rename `pool_key` to `shard` and use kwargs for connection methods
2020-08-10 08:53:16 -04:00
Ryuta Kamizono
f9315d2ead
Merge pull request #40000 from kamipo/fix_eager_loading_duplicated_association
Fix incorrect result when eager loading with duplicated through association with join scope
2020-08-10 15:41:22 +09:00
Corey Smith
3c500511e9
Remove the unnessecary default: 'gen_random_uuid()' (#40012) 2020-08-08 15:11:34 +05:30
Eileen M. Uchitelle
5d3b9f7c87
Merge pull request #40003 from eileencodes/remove-unnecessary-with_temporary_connection_pool-calls
Remove unnecessary with_temporary_connection_pool calls
2020-08-07 14:09:13 -04:00
eileencodes
d3061cdab2
Update connection methods to use kwargs
This change ensures that the connection methods are using kwargs instead
of positional arguments. This change may look unnecessary but we're
working on refactoring connection management to make it more robust and
flexible so the method signatures of the methods changed here will
continue to evolve and change.

This commit does not change any released public APIs. The `shard` and
`owner_name` arguments were added in 6.1 which is not released yet.
Using kwargs will allow these methods to be more flexible and not get
super ugly as we change their underlying behavior. The kwargs let us
support multiple non-positional arguments with default.

Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
2020-08-07 14:06:09 -04:00
eileencodes
85ef219621
Rename pool_key to shard
This change renames the following:

* `current_pool_key` -> `current_shard`
* `default_pool_key` -> `default_shard`
* `pool_key` -> `shard`

Originally we had intended to name the `shard` as `pool_key` because
when we implemented the internal private API we weren't sure how it was
going to be used for sharding and wanted to implement behavior without
promising a public API. Now that we have a public API for sharding it's
better to use the same name everywhere rather than have one name for
private APIs and one name for public APIs. This should make
contributions and tracking down bugs easier in the future.

This PR doesn't require any deprecations because the sharding API is
unreleased and so is all the internal code that was using `pool_key`.

Co-authored-by: John Crepezzi <john.crepezzi@gmail.com>
2020-08-07 13:43:18 -04:00
eileencodes
9289232bc9
Remove unnecessary with_temporary_connection_pool calls
While debugging a different problem I'm working on I realized that this
method `with_temporary_connection_pool` isn't necessary in most of the
cases we're using it for.

Anywhere we establish new connections inside the block won't throw away
those new connections. I also removed this from places that can use the
existing connection and don't need a new temporary pool. I'm not sure if
this file was using it in many places because of copy / paste or real
issues that are no longer present.
2020-08-07 13:36:03 -04:00
Ryuta Kamizono
0133428ffa
Merge pull request #39998 from kamipo/fix_enum_deserialize_mapping_nil
Fix deserializing enum mapping nil
2020-08-07 14:03:32 +09:00
Ryuta Kamizono
10b36e81a3 Fix incorrect result when eager loading with duplicated through association with join scope
I had found the issue while working on fixing #33525.

That is if duplicated association has a scope which has `where` with
explicit table name condition (e.g. `where("categories.name": "General")`),
that condition in all duplicated associations will filter the first one
only, other all duplicated associations are not filtered, since
duplicated joins will be aliased except the first one (e.g.
`INNER JOIN "categories" "categories_categorizations"`).

```ruby
class Author < ActiveRecord::Base
  has_many :general_categorizations, -> { joins(:category).where("categories.name": "General") }, class_name: "Categorization"
  has_many :general_posts, through: :general_categorizations, source: :post
end

authors = Author.eager_load(:general_categorizations, :general_posts).to_a
```

Generated eager loading query:

```sql
SELECT "authors"."id" AS t0_r0, ... FROM "authors"

-- `has_many :general_categorizations, -> { joins(:category).where("categories.name": "General") }`
LEFT OUTER JOIN "categorizations" ON "categorizations"."author_id" = "authors"."id"
INNER JOIN "categories" ON "categories"."id" = "categorizations"."category_id" AND "categories"."name" = ?

-- `has_many :general_posts, through: :general_categorizations, source: :post`
---- duplicated `through: :general_categorizations` part
LEFT OUTER JOIN "categorizations" "general_categorizations_authors_join" ON "general_categorizations_authors_join"."author_id" = "authors"."id"
INNER JOIN "categories" "categories_categorizations" ON "categories_categorizations"."id" = "general_categorizations_authors_join"."category_id" AND "categories"."name" = ? -- <-- filtering `"categories"."name" = ?` won't work
---- `source: :post` part
LEFT OUTER JOIN "posts" ON "posts"."id" = "general_categorizations_authors_join"."post_id"
```

Originally eager loading with join scope didn't work before Rails 5.2
(#29413), and duplicated through association with join scope raised a
duplicated alias error before alias tracking is improved in 590b045.

But now it will potentially be got incorrect result instead of an error,
it is worse than an error.

To fix the issue, it makes eager loading to deduplicate / re-use
duplicated through association if possible, like as `preload`.

```sql
SELECT "authors"."id" AS t0_r0, ... FROM "authors"

-- `has_many :general_categorizations, -> { joins(:category).where("categories.name": "General") }`
LEFT OUTER JOIN "categorizations" ON "categorizations"."author_id" = "authors"."id"
INNER JOIN "categories" ON "categories"."id" = "categorizations"."category_id" AND "categories"."name" = ?

-- `has_many :general_posts, through: :general_categorizations, source: :post`
---- `through: :general_categorizations` part is deduplicated / re-used
LEFT OUTER JOIN "posts" ON "posts"."id" = "categorizations"."post_id"
```

Fixes #32819.
2020-08-07 13:39:30 +09:00
Ryuta Kamizono
afeb756828 Fix deserializing enum mapping nil
Follow up to #38086.

User assigned nil is type casted by #38086, but loaded nil from database
does not yet, this fixes that.
2020-08-07 04:33:21 +09:00
Ryuta Kamizono
7793244689
Merge pull request #39996 from savin-alexey/faster-pluck
Make `Enumerable.pluck` faster for single key
2020-08-07 04:17:39 +09:00
Ryuta Kamizono
e8cf45dc4a Don't use arel factory methods for creating join nodes
Directly `klass.new` is clear enough than factory methods.
2020-08-07 03:57:34 +09:00
Alexey Savin
80f59eadb4 Make Enumerable.pluck faster for single key 2020-08-06 21:22:20 +03:00
Jonathan Hefner
ab0e39c231
Merge pull request #39995 from assain/fix-old-github-link-and-documentation-in-rails-api
Update GitHub gist api link and fix documentation in api_app guide [ci skip]
2020-08-06 11:27:10 -05:00
assain
0f4de21b57 Update GitHub gist api link and fix documentation in api_app page [ci skip]
The GitHub gist API page is out of date. This commit replaces it with
the new link.

Also, removed unnecessary commas, added missing fullstop and fixed
a ruby snippet which wasn't rendered correctly before.
2020-08-06 21:47:45 +05:30
Eileen M. Uchitelle
8654891cc7
Merge pull request #39988 from Shopify/unused-registry-connection-handler
Get rid of the unused RuntimeRegistry.connection_handler
2020-08-05 09:22:05 -04:00
Jean Boussier
631925454e Get rid of the unused RuntimeRegistry.connection_handler
It's no longer used since 5ce3e022ef
2020-08-05 14:16:56 +02:00
Eugene Kenny
e53f5ff730
Merge pull request #39851 from piecehealth/engine_skip_mailers
--skip-action-mailer option doesn't work for "rails plugin new"
2020-08-04 20:13:19 +01:00
Eileen M. Uchitelle
8de10f9bb6
Merge pull request #39758 from tgxworld/create_own_connection_handler_for_advisory_locks
Move advisory locks to own connection handler.
2020-08-04 09:27:04 -04:00
Michael Hagar
7f11ffbcfb Fix noun-verb agreement in ASt guide 2020-08-04 00:33:40 -04:00
Lawrence Chou
eb5f5ed0be
Fix attaching blobs via nested attributes
Closes #37411.
2020-08-04 00:26:27 -04:00
piecehealth
1f2d0120c0 --skip-action-mailer and --skip-active_job option doesn't work for rails plugin new project_name --full or rails plugin new project_name --mountable 2020-08-04 10:48:34 +08:00
Guo Xiang Tan
45add34478
Move advisory locks to own connection handler.
Removes the use of `ActiveRecord::AdvisoryLockBase` since it inherits
from `ActiveRecord::Base` and hence share module attributes that are defined in `ActiveRecord::Base`.
This is problematic because establishing connections through
`ActiveRecord::AdvisoryLockBase` can end up changing state of the default
connection handler of `ActiveRecord::Base` leading to unexpected
behaviors in a Rails application.

In the case of https://github.com/rails/rails/issues/39157,

Running migrations with `rails db:migrate:primary_shard_one` was not working as
the application itself defined the following

```
class ApplicationRecord < ActiveRecord::Base
  self.abstract_class = true

  connects_to shards: {
    default: { writing: :primary },
    shard_one: { writing: :primary_shard_one }
  }
end
```

In the database migrate rake task, the default connection was
established with the database config of `primary_shard_one`. However,
the default connection was altered to that of `primary` because
`ActiveRecord::AdvisoryLockBase.establish_connection` ended up loading
`ApplicationRecord` which calls `connects_to shards:`. Since all we
really need here is just a normal database connection, we can avoid
accidentally altering the default connection handler state during the migration
by creating a custom connection handler used for retrieving a connection.
2020-08-04 10:17:41 +08:00
Eugene Kenny
b39ea7cc36 Add tiff and bmp to content_types_allowed_inline [ci skip]
These formats were added in c329d323fc2cc9ba1c486932556afd8fbd9333cc and
bcf370d689673031073ba2ac5588afe41cc315c9, respectively.
2020-08-03 21:51:07 +01:00
Eugene Kenny
5baed70b35
Merge pull request #39948 from victorperez/fix-variant-test
fix misleading variant test
2020-08-03 21:21:10 +01:00
Eugene Kenny
7c32f09c4a
Merge pull request #39825 from manuelmeurer/improve-active-record-validations-docs
improve active record validation docs [ci skip]
2020-08-03 14:13:56 +01:00
Eileen M. Uchitelle
fb30d1c513
Merge pull request #39969 from abhaynikam/fix-issue-with-pluralize-database-abstract-class-name
The abstract parent class file generated via generator should not be pluralized
2020-08-03 08:44:24 -04:00
Manuel Meurer
02a93f6afd
improve active record validation docs 2020-08-03 12:18:47 +02:00
Ryuta Kamizono
afb65390c1
Merge pull request #39967 from kamipo/fix_or_with_sti_relation
Avoid implicit `create_with` for `StiClass.all`
2020-08-03 19:14:36 +09:00
Abhay Nikam
b9879bb8ad The abstract parent class file generated via generator should not be pluralized
Currently, the file generated via the generator is pluralized
but the parent class is singluar.

example: bundle exec rails g scaffold Pet name:string --database=animals

The above command should generate: apps/models/animals_record.rb

but the pets model would inherit from: `AnimalRecord` as
`"animals".classify` would be `Animal`

This will throw the `uninitialized constant AnimalRecord Did you mean? AnimalsRecord`
error.
2020-08-02 22:25:57 +05:30
Matthew Draper
843898c57a
Merge pull request #22610 from KevinSjoberg/feature/array-member-inclusion
Validate inclusion of each object in an array
2020-08-03 00:09:35 +09:30
Ryuta Kamizono
bdaf273cbe Avoid implicit create_with for StiClass.all
Regardless of whether doing implicit `create_with` or not, `sti_name` is
set by `ensure_proper_type`.

11f54e12b9/activerecord/lib/active_record/inheritance.rb (L289-L304)

The purpose of the `create_with` is to suppress/override `type_condition`,
since `type_condition` will be an array of all sti subclass names for
`scope_for_create`, it is not desired behavior for `scope_for_create`,
and it will cause `find_sti_class` to fail.

That undesired behavior was derived from `In` arel node was accidentally
a subclass of `Equality` node, IMHO it is considered as almost a bug.
But someone depends on the behavior for now (see also #39288).

Unfortunately that implicit `create_with` had an unwanted side effect to
fail `or` with STI subclass relation #39956.

This changes a way to suppress/override `type_condition`, from doing
implicit `create_with` to excepting `type_condition` in `scope_for_create`,
to fix the `or` issue #39956.
2020-08-01 23:02:50 +09:00
Ryuta Kamizono
11f54e12b9
Merge pull request #39965 from abhaynikam/fix-the-typo-in-migration-multi-db-doc
Fixed the column type typo in the scaffold command for multi-database guide [ci skip]
2020-08-01 19:02:51 +09:00
Abhay Nikam
5392125fb7 Fixed the column type typo in the scaffold command for multi-database guide [ci skip] 2020-08-01 15:24:09 +05:30
Eugene Kenny
c6859c40eb
Merge pull request #39960 from jonathanhefner/plugin-useful-test
Load plugin VERSION constant by default
2020-07-31 22:25:46 +01:00
Jonathan Hefner
f24347031f Load plugin VERSION constant by default
For example, for a plugin like `my_plugin`, this makes the
`MyPlugin::VERSION` constant available to all consumers by default.

This commit also replaces the default generated test with a test that
the developer is less likely to delete.
2020-07-31 15:43:34 -05:00
Eugene Kenny
6b359cef39
Merge pull request #39959 from jonathanhefner/plugin-test-class-naming-convention
Follow naming conventions in generated plugin test
2020-07-31 21:31:50 +01:00
Eileen M. Uchitelle
4c8e9a3e08
Merge pull request #39958 from andrehjr/add-migrate-redo-multi-dbs
Adds db:migrate:redo:NAME support for multidbs
2020-07-31 12:27:13 -04:00
Eileen M. Uchitelle
b343a727ec
Merge pull request #39791 from tgxworld/prioritize_db_config_in_current_env
Prioritize db_config for current env when resolving config.
2020-07-31 12:25:27 -04:00
Jonathan Hefner
0098088fc1 Follow naming conventions in generated plugin test
This commit ensures that a generated test file like "foo/bar_test.rb"
defines the class `Foo::BarTest` rather than `Foo::Bar::Test`.
2020-07-31 10:01:48 -05:00
Vipul A M
1d6daaaa56
Merge pull request #39735 from lulalala/doc-errors-update
Document ActiveModel errors methods
2020-07-31 08:54:08 +05:30