Commit Graph

84108 Commits

Author SHA1 Message Date
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 7e499b25acd5e1bfdd54ca2af66678b0ed05def1.
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
Rafael Mendonça França
0f7b27cded
Merge pull request #45518 from fatkodima/pg-schemas-improvements
Improve working with PostgreSQL schemas
2022-07-04 14:10:26 -04:00
Rafael Mendonça França
77f97d8eef
Merge pull request #45515 from fatkodima/remove_check_constraint-symbol
Fix remove_check_constraint by symbol name
2022-07-04 14:00:19 -04:00
Rafael Mendonça França
e445f48cbb
Merge pull request #45513 from skipkayhil/rm-legacy-connection-guide
Remove docs for legacy_connection_handling [ci skip]
2022-07-04 13:57:43 -04:00
Rafael Mendonça França
9ef41cd548
Merge pull request #45512 from skipkayhil/rm-aj-false-on-aborted-enqueue
Remove return_false_on_aborted_enqueue again
2022-07-04 13:56:06 -04:00
fatkodima
a4d1aceeba Improve working with PostgreSQL schemas 2022-07-04 20:35:23 +03:00
fatkodima
8103d611f4 Fix remove_check_constraint by symbol name 2022-07-04 15:38:52 +03:00
Hartley McGuire
0348b219cf
Remove docs for legacy_connection_handling
The config was fully removed in ad52c0a but this was missed
2022-07-03 20:12:03 -04:00
Hartley McGuire
a0030bf44f
Remove return_false_on_aborted_enqueue again
This was supposed to be removed in 7c788e91 but only the deprecation was
actually removed.
2022-07-03 16:23:04 -04:00