activerecord-slotted_counters 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 76c73b0a3e75e1fbddb355beedf5aa5967f43ffb5043d0051572423ef3a2188c
4
- data.tar.gz: be40038645de42ee29e31c02cdafac846641d079404c65ac12a44726d22dc8fa
3
+ metadata.gz: 2a233ed9bbeebe36d70a84c813b01d07cf74057709b907d491ac71e688bfc44c
4
+ data.tar.gz: c93306db04ae60233c9fb0ec6b8a28f1ee75f5bf22e34f503ae462f6858baa07
5
5
  SHA512:
6
- metadata.gz: e5f7a7077bb02f35124a72417a2d60d2ea8da356a32e12020549fbacdb08d7ea76f2ff913e4fdd79376ab334cfec15221e808e62a76bc2652e779c31a9462ef7
7
- data.tar.gz: a8f3881d903bbb5f032bc8763733d5f3abd770c44a9796cfbec79db171fcb1f0a453d4fe0124907315c662161d3c33a523fd20557fc2419a6c3b3c270323e71b
6
+ metadata.gz: ce0bb7f7cdb8fe0c48de169c2e8e94d5977e74cb3840bc4aafbaf181a866e12eb7b06d4ffef603d0768d21ec4b16f7092f2884dd0c1bf1b76d99cd61e71c9353
7
+ data.tar.gz: 93d093f88f9cae152096dbc7c3e906f4a9c39a2e50db21befeef4b9ab756515bea3149ffa6fdf7391aa2dc16d2e816f9a0819567d25992f517936b872391c135
data/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  ## master
4
4
 
5
+ ## 0.3.0 (2025-03-05)
6
+
7
+ - Rails 8 support. ([@palkan][])
8
+
5
9
  ## 0.2.0 (2023-10-27)
6
10
 
