estimate_count 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4820f4b135cde08c8d827ebe9232a0b7fba84d33deefb0b866b904b14cddc754
4
- data.tar.gz: 303e049e4e60d36dfbe53b3c58fdbbe3514b9c40c628d79bf42bc779ce30fc15
3
+ metadata.gz: a183b9931539602b197f560cb1e2f3c1a23735e0ac88f3a1bdfe1e7d3055420a
4
+ data.tar.gz: 1399ce07b08369ab9a30e2a92f70598d8e502da7c7505bc1970c331a461d9ff8
5
5
  SHA512:
6
- metadata.gz: 027fe306e6a234b4b3cfd0d810f44990186e5e9e5d59a3e8e5f2ead9acd3e14817729577aad748bb4baccaaafc673cef8fe21d79fbd1cf4ad32e99912c618623
7
- data.tar.gz: 89dd325d30d773c160681a4e48660cc0239113ee9b41cef257292b93f75595644af8afab1677beb8ecc108650b8198275b6ecdf829ccdee164aa03de36d91741
6
+ metadata.gz: 202a74c4860aede412634c0dd9962b24c31663db14970fedd1dde06ff1de9e5fc12fede2d59ca5ca9e54210094721f06f37a22e4a60b45fc3ef3eb0282917e8f
7
+ data.tar.gz: e825b7766d5eb08d63fbd59039f995bb4fb15c82602de9b42f1f0db1d4ffe6327be7b864e47b5ec575f081ad1ac5f354cc36bae9aa5d1d15824c8649d093379d
data/Gemfile CHANGED
@@ -8,3 +8,5 @@ gemspec
8
8
  gem "rake", "~> 13.0"
9
9
 
10
10
  gem "rspec", "~> 3.0"
11
+
12
+ gem "pry"
data/Gemfile.lock CHANGED
@@ -3,27 +3,32 @@ PATH
3
3
  specs:
4
4
  estimate_count (0.1.0)
5
5
  activerecord
6
- pg
7
6
 
8
7
  GEM
9
8
  remote: https://rubygems.org/
10
9
  specs:
11
- activemodel (7.0.3.1)
12
- activesupport (= 7.0.3.1)
13
- activerecord (7.0.3.1)
14
- activemodel (= 7.0.3.1)
15
- activesupport (= 7.0.3.1)
16
- activesupport (7.0.3.1)
10
+ activemodel (7.0.4.3)
11
+ activesupport (= 7.0.4.3)
12
+ activerecord (7.0.4.3)
13
+ activemodel (= 7.0.4.3)
14
+ activesupport (= 7.0.4.3)
15
+ activesupport (7.0.4.3)
17
16
  concurrent-ruby (~> 1.0, >= 1.0.2)
18
17
  i18n (>= 1.6, < 2)
19
18
  minitest (>= 5.1)
20
19
  tzinfo (~> 2.0)
21
- concurrent-ruby (1.1.10)
20
+ coderay (1.1.3)
21
+ concurrent-ruby (1.2.2)
22
22
  diff-lcs (1.5.0)
23
- i18n (1.12.0)
23
+ i18n (1.13.0)
24
24
  concurrent-ruby (~> 1.0)
25
- minitest (5.16.2)
25
+ method_source (1.0.0)
26
+ minitest (5.18.0)
27
+ mysql2 (0.5.5)
26
28
  pg (1.4.1)
29
+ pry (0.14.2)
30
+ coderay (~> 1.1)
31
+ method_source (~> 1.0)
27
32
  rake (13.0.6)
28
33
  rspec (3.11.0)
29
34
  rspec-core (~> 3.11.0)
@@ -38,17 +43,22 @@ GEM
38
43
  diff-lcs (>= 1.2.0, < 2.0)
39
44
  rspec-support (~> 3.11.0)
40
45
  rspec-support (3.11.0)
41
- tzinfo (2.0.4)
46
+ tzinfo (2.0.6)
42
47
  concurrent-ruby (~> 1.0)
43
48
 
