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 +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/activerecord_slotted_counters/adapters/mysql_upsert.rb +1 -1
- data/lib/activerecord_slotted_counters/adapters/pg_upsert.rb +1 -1
- data/lib/activerecord_slotted_counters/adapters/rails7_upsert.rb +22 -0
- data/lib/activerecord_slotted_counters/adapters/rails_upsert.rb +16 -4
- data/lib/activerecord_slotted_counters/adapters/sqlite_upsert.rb +1 -1
- data/lib/activerecord_slotted_counters/has_slotted_counter.rb +6 -4
- data/lib/activerecord_slotted_counters/utils.rb +2 -2
- data/lib/activerecord_slotted_counters/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2a233ed9bbeebe36d70a84c813b01d07cf74057709b907d491ac71e688bfc44c
|
4
|
+
data.tar.gz: c93306db04ae60233c9fb0ec6b8a28f1ee75f5bf22e34f503ae462f6858baa07
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ce0bb7f7cdb8fe0c48de169c2e8e94d5977e74cb3840bc4aafbaf181a866e12eb7b06d4ffef603d0768d21ec4b16f7092f2884dd0c1bf1b76d99cd61e71c9353
|
7
|
+
data.tar.gz: 93d093f88f9cae152096dbc7c3e906f4a9c39a2e50db21befeef4b9ab756515bea3149ffa6fdf7391aa2dc16d2e816f9a0819567d25992f517936b872391c135
|
data/CHANGELOG.md
CHANGED
@@ -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
|
-
|
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
|
-
|
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
|
@@ -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"
|
10
|
+
:"#{counter_type}_slotted_counters"
|
11
11
|
end
|
12
12
|
|
13
13
|
def slotted_counter_name(counter_type)
|
14
|
-
"#{counter_type}_count"
|
14
|
+
:"#{counter_type}_count"
|
15
15
|
end
|
16
16
|
|
17
17
|
# TODO refactoring
|
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.
|
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:
|
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.
|
130
|
+
rubygems_version: 3.4.19
|
130
131
|
signing_key:
|
131
132
|
specification_version: 4
|
132
133
|
summary: Active Record slotted counters support
|