random-rails 1.0.1 → 1.0.2

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: 742b88b0a7c5aade166e3bb2d4729fc094955432503683d57aa102a16887cdd9
4
- data.tar.gz: fa97f1b124d647cb874c41f2ffa6f1f3188d817f6e093bc1f081eddc8c646c29
3
+ metadata.gz: 353a1c5d2a49d22d293032ea70253cb1bf711173c01fab5f6d528a0c3ed2661a
4
+ data.tar.gz: 864bb5abd310a796055a5d3a0f3648ad01a2115f8e1eedd9b8be09819b430f72
5
5
  SHA512:
6
- metadata.gz: 37c0f1ebecdde3365155e49d9e60cf5345a0c60880ede8e15de4bf64759e587240a5267cc4b3a1b941a3a7d8ef58a8bf095200a41ac9c5136ba4e2302d5ba16e
7
- data.tar.gz: 33238f0df65b20e6395205ee33834627f47b62f4022a5112a4d46f5cd964558d463a1c0535aa1068a095a2de2151733559a8f216e074b5b616bd3799c6d3e047
6
+ metadata.gz: 8a939b36e74e9bb6904544089ad7f16ab3adb546755b0736ff56e4fdbd47f39213b8914b3c77f25316dd76f8df4b17baed3f7a8b822702aa96234c86621f9867
7
+ data.tar.gz: ddb9675cbca82dd3602272c991c194aef7034e564760cec346e0e3563bad0309d9f0d6e06165d5dcd0e5848063205429ef32ddb2189843dac475d9882cb4ccd8
data/CHANGELOG.md CHANGED
@@ -1,3 +1,32 @@
1
+ ## [1.0.2] - 2025-10-06
2
+
3
+ ### Maintenance Release - Code Quality & CI Improvements
4
+
5
+ #### Fixed
6
+
7
+ - **Floating-point precision bug**: Replaced direct float equality comparison (`precision == 1.0`) with epsilon-based comparison (`(precision - 1.0).abs < Float::EPSILON`) to avoid floating-point precision issues
8
+ - **Code formatting**: Improved code consistency and readability with proper parentheses around ternary operators and aligned variable assignments
9
+
10
+ #### Added
11
+
12
+ - **Enhanced test matrix**: Added Ruby 3.2.9, 3.3.9, and Rails 8.0.3 to CI test matrix for better compatibility testing
13
+ - **Improved .gitignore**: Added missing `*.gem` pattern to prevent accidental gem file commits
14
+
15
+ #### Changed
16
+
17
+ - **Code style improvements**:
18
+ - Added parentheses around ternary operator conditions for better readability
19
+ - Aligned variable assignments for consistent formatting
20
+ - Updated string quotations to use double quotes consistently in specs
21
+ - Removed unused `cache_key` variable
22
+ - Added missing blank lines for better code organization
23
+
24
+ #### Technical Improvements
25
+
26
+ - Better floating-point comparison to prevent precision-related bugs
27
+ - More robust code style following Ruby best practices
28
+ - Enhanced CI coverage with additional Ruby and Rails versions
29
+
1
30
  ## [1.0.1] - 2025-09-30
2
31
 
3
32
  ### Major Release - Multi-Database Support & Performance Optimizations
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- random-rails (1.0.1)
4
+ random-rails (1.0.2)
5
5
  activerecord (>= 4.0, < 8.1)
6
6
  activesupport (>= 6.1.5, < 8.1)
7
7
 
@@ -24,7 +24,7 @@ module RandomRails
24
24
  end
25
25
 
26
26
  # Return single object for count=1, relation for count>1
27
- count == 1 ? relation.take : relation
27
+ (count == 1) ? relation.take : relation
28
28
  end
29
29
 
30
30
  private
@@ -45,7 +45,7 @@ module RandomRails
45
45
  # PostgreSQL supports TABLESAMPLE for large tables, offset for smaller ones
46
46
  estimated_count = estimate_table_size
47
47
 
48
- estimated_count > RandomRails.configuration.tablesample_threshold ? :tablesample : :offset
48
+ (estimated_count > RandomRails.configuration.tablesample_threshold) ? :tablesample : :offset
49
49
  when "mysql", "mysql2"
50
50
  # MySQL doesn't have TABLESAMPLE, use offset method
51
51
  :offset
@@ -62,7 +62,8 @@ module RandomRails
62
62
  def tablesample_random(precision:, count:)
63
63
  if connection.adapter_name.downcase == "postgresql"
64
64
  # Use configured precision if not specified
65
- precision = RandomRails.configuration.precision if precision == 1.0
65
+ # using Float::EPSILON to avoid floating point precision issues
66
+ precision = RandomRails.configuration.precision if (precision - 1.0).abs < Float::EPSILON
66
67
 
67
68
  from("#{table_name} TABLESAMPLE BERNOULLI(#{precision})").limit(count)
68
69
  else
@@ -78,8 +79,8 @@ module RandomRails
78
79
  return limit(count) if total_count == 0
79
80
 
80
81
  # Generate random offset, ensuring we always have an offset clause
81
- max_offset = [total_count - count, 0].max
82
- random_offset = max_offset > 0 ? rand(max_offset + 1) : 0
82
+ max_offset = [total_count - count, 0].max
83
+ random_offset = (max_offset > 0) ? rand(max_offset + 1) : 0
83
84
 
84
85
  # Always apply offset, even if it's 0, to ensure consistent SQL structure
85
86
  offset(random_offset).limit(count)
@@ -101,8 +102,6 @@ module RandomRails
101
102
 
102
103
  # Estimate table size efficiently
103
104
  def estimate_table_size
104
- cache_key = "#{table_name}_count_estimate"
105
-
106
105
  if RandomRails.configuration.cache_table_sizes && @estimated_count
107
106
  return @estimated_count
108
107
  end
@@ -111,13 +110,13 @@ module RandomRails
111
110
  case connection.adapter_name.downcase
112
111
  when "postgresql", "pg"
113
112
  # Use pg_class for fast estimate
114
- sql = "SELECT reltuples::INTEGER FROM pg_class WHERE relname = '#{table_name}'"
113
+ sql = "SELECT reltuples::INTEGER FROM pg_class WHERE relname = '#{table_name}'"
115
114
  result = connection.execute(sql).first
116
115
 
117
116
  result ? result["reltuples"].to_i : count
118
117
  when "mysql", "mysql2"
119
118
  # Use information_schema for fast estimate
120
- sql = "SELECT table_rows FROM information_schema.tables WHERE table_name = '#{table_name}'"
119
+ sql = "SELECT table_rows FROM information_schema.tables WHERE table_name = '#{table_name}'"
121
120
  result = connection.execute(sql).first
122
121
 
123
122
  result ? result[0].to_i : count
@@ -5,10 +5,10 @@ module RandomRails
5
5
  attr_accessor :default_strategy, :tablesample_threshold, :cache_table_sizes, :precision
6
6
 
7
7
  def initialize
8
- @default_strategy = :auto
8
+ @default_strategy = :auto
9
9
  @tablesample_threshold = 10_000 # Use TABLESAMPLE for tables larger than this
10
- @cache_table_sizes = true # Cache table size estimates
11
- @precision = 1.0 # Default precision for TABLESAMPLE
10
+ @cache_table_sizes = true # Cache table size estimates
11
+ @precision = 1.0 # Default precision for TABLESAMPLE
12
12
  end
13
13
  end
14
14
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RandomRails
4
- VERSION = "1.0.1"
4
+ VERSION = "1.0.2"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: random-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - loqimean