Commit Graph

87458 Commits

Author SHA1 Message Date
fatkodima
cabc1842b7 Make increment_counter/decrement_counter accept an amount argument 2023-05-05 12:18:31 +03:00
Jonathan Hefner
490804f7d5
Merge pull request #48125 from jonathanhefner/message_pack-cache-serializer-silence-warning
Silence msgpack warnings when detecting format
2023-05-04 16:37:32 -05:00
Jonathan Hefner
c94760cc0b
Merge pull request #48122 from jonathanhefner/cache-serializer_with_fallback-string-bypass
Improve cache performance for bare string values
2023-05-04 16:37:06 -05:00
Jonathan Hefner
daa0cb80db Improve cache performance for bare string values
This commit introduces a performance optimization for cache entries with
bare string values such as view fragments.

A new `7.1` cache format has been added which includes the optimization,
and the `:message_pack` cache format now includes the optimization as
well.  (A new cache format is necessary because, during a rolling
deploy, unupgraded servers must be able to read cache entries from
upgraded servers, which means the optimization cannot be enabled for
existing apps by default.)

New apps will use the `7.1` cache format by default, and existing apps
can enable the format by setting `config.load_defaults 7.1`.  Cache
entries written using the `6.1` or `7.0` cache formats can be read when
using the `7.1` format.

**Benchmark**

  ```ruby
  # frozen_string_literal: true
  require "benchmark/ips"

  serializer_7_0 = ActiveSupport::Cache::SerializerWithFallback[:marshal_7_0]
  serializer_7_1 = ActiveSupport::Cache::SerializerWithFallback[:marshal_7_1]
  entry = ActiveSupport::Cache::Entry.new(Random.bytes(10_000), version: "123")

  Benchmark.ips do |x|
    x.report("dump 7.0") do
      $dumped_7_0 = serializer_7_0.dump(entry)
    end

    x.report("dump 7.1") do
      $dumped_7_1 = serializer_7_1.dump(entry)
    end

    x.compare!
  end

  Benchmark.ips do |x|
    x.report("load 7.0") do
      serializer_7_0.load($dumped_7_0)
    end

    x.report("load 7.1") do
      serializer_7_1.load($dumped_7_1)
    end

    x.compare!
  end
  ```

  ```
  Warming up --------------------------------------
              dump 7.0     5.482k i/100ms
              dump 7.1    10.987k i/100ms
  Calculating -------------------------------------
              dump 7.0     73.966k (± 6.9%) i/s -    367.294k in   5.005176s
              dump 7.1    127.193k (±17.8%) i/s -    615.272k in   5.081387s

  Comparison:
              dump 7.1:   127192.9 i/s
              dump 7.0:    73966.5 i/s - 1.72x  (± 0.00) slower

  Warming up --------------------------------------
              load 7.0     7.425k i/100ms
              load 7.1    26.237k i/100ms
  Calculating -------------------------------------
              load 7.0     85.574k (± 1.7%) i/s -    430.650k in   5.034065s
              load 7.1    264.877k (± 1.6%) i/s -      1.338M in   5.052976s

  Comparison:
              load 7.1:   264876.7 i/s
              load 7.0:    85573.7 i/s - 3.10x  (± 0.00) slower
  ```

Co-authored-by: Jean Boussier <jean.boussier@gmail.com>
2023-05-04 16:10:22 -05:00
Eileen M. Uchitelle
533161a73d
Merge pull request #48131 from luanzeba/fix_inner_with_raw_connection
Avoid nested `#with_raw_connection` calls in `#select_all`
2023-05-04 13:04:34 -05:00
Eileen M. Uchitelle
7e735451a7
Merge pull request #48126 from nshki/feat/action-mailbox-configured-primary-keys
feat: use configured pk type in Action Mailbox migration
2023-05-04 10:46:29 -05:00
Luan Vieira
ec7f5a1193
Avoid nested #with_raw_connection
At GitHub we're trying to switch over from the GitHub
activerecord-trilogy-adapter to the one in Rails. One difference between
the adapters is that the GitHub one did not have the `#select_all`
method that abandons multi results.