44
49
  PLATFORMS
50
+ arm64-darwin-21
45
51
  arm64-darwin-22
52
+ ruby
53
+ x86_64-linux
46
54
 
47
55
  DEPENDENCIES
48
- activerecord
49
56
  estimate_count!
57
+ mysql2
58
+ pg
59
+ pry
50
60
  rake (~> 13.0)
51
61
  rspec (~> 3.0)
52
62
 
53
63
  BUNDLED WITH
54
- 2.3.26
64
+ 2.3.10
@@ -0,0 +1,27 @@
1
+ version: "3"
2
+
3
+ services:
4
+ mysql:
5
+ image: mysql:8.0
6
+ environment:
7
+ MYSQL_ROOT_PASSWORD: root
8
+ MYSQL_DATABASE: test
9
+ MYSQL_USER: test
10
+ MYSQL_PASSWORD: test
11
+ ports:
12
+ - 3306:3306
13
+ volumes:
14
+ - ./db/mysql:/var/lib/mysql
15
+ healthcheck:
16
+ test: [ "CMD", "mysql", "-u", "root", "-e", "USE test;" ]
17
+ retries: 1
18
+ postgresql:
19
+ image: postgres:15-alpine
20
+ environment:
21
+ POSTGRES_USER: test
22
+ POSTGRES_PASSWORD: test
23
+ POSTGRES_DB: test
24
+ ports:
25
+ - 5432:5432
26
+ volumes:
27
+ - ./db/postgresql:/var/lib/postgresql/data
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EstimateCount
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -1,18 +1,36 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "estimate_count/version"
4
+ require "active_record"
4
5
 
5
6
  module ActiveRecord
6
7
  class Base
7
8
  def self.estimate_count(threshold: 1000)
8
- full_scope = current_scope.limit(nil).offset(nil)
9
+ full_scope = current_scope&.limit(nil)&.offset(nil) || all
9
10
  rows = estimate_rows(full_scope.to_sql)
10
11
  rows = full_scope.count if threshold.is_a?(Integer) && rows < threshold
11
12
  rows
12
13
  end
13
14
 
14
15
  private_class_method def self.estimate_rows(query)
16
+ case connection.adapter_name
17
+ when "PostgreSQL"
18
+ estimate_rows_postgresql(query)
19
+ when "MySQL", "Mysql2"
20
+ estimate_rows_mysql(query)
21
+ else
22
+ raise "Unsupported database"
23
+ end
24
+ end
25
+
26
+ private_class_method def self.estimate_rows_postgresql(query)
15
27
  connection.execute("EXPLAIN #{query}").to_a.first["QUERY PLAN"].match(/rows=(\d+)/)[1].to_i
16
28
  end
29
+
30
+ private_class_method def self.estimate_rows_mysql(query)
31
+ result = connection.execute("EXPLAIN FORMAT=TRADITIONAL #{query}")
32
+ index = result.fields.index("rows")
33
+ result.first[index].to_i
34
+ end
17
35
  end
18
36
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: estimate_count
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Hasiński
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-04 00:00:00.000000000 Z
11
+ date: 2023-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -31,7 +31,35 @@ dependencies:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
- type: :runtime
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mysql2
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
35
63
  prerelease: false
36
64
  version_requirements: !ruby/object:Gem::Requirement
37
65
  requirements:
@@ -52,6 +80,7 @@ files:
52
80
  - LICENSE.txt
53
81
  - README.md
54
82
  - Rakefile
83
+ - docker-compose.yml
55
84
  - lib/estimate_count.rb
56
85
  - lib/estimate_count/version.rb
57
86
  - sig/estimate_count.rbs
@@ -77,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
106
  - !ruby/object:Gem::Version
78
107
  version: '0'
79
108
  requirements: []
80
- rubygems_version: 3.3.26
109
+ rubygems_version: 3.2.32
81
110
  signing_key:
82
111
  specification_version: 4
83
112
  summary: Adds a method to get an estimate count for an ActiveRecord::Relation