Commit Graph

84116 Commits

Author SHA1 Message Date
Jonathan Hefner 915776ad04 Generate secret_key_base for all new credentials
Currently, when `config/credentials.yml.enc` is generated by
`CredentialsGenerator`, it includes a `secret_key_base` for convenience.
However, because `config/credentials/#{environment}.yml.enc` files are
generated by a different generator (`EncryptedFileGenerator`), they do
not include a `secret_key_base`.

This commit revises `CredentialsGenerator` to be more generator-like,
and changes `rails credentials:edit` to use it for generating both
`config/credentials.yml.enc` and `config/credentials/#{environment}.yml.enc`
files, thereby always including a `secret_key_base`.
2022-07-11 15:36:54 -05:00
Jonathan Hefner 189356bd24
Merge pull request #45563 from diegomichel/fixes-rubydoc-info-links
Fixes rubydoc links [ci-skip]
2022-07-11 14:19:29 -05:00
Diego Michel eeb14214fd Fixes rubydoc links
Uses gem version of documentation instead of repo version

Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2022-07-11 12:58:12 -06:00
Yasuo Honda a3475f07cc
Merge pull request #45559 from skipkayhil/fix-missing-changelog-authors
Add missing CHANGELOG authors
2022-07-11 15:31:19 +09:00
Hartley McGuire 5458571254
Add missing CHANGELOG authors
Ref 44a2971
Ref 9f0b8eb
2022-07-11 01:43:32 -04:00
Yasuo Honda 2cf8f37d50
Merge pull request #44601 from ghiculescu/time-zone-aware-type-postgres
Add `timestamptz` as a time zone aware type for PostgreSQL
2022-07-11 08:23:04 +09:00
Jonathan Hefner a72a65bc49
Merge pull request #45557 from ghiculescu/patch-10
Missing author on changelog entry for #44189 [ci-skip]
2022-07-10 15:53:00 -05:00
Alex Ghiculescu 2f52610c49
Missing author on changelog entry for #44189
cc @jonathanhefner
2022-07-10 15:33:50 -05:00
Alex Ghiculescu 75c406d774 Make `timestamptz` a time zone aware type for Postgres
https://github.com/rails/rails/pull/41395 added support for the `timestamptz` type on the Postgres adapter.