This wouldn't be a problem, except we also have more aggressive query
retries. This led to an issue because the `super` call in `#select_all`
leads to a nested `#with_raw_connection` call. If we experienced
transient connection errors during the query, the inner
`#with_raw_connection` could reconnect leaving the outer block with a
closed connection. In that case, calling `#more_results_exist?` results
in a `Trilogy::ConnectionClosed Attempted to use closed connection`.
That's the scenario described in this comment
7e735451a7/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb (L981-L983)

Moving the call to `super` out of the `#with_raw_connection` block will
avoid the nesting and fix the issue. We prefer that solution over
detecting nested `#with_raw_connections` and preventing reconnects when
nested, since in our case we actually do want to reconnect and retry.

We're hoping this change is ok even though it's not strictly necessary
for general Rails users. We don't believe it has any downsides.
We did not add a test for this case since it's not possible to occur in
Rails itself. It's specific to custom GitHub code.

Co-authored-by: Daniel Colson <composerinteralia@github.com>
2023-05-04 11:43:59 -04:00
Nishiki Liu
1df5ece43b feat: use config pk type in Action Mailbox migration 2023-05-04 08:19:36 -07:00
Jean Boussier
018b2ae377
Merge pull request #48120 from a5-stable/rewhere-with-nil
Allow passing nil to rewhere method
2023-05-04 13:49:51 +02:00
a5-stable
0924095748 allow rewhere to pass nil and return unscope(:where) 2023-05-04 20:31:19 +09:00
Jonathan Hefner
bbedd4f2ba Silence msgpack warnings when detecting format
This silences a "ActiveSupport::MessagePack requires the msgpack gem"
warning that can occur when using a non-`:message_pack` serializer and
trying to detect the format of a serialized cache entry.

`silence_warnings` is a blunt instrument, but this `require` is only for
decoding legacy cache entries that were encoded using `:message_pack`,
if any.  (i.e. If `:message_pack` is currently being used as a
serializer, then `SerializerWithFallback::[]` should have already loaded
`active_support/message_pack`.)  Therefore, it seems less hazardous to
inadvertently silence other warnings that may occur when loading the
file.

Co-authored-by: Alex Ghiculescu <alex@tanda.co>
2023-05-03 21:34:08 -05:00
Jonathan Hefner
8049d7983f
Merge pull request #48104 from jonathanhefner/message_pack-cache-serializer
Support `:message_pack` as a cache serializer format
2023-05-03 15:43:30 -05:00
Jonathan Hefner
e2524e574b Support :message_pack as a cache serializer format
This commit adds support for `:message_pack` as an option for
`config.active_support.cache_format_version`.