7
11
  - Add Mysql support [#18](https://github.com/evilmartians/activerecord-slotted_counters/pull/18) ([@prog-supdex][])
@@ -5,7 +5,7 @@ module ActiveRecordSlottedCounters
5
5
  class MysqlUpsert
6
6
  attr_reader :klass
7
7
 
8
- def initialize(klass)
8
+ def initialize(klass, **)
9
9
  @klass = klass
10
10
  end
11
11
 
@@ -5,7 +5,7 @@ module ActiveRecordSlottedCounters
5
5
  class PgUpsert
6
6
  attr_reader :klass
7
7
 
8
- def initialize(klass)
8
+ def initialize(klass, **)
9
9
  @klass = klass
10
10
  end
11
11
 
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecordSlottedCounters
4
+ module Adapters
5
+ class Rails7Upsert < RailsUpsert
6
+ def apply?(_)
7
+ ActiveRecord::VERSION::MAJOR == 7 && ActiveRecord::VERSION::MINOR < 2
8
+ end
9
+
10
+ def bulk_insert(attributes, on_duplicate: nil, unique_by: nil)
11
+ opts = {on_duplicate: on_duplicate, unique_by: unique_by}
12
+ opts.delete(:unique_by) unless supports_insert_conflict_target
13
+
14
+ # We have to manually call #update here to return the number of affected rows.
15
+ # In Rails <7.2, connection is obtained internally.
16
+ ActiveRecord::InsertAll.new(klass, attributes, **opts).then do |inserter|
17
+ inserter.send(:connection).update(inserter.send(:to_sql))
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -3,10 +3,11 @@
3
3
  module ActiveRecordSlottedCounters
4
4
  module Adapters
5
5
  class RailsUpsert
6
- attr_reader :klass
6
+ attr_reader :klass, :supports_insert_conflict_target
7
7
 
8
- def initialize(klass)
8
+ def initialize(klass, supports_insert_conflict_target: false)
9
9
  @klass = klass
10
+ @supports_insert_conflict_target = supports_insert_conflict_target
10
11
  end
11
12
 
12
13
  def apply?(_)
@@ -14,11 +15,22 @@ module ActiveRecordSlottedCounters
14
15
  end
15
16
 
16
17
  def bulk_insert(attributes, on_duplicate: nil, unique_by: nil)
17
- klass.upsert_all(attributes, on_duplicate: on_duplicate, unique_by: unique_by).rows.count
18
+ opts = {on_duplicate: on_duplicate, unique_by: unique_by}
19
+ opts.delete(:unique_by) unless supports_insert_conflict_target
20
+
21
+ klass.with_connection do |c|
22
+ # We have to manually call #update here to return the number of affected rows
23
+ c.update(ActiveRecord::InsertAll.new(klass.all, c, attributes, **opts).send(:to_sql))
24
+ end
18
25
  end
19
26
 
20
27
  def wrap_column_name(value)
21
- "EXCLUDED.#{value}"
28
+ # This is mysql
29
+ if !supports_insert_conflict_target
30
+ "VALUES(#{value})"
31
+ else
32
+ "EXCLUDED.#{value}"
33
+ end
22
34
  end
23
35
  end
24
36
  end
@@ -5,7 +5,7 @@ module ActiveRecordSlottedCounters
5
5
  class SqliteUpsert
6
6
  attr_reader :klass
7
7
 
8
- def initialize(klass)
8
+ def initialize(klass, **)
9
9
  @klass = klass
10
10
  end
11
11
 
@@ -4,6 +4,7 @@ require "active_support"
4
4
  require "activerecord_slotted_counters/utils"
5
5
 
6
6
  require "activerecord_slotted_counters/adapters/rails_upsert"
7
+ require "activerecord_slotted_counters/adapters/rails7_upsert"
7
8
  require "activerecord_slotted_counters/adapters/pg_upsert"
8
9
  require "activerecord_slotted_counters/adapters/sqlite_upsert"
9
10
  require "activerecord_slotted_counters/adapters/mysql_upsert"
@@ -44,16 +45,17 @@ module ActiveRecordSlottedCounters
44
45
 
45
46
  def set_slotted_counter_db_adapter
46
47
  available_adapters = [
48
+ ActiveRecordSlottedCounters::Adapters::Rails7Upsert,
49
+ ActiveRecordSlottedCounters::Adapters::RailsUpsert,
47
50
  ActiveRecordSlottedCounters::Adapters::MysqlUpsert,
48
51
  ActiveRecordSlottedCounters::Adapters::SqliteUpsert,
49
- ActiveRecordSlottedCounters::Adapters::PgUpsert,
50
- ActiveRecordSlottedCounters::Adapters::RailsUpsert
52
+ ActiveRecordSlottedCounters::Adapters::PgUpsert
51
53
  ]
52
54
 
53
55
  current_adapter_name = connection.adapter_name
54
56
 
55
57
  adapter = available_adapters
56
- .map { |adapter| adapter.new(self) }
58
+ .map { |adapter| adapter.new(self, supports_insert_conflict_target: connection.supports_insert_conflict_target?) }
57
59
  .detect { |adapter| adapter.apply?(current_adapter_name) }
58
60
 
59
61
  raise NotSupportedAdapter.new(current_adapter_name) if adapter.nil?
@@ -224,7 +226,7 @@ module ActiveRecordSlottedCounters
224
226
  end
225
227
 
226
228
  def touch_attributes(ids, touch)
227
- scope = where(id: ids)
229
+ scope = unscoped.where(id: ids)
228
230
  return scope.touch_all if touch == true
229
231
 
230
232
  scope.touch_all(touch)
@@ -7,11 +7,11 @@ module ActiveRecordSlottedCounters
7
7
  private
8
8
 
9
9
  def slotted_counter_association_name(counter_type)
10
- "#{counter_type}_slotted_counters".to_sym
10
+ :"#{counter_type}_slotted_counters"
11
11
  end
12
12
 
13
13
  def slotted_counter_name(counter_type)
14
- "#{counter_type}_count".to_sym
14
+ :"#{counter_type}_count"
15
15
  end
16
16
 
17
17
  # TODO refactoring
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecordSlottedCounters # :nodoc:
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-slotted_counters
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Egor Lukin
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2023-10-27 00:00:00.000000000 Z
12
+ date: 2025-03-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -94,6 +94,7 @@ files:
94
94
  - lib/activerecord-slotted_counters.rb
95
95
  - lib/activerecord_slotted_counters/adapters/mysql_upsert.rb
96
96
  - lib/activerecord_slotted_counters/adapters/pg_upsert.rb
97
+ - lib/activerecord_slotted_counters/adapters/rails7_upsert.rb
97
98
  - lib/activerecord_slotted_counters/adapters/rails_upsert.rb
98
99
  - lib/activerecord_slotted_counters/adapters/sqlite_upsert.rb
99
100
  - lib/activerecord_slotted_counters/has_slotted_counter.rb
@@ -126,7 +127,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
126
127
  - !ruby/object:Gem::Version
127
128
  version: '0'
128
129
  requirements: []
129
- rubygems_version: 3.4.20
130
+ rubygems_version: 3.4.19
130
131
  signing_key:
131
132
  specification_version: 4
132
133
  summary: Active Record slotted counters support