As we found [here](https://github.com/rails/rails/pull/41084#issuecomment-1056430921) this causes issues because in some scenarios the new type is not considered a time zone aware attribute, meaning values of this type in the DB are presented as a `Time`, not an `ActiveSupport::TimeWithZone`.

This PR fixes that by ensuring that `timestamptz` is always a time zone aware type, for Postgres users.
2022-07-10 15:32:18 -05:00
Petrik de Heus 81d6012f4f
Merge pull request #45538 from hexdevs/custom-validator-loaded-note
Clarify how `EachValidator` is automatically loaded [ci-skip]
2022-07-10 10:46:36 +02:00
Ryuta Kamizono c3fd9f1776 Remove reverted CHANGELOG entry [ci-skip]
Follow up to #45553.
2022-07-10 10:02:27 +09:00
Stefanni Brasil ecbca0cb1e Make Custom Validators example more clear
The Active Record Validations guide use `Person < ApplicationRecord`
in all examples. When reading about Custom Validators, one of the
examples had a different configuration. By using the same example
everywhere, this change helps the user save time and feel more confident
using the feature.
2022-07-09 17:56:51 -07:00
Jonathan Hefner b9f1d406f6
Merge pull request #45555 from bbenno/main
Fix rdoc for ActiveRecord::Associations::ClassMethods [ci-skip]
2022-07-09 17:22:54 -05:00
Benno Bielmeier d4d3cca23c
Fix rdoc syntax for ActiveRecord::Associations::ClassMethods 2022-07-10 00:01:33 +02:00
Jonathan Hefner b495b4eea1
Merge pull request #45550 from jonathanhefner/rename-urlsafe-to-url_safe
Rename `:urlsafe` option to `:url_safe`
2022-07-09 15:18:41 -05:00
Gannon McGibbon 6f20be9f97
Merge pull request #45553 from gmcgibbon/revert_model_pattern_matching
Revert "Provide pattern matching for ActiveModel"
2022-07-09 01:39:27 -04:00
Gannon McGibbon 17b4b8fd63 Revert "Provide pattern matching for ActiveModel"
This reverts commit 7e499b25ac.
2022-07-09 00:13:23 -04:00
Jonathan Hefner 7094d0fc43 Rename :urlsafe option to :url_safe
Although Ruby provides `Base64.urlsafe_encode64` and
`Base64.urlsafe_decode64` methods, the technical term is "URL-safe",
with "URL" and "safe" as separate words.

For better readability, this commit renames the `:urlsafe` option for
`MessageEncryptor` and `MessageVerifier` to `:url_safe`.
2022-07-08 15:36:24 -05:00
Jonathan Hefner d44e786d21 Add CHANGELOG entry for #44189 [ci-skip] 2022-07-08 14:54:42 -05:00
Gannon McGibbon e240daa6cc
Merge pull request #45528 from gmcgibbon/job_parent_option
Add `--parent` option to job generator to specify parent class of job.
2022-07-07 16:57:35 -04:00
Jonathan Hefner e722b74a92
Merge pull request #45470 from AlexB52/doc-full-message-format
Add Formatting Full Messages with I18n section in ActiveRecordValidations guides [ci-skip]
2022-07-07 13:33:36 -05:00
Alexandre Barret 0dd813c6da Add documentation to customize full_messages on ActiveModel and ActiveRecord
Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>
2022-07-07 13:28:56 -05:00
Jonathan Hefner d525b7848b
Merge pull request #45188 from tiramizoo/include-seconds-in-datetime-local-field
Add include_seconds option to datetime_local_field.
2022-07-07 13:16:24 -05:00
Yasuo Honda cd08e6bcc4
Merge pull request #45537 from fatkodima/clear-memcache-in-tests
Clear cache in MemCacheStore tests
2022-07-07 08:41:33 +09:00
fatkodima faacccdb75 Clear cache in MemCacheStore tests 2022-07-07 00:25:15 +03:00
Wojciech Wnętrzak 2dea9aebf2
Add include_seconds option to datetime_local_field.
This allows to omit seconds part in the input field, by passing `include_seconds: false`

It's a follow up to https://github.com/rails/rails/pull/41728
2022-07-06 22:27:04 +02:00
Jonathan Hefner a6cb4531d5
Merge pull request #45520 from jonathanhefner/app_generator-implied-options
Improve generator implied option handling
2022-07-06 11:26:21 -05:00
Jonathan Hefner 0e77c83189
Merge pull request #45511 from ajporterfield/migration-index-update
Update migration near bottom of Active Record Basic guide [ci-skip]
2022-07-06 11:11:13 -05:00
Jonathan Hefner 54e3f8c381
Merge pull request #45521 from r7kamura/feature/fix-custom-behavior
Fix NoMethodError on custom ActiveSupport::Deprecation behavior
2022-07-06 10:57:57 -05:00
Jean Boussier 4491822643
Merge pull request #45536 from byroot/foreign-key-flaky-test
Fix flaky foreign key test
2022-07-06 16:16:08 +02:00
Jean Boussier 939d4ee7ca
Merge pull request #45531 from djmb/reduce-encrypted-properties-allocations
Fewer object allocations in Encryption::Properties
2022-07-06 16:15:42 +02:00
Jean Boussier c112287974 Fix flaky foreign key test
Fix: https://github.com/rails/rails/pull/45491
2022-07-06 16:04:43 +02:00
Donal McBreen 0279c033e2 Fewer object allocations in Encryption::Properties
`delegate_missing_to` and `Enumerable#find` both allocate objects. When
selecting a large number of encrypted values with can lead to a
significant number of allocations.

```

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  # Activate the gem you are reporting the issue against.
  gem "activerecord", "~> 7.0.0"
  gem "sqlite3"
  gem "benchmark-ips"
end

require "active_record"
require 'benchmark/ips'

ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")

ActiveRecord::Encryption.configure \
  primary_key: "test master key",
  deterministic_key: "test deterministic key",
  key_derivation_salt: "testing key derivation salt",
  support_unencrypted_data: true

ActiveRecord::Schema.define do
  create_table :comments, force: true do |t|
    t.string :message, length: 1000
  end
end

class Comment < ActiveRecord::Base
  encrypts :message
end

srand(123456)
1000.times { Comment.create!(message: 100.times.map { ("A".."Z").to_a.sample }.join) }

if ENV['OPTIMIZED']
  module ActiveRecord
    module EncryptionPropertiesAvoidAllocations
      extend ActiveSupport::Concern

      ALLOWED_VALUE_CLASSES = [String, ActiveRecord::Encryption::Message, Numeric, TrueClass, FalseClass, Symbol, NilClass, Float, Integer]

      if %w{delegation all}.include?(ENV['OPTIMIZED'])
        delegate :each, :[], :key?, to: :data
      end

      if %w{find all}.include?(ENV['OPTIMIZED'])
        # find also involves allocations, so we can cache the results which classes
        # are valid to avoid the call
        def validate_value_type(value)
          unless ALLOWED_VALUE_CLASSES.include?(value.class) || ALLOWED_VALUE_CLASSES.any? { |klass| value.is_a?(klass) }
            raise ActiveRecord::Encryption::Errors::ForbiddenClass, "Can't store a #{value.class}, only properties of type #{ALLOWED_VALUE_CLASSES.inspect} are allowed"
          end
        end
      end
    end
  end

  ActiveRecord::Encryption::Properties.prepend(ActiveRecord::EncryptionPropertiesAvoidAllocations);
end

def allocation_count
  before = GC.stat(:total_allocated_objects)
  yield
  GC.stat(:total_allocated_objects) - before
end

allocated_objects = allocation_count { Comment.pluck(:message) }
puts "Optimization: #{ENV['OPTIMIZED'] || "none"}, allocations: #{allocated_objects}"

Benchmark.ips do |x|
  x.config(time: 30)
  x.report("default")                 { Comment.pluck(:message) }
  x.report("delegation_optimization") { Comment.pluck(:message) }
  x.report("find_optimization")       { Comment.pluck(:message) }
  x.report("all_optimization")        { Comment.pluck(:message) }
  x.hold! "temp_results"
  x.compare!
end
```

Results:
```
$ ruby encryption_properties_benchmark.rb; OPTIMIZED=delegation ruby encryption_properties_benchmark.rb; OPTIMIZED=find ruby encryption_properties_benchmark.rb; OPTIMIZED=all ruby encryption_properties_benchmark.rb
...snip...
Optimization: none, allocations: 72238
Warming up --------------------------------------
             default     6.000  i/100ms
Calculating -------------------------------------
             default     70.643  (± 4.2%) i/s -      2.118k in  30.052046s

...snip...
Optimization: delegation, allocations: 62313
Warming up --------------------------------------
delegation_optimization
                         7.000  i/100ms
Calculating -------------------------------------
delegation_optimization
                         75.785  (± 4.0%) i/s -      2.275k in  30.086061s

Pausing here -- run Ruby again to measure the next benchmark...

Comparison:
delegation_optimization:       75.8 i/s
             default:       70.6 i/s - same-ish: difference falls within error

...snip...
Optimization: find, allocations: 60306
Warming up --------------------------------------
   find_optimization     7.000  i/100ms
Calculating -------------------------------------
   find_optimization     74.179  (± 4.0%) i/s -      2.226k in  30.082991s

Pausing here -- run Ruby again to measure the next benchmark...

Comparison:
delegation_optimization:       75.8 i/s
   find_optimization:       74.2 i/s - same-ish: difference falls within error
             default:       70.6 i/s - same-ish: difference falls within error

...snip...
Optimization: all, allocations: 50315
Warming up --------------------------------------
    all_optimization     8.000  i/100ms
Calculating -------------------------------------
    all_optimization     84.689  (± 5.9%) i/s -      2.528k in  30.008722s

Comparison:
    all_optimization:       84.7 i/s
delegation_optimization:       75.8 i/s - 1.12x  (± 0.00) slower
   find_optimization:       74.2 i/s - 1.14x  (± 0.00) slower
             default:       70.6 i/s - 1.20x  (± 0.00) slower

```

Overall it's about 15% faster with a 30% reduction in allocations.
2022-07-06 14:56:18 +01:00
Jean Boussier 2dfefb5104
Merge pull request #45534 from etiennebarrie/fix-actionpack-test-with-rack-test-2
Fix actionpack test with rack test 2
2022-07-06 15:25:39 +02:00
Étienne Barrié 889886d6ad Allow rack-test >= 2 2022-07-06 14:54:39 +02:00
Aaron Patterson 07be723bc1 Ensure Rails is green with Rack-Test main branch
This commit just ensures we're green with the main branch of rack test.
The changes are things we should have done anyway, and are backwards
compatible with older versions of rack test
2022-07-06 14:54:01 +02:00
Jean Boussier fd1196bd51
Merge pull request #45414 from fatkodima/whole-table-batching
Optimize Active Record batching for whole table iterations
2022-07-06 10:10:10 +02:00
Gannon McGibbon 02444d9267 Add `--parent` option to job generator to specify parent class of job.
Example:

`bin/rails g job process_payment --parent=payment_job` generates:

```ruby
class ProcessPaymentJob < PaymentJob
  # ...
end
```
2022-07-05 20:55:38 -04:00
Ryo Nakamura 6f9bb2cc0f Fix NoMethodError on custom ActiveSupport::Deprecation behavior
with some additional changes made later:

1. Flip the condition for backward compatibility
   Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>

2. Improve custom behavior test
   Co-authored-by: Jonathan Hefner <jonathan@hefner.pro>

3. Fix indentation
2022-07-06 06:41:41 +09:00
fatkodima 620f247829 Optimize Active Record batching for whole table iterations 2022-07-06 00:21:39 +03:00
Andrew Porterfield d0f07d7abd Update index in migration at bottom of Active Record Basic guide 2022-07-05 17:06:14 -04:00
Jonathan Hefner 4c67975c9c Improve generator implied option handling
This commit improves handling of implied options for `AppGenerator` and
`PluginGenerator` in a few different ways.

Implied options are now reported:

```console
$ rails new my_cool_app --skip-active-job
Based on the specified options, the following options will also be activated:

  --skip-action-mailer [due to --skip-active-job]
  --skip-active-storage [due to --skip-active-job]
  --skip-action-mailbox [due to --skip-active-storage]
  --skip-action-text [due to --skip-active-storage]

...
```

Option conflicts raise an error:

```console
$ rails new my_cool_app --skip-active-job --no-skip-active-storage
Based on the specified options, the following options will also be activated:

  --skip-action-mailer [due to --skip-active-job]
  --skip-active-storage [due to --skip-active-job]
    ERROR: Conflicts with --no-skip-active-storage
  --skip-action-mailbox [due to --skip-active-storage]
  --skip-action-text [due to --skip-active-storage]

railties/lib/rails/generators/app_base.rb:206:in `report_implied_options': Cannot proceed due to conflicting options (RuntimeError)
```

Meta option (e.g. `--minimal`) implications are also reported:

```console
$ rails new my_cool_app --minimal
Based on the specified options, the following options will also be activated:

  --skip-active-job [due to --minimal]
  --skip-action-mailer [due to --skip-active-job, --minimal]
  --skip-active-storage [due to --skip-active-job, --minimal]
  --skip-action-mailbox [due to --skip-active-storage, --minimal]
  --skip-action-text [due to --skip-active-storage, --minimal]
  --skip-javascript [due to --minimal]
  --skip-hotwire [due to --skip-javascript, --minimal]
  --skip-action-cable [due to --minimal]
  --skip-bootsnap [due to --minimal]
  --skip-dev-gems [due to --minimal]
  --skip-system-test [due to --minimal]

...
```

`--no-*` options now work with meta options, and are both comprehensive
and precise:

```console
$ rails new my_cool_app --minimal --no-skip-active-storage
Based on the specified options, the following options will also be activated:

  --skip-action-mailer [due to --minimal]
  --skip-action-mailbox [due to --minimal]
  --skip-action-text [due to --minimal]
  --skip-javascript [due to --minimal]
  --skip-hotwire [due to --skip-javascript, --minimal]
  --skip-action-cable [due to --minimal]
  --skip-bootsnap [due to --minimal]
  --skip-dev-gems [due to --minimal]
  --skip-system-test [due to --minimal]

...
```

Closes #45223.
Closes #45205.

Co-authored-by: Brad Trick <btrick@appacademy.io>
2022-07-05 15:16:28 -05:00
Eileen M. Uchitelle 19f9922523
Merge pull request #45461 from fatkodima/sqlite3-index-trailing-comment
Fix retrieving sqlite3 expression indexes created by sql annotated with trailing comment
2022-07-05 14:59:45 -04:00
Eileen M. Uchitelle 45eb2958a7
Merge pull request #45526 from eileencodes/remove-db-config-owner-name
Remove owner_name from db_config
2022-07-05 14:05:11 -04:00
eileencodes 4a95e9aa5f
Remove owner_name from db_config
The majority of where this functionality was removed in #38672.
`owner_name` was never used publicaly in the db_config and was at a time
used for identifying connection owners. Now the connection owner is
always stored in the PoolConfig (same place as the db_config) so the
db_config doesn't need this information.
2022-07-05 13:33:32 -04:00
Jonathan Hefner c0f71dac70
Merge pull request #45501 from ghiculescu/same-site-false
Allow opting out of the `SameSite` cookie attribute when setting a cookie
2022-07-05 11:06:45 -05:00
Eileen M. Uchitelle b70947bcf6
Merge pull request #45525 from sushantmittal/fix-flaky-test-cases
Fix flaky test cases in file 'activerecord/test/cases/relation/merging_test.rb'.
2022-07-05 09:13:58 -04:00
Sushant Mittal c265974538 Fixed flaky test cases in 'test/cases/relation/merging_test.rb'. 2022-07-05 16:54:50 +05:30
Jean Boussier b3175b953c
Merge pull request #37944 from vlado/with_cte
Common Table Expression support added "out-of-the-box"
2022-07-05 08:39:29 +02:00
Rafael Mendonça França dadf171db8
Merge pull request #45508 from malis/patch-3
Reading form's method by getAttribute instead of form.method
2022-07-04 15:33:28 -04:00