Cache entries written using the `6.1` or `7.0` formats can be read when
using the `:message_pack` format. Additionally, cache entries written
using the `:message_pack` format can now be read when using the `6.1` or
`7.0` format. These behaviors makes it easy to migrate between formats
without invalidating the entire cache.
2023-05-03 14:22:20 -05:00
Eileen M. Uchitelle
dae86240a2
Merge pull request #47892 from ghiculescu/check-pending-crash
Fix error in `ActiveRecord::Migration.check_pending!`
2023-05-03 12:23:43 -05:00
Eileen M. Uchitelle
5947ec2d06
Merge pull request #48112 from adrianna-chang-shopify/ac-shared-mysql-db-statements
Clean up shared DB statements code between Mysql2 and Trilogy
2023-05-03 11:52:07 -05:00
Eileen M. Uchitelle
d7d595bc2a
Merge pull request #48121 from shivamsinghchahar/fix-typo-aj-serializers-test
Fix typo in activejob tests (know -> known)
2023-05-03 10:47:25 -05:00
Shivam Chahar
8ccf129ea1 Fix typo in activejob tests
changes `know` -> `known` in activejob serializers test
2023-05-03 20:38:48 +05:30
Adrianna Chang
630ddc707e
Move shared database logic to MySQL::DatabaseStatements 2023-05-03 10:11:27 -04:00
Eileen M. Uchitelle
6cee8343f1
Merge pull request #48101 from joshuay03/fix-broken-includes-order-ids
[Fix #48080] Use `group` over `distinct` in `ActiveRecord::Calculations#ids` with `includes(...).order(...)`
2023-05-03 08:33:09 -05:00
Jean Boussier
b130a169f5
Merge pull request #48116 from Shopify/update-pg-fix-segfault
Update pg gem to 1.5.3
2023-05-03 09:41:36 +02:00
Jean Boussier
1794bc3437 Update pg gem to 1.5.3
Ref: https://github.com/ged/ruby-pg/issues/530

Fix segfault witnessed in https://buildkite.com/rails/rails/builds/96048#0187c9f9-fafa-4795-a52f-163071126d61
2023-05-03 09:16:51 +02:00
Joshua Young
e856d7f1a2 [Fixes #48080] Fix broken distinct in ActiveRecord::Calculations#ids with includes(...).order(...) 2023-05-03 11:07:23 +10:00
Jean Boussier
10285e977b
Merge pull request #48111 from adrianna-chang-shopify/ac-trilogy-translate-error-use-match
Use `#include?` with String instead of Regexp for Trilogy conn errors
2023-05-02 20:46:26 +02:00
Adrianna Chang
11cc54dd25
Use String instead of Regexp when checking #include? for Trilogy conn errors 2023-05-02 14:25:09 -04:00
Jean Boussier
580ad7e063
Merge pull request #48108 from adrianna-chang-shopify/ac-remove-abstract-mysql-adapter-test
Remove `AbstractMysqlAdapterTest`
2023-05-02 15:46:15 +02:00
Adrianna Chang
abe0b123bc
Remove AbstractMysqlAdapterTest
This test was added in 8f5095a to ensure that there was coverage
for `AbstractMysqlAdapter#execute`, but 63c0d6b refactored `#execute`
to be defined at the Abstract adapter level, and rely on concrete MySQL
adapters to implement `#raw_execute`.

This test won't pass without having the `ExampleMysqlAdapter` implement
`#raw_execute`, but this test is obsolete now that AbstractMysql2Adapter
doesn't implement `#execute` directly.
2023-05-02 09:15:01 -04:00
Adrianna Chang
93b5fc1f95
Rename MySQL::DatabaseStatements => Mysql2::DatabaseStatements 2023-05-02 09:08:25 -04:00
Jean Boussier
62b163151e
Merge pull request #48107 from Mangara/mangara-fix-logging-queue-adapter-name
Improve queue adapter name extraction for logs
2023-05-02 14:54:36 +02:00
Sander Verdonschot
e41d1acc91
Improve queue adapter name extraction for logs
We recently made improvements to the name extraction for `MyJob.queue_adapter_name` (see https://github.com/rails/rails/pull/47995 and https://github.com/rails/rails/pull/48003). I had overlooked that we also extract the queue adapter name in a similar (but not identical) way for log messages, so this change aligns the two.
2023-05-02 08:37:53 -04:00
Jean Boussier
8bdf5e3557
Merge pull request #48106 from Shopify/deep-dup-modules
`Object#deep_dup` no longer duplicate named classes and modules.
2023-05-02 11:25:03 +02:00
Jean Boussier
2fd61a97c8 Add a test for querying serialized attributes containing Module
Extracted from https://github.com/rails/rails/pull/47352
2023-05-02 10:59:12 +02:00
Jean Boussier
e54ef0ab14 Object#deep_dup no longer duplicate named classes and modules.
It makes very little sense to duplicate named classes and modules.

Before:

```ruby
hash = { class: Object, module: Kernel }
hash.deep_dup # => {:class=>#<Class:0x00000001063ffc80>, :module=>#<Module:0x00000001063ffa00>}
```

After:

```ruby
hash = { class: Object, module: Kernel }
hash.deep_dup # => {:class=>Object, :module=>Kernel}
```
2023-05-02 10:54:46 +02:00
Jean Boussier
b717a5a0d4 Revert "Merge pull request #47352 from basecamp/ar-freeze-cached"
This reverts commit 2c20f90ebae3fdd4d7e3351aeaffc4ad2c472069, reversing
changes made to 912096d4ce930b8e7e5d91e0c86bae2091fda0e4.
2023-05-02 10:53:14 +02:00
Jean Boussier
2c20f90eba Merge pull request #47352 from basecamp/ar-freeze-cached
Fix querying of serialized Class attributes

Fix: #47352
2023-05-02 10:11:36 +02:00
Jorge Manrubia
f195e033ac Freeze casted values instead of original values in database queries
This deals with a problem introduced in #7743ab95b8e15581f432206245c691434a3993d1a751b9d451170956d59457a9R8
that was preventing query `Class` serialized attributes. Duplicating the original
`Class` argument generates an anonymous class that can't be serialized as YAML.

This change makes query attributes hasheable based on their frozen casted values
to prevent the problem.

This solution is based on an idea by @matthewd from https://github.com/rails/rails/issues/47338#issuecomment-1424402777.
2023-05-02 10:09:20 +02:00
Yasuo Honda
912096d4ce
Merge pull request #48081 from shivamsinghchahar/fix-flaky-test-in-ar-token-for
Fix flaky test in AR - token_for_test
2023-04-29 07:21:28 +09:00
Yasuo Honda
2a7cd759be
Merge pull request #48085 from wpank84/patch-1
Update migration examples to showcase database-agnostic raw SQL execution
2023-04-29 07:14:13 +09:00
wpankonien
11efa0445d Update migration example to showcase database-agnostic raw SQL execution 2023-04-28 10:04:13 -05:00
Shivam Chahar
e0ad2e7962 Fix flaky test in AR - token for
pass time arg to @user.touch
2023-04-28 16:40:00 +05:30
Yasuo Honda
cc359c077f
Merge pull request #48019 from agrobbin/postgresql-exclusion-constraints-guides
Add a section on exclusion constraints to the AR PostgreSQL guide
2023-04-28 08:48:09 +09:00
John Hawthorn
9eab88a557
Merge pull request #48084 from aharpole/trilogy-collation-compatibility
In older migrations, allow column collation to be explicitly specified
2023-04-27 12:50:26 -07:00
Aaron Harpole
ac63587cb7 In older migrations, allow column collation to be explicitly specified 2023-04-27 12:21:00 -07:00
Carlos Antonio da Silva
f457544f44 Use the skip forgery protection helper in Active Storage guide
The helper method is essentially a wrapper around skipping the callback,
and it simplifies the doc example.

[ci skip]
2023-04-27 13:58:53 -03:00
Jean Boussier
d5ff2bf222
Merge pull request #48067 from shouichi/document-flash-delete
document AD::FlashHash#delete [skip ci]
2023-04-27 17:04:05 +02:00
Jean Boussier
ce032556c8
Merge pull request #48018 from fatkodima/insert_all-out-of-order-indexes
Allow `insert_all`/`upsert_all` to use indexes with columns not in the same order as in `:unique_by`
2023-04-27 16:58:57 +02:00
fatkodima
45bff1623e Allow insert_all/upsert_all to use indexes with columns not in the same order as in :unique_by 2023-04-27 17:55:41 +03:00
Jean Boussier
976c48779e
Merge pull request #47767 from joshuay03/fix-updated-at-not-updating-in-before-update
Fix #45389 | `updated_at` not updating in `before_update` callback
2023-04-27 16:14:56 +02:00
Jean Boussier
bb4cc27003
Merge pull request #47670 from john-h-k/add-relation-intersect
Add `intersects?` to `Relation`
2023-04-27 16:12:21 +02:00
John Kelly
c837ae624b Add intersects? to Relation
Ruby 3.1 added `intersects?` which is equivalent to `(a & b).any?`. Rubocop added a corresponding cop, `Style/ArrayIntersect`, which transforms the old style to use `intersect?`. Unfortunately as `intersects?` is not delegated on `CollectionProxy`, this leads to false positives that need to be disabled for no good reason other than the fact the method isn't delegated.

This PR add delegation of `intersects?` to `Relation` which fixes this.
2023-04-27 16:09:34 +02:00
Alex Robbin
2d8c3495c3
add a section on exclusion constraints to the AR PostgreSQL guide 2023-04-27 08:18:04 -04:00