activerecord-find_each_ordered 0.1.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 +7 -0
- data/.gitignore +12 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +52 -0
- data/Rakefile +6 -0
- data/activerecord-find_each_ordered.gemspec +28 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/active_record/find_each_ordered.rb +56 -0
- data/lib/activerecord/find_each_ordered.rb +5 -0
- metadata +124 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 749762b9588483129fbd592903401c8d09420708d17265df2566bf94c25c5bc5
|
4
|
+
data.tar.gz: 31c93153183fd4a025fda20eec4274586057abe904d04ed693c120cd9fd3e6e3
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c4ee8236887c06b6dbcea2fa79ebe22ebc0e75f7a714e0e055383a521eaec58971a6cdbc71806556f841958087c6b9c687da35ca06a51479f62077e3edc9af57
|
7
|
+
data.tar.gz: 136faf9bf86850edd8708df31ba30651267bdbc8a61bc1952419feac0ab9144ac63ba500d99618cc5e55408796100984cf43169f6fe77c9071c95bc079f2e2da
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Joshua Flanagan
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# ActiveRecord::FindEachOrdered
|
2
|
+
|
3
|
+
Behave's like ActiveRecord's [#find_each](https://api.rubyonrails.org/classes/ActiveRecord/Batches.html#method-i-find_each) method,
|
4
|
+
but allows sorting by a column other than the primary key.
|
5
|
+
|
6
|
+
There are times when you need to load a large number of records, but sorting
|
7
|
+
by the primary key is undesired, or prohibitely expensive. Sorting by the
|
8
|
+
primary key may cause the database planner to avoid an index that would otherwise
|
9
|
+
be more helpful.
|
10
|
+
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
gem 'activerecord-find_each_ordered'
|
18
|
+
```
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install activerecord-find_each_ordered
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
```
|
31
|
+
yesterday = 1.day.ago
|
32
|
+
Item.
|
33
|
+
where(purchased_at: (yesterday.beginning_of_day..yesterday.end_of_day)).
|
34
|
+
find_each_ordered(:purchased_at, batch_size: 500) do |item|
|
35
|
+
|
36
|
+
# work with each item instance
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
## Development
|
41
|
+
|
42
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
43
|
+
|
44
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in the gemspec, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
45
|
+
|
46
|
+
## Contributing
|
47
|
+
|
48
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/joshuaflanagan/activerecord-find_each_ordered.
|
49
|
+
|
50
|
+
## License
|
51
|
+
|
52
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
|
4
|
+
Gem::Specification.new do |spec|
|
5
|
+
spec.name = "activerecord-find_each_ordered"
|
6
|
+
spec.version = "0.1.0"
|
7
|
+
spec.authors = ["Joshua Flanagan"]
|
8
|
+
spec.email = ["joshuaflanagan@gmail.com"]
|
9
|
+
|
10
|
+
spec.summary = %q{Allow custom sort order with find_each behavior}
|
11
|
+
spec.description = %q{Allow custom sort order with find_each behavior}
|
12
|
+
spec.homepage = "https://github.com/joshuaflanagan/activerecord-find_each_ordered"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
# Specify which files should be added to the gem when it is released.
|
16
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
17
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
18
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
end
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "activerecord"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
25
|
+
spec.add_development_dependency "sqlite3"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
28
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "activerecord/find_each_ordered"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module FindEachOrdered
|
3
|
+
# Behaves like find_each, but allows a custom sort order.
|
4
|
+
#
|
5
|
+
# #find_each forces the records to be returned sorted by 'id'.
|
6
|
+
#
|
7
|
+
# Make sure none of the returned records have a NULL value for the sort key,
|
8
|
+
# or this may fail with an UnsortableNullValue error.
|
9
|
+
# If the column is nullable, you may want to add something like:
|
10
|
+
# .where.not(last_name: nil) # assuming a sort_key of :last_name
|
11
|
+
# or other criteria that will ensure no NULL values are encountered.
|
12
|
+
def find_each_ordered(sort_key, batch_size: 1000)
|
13
|
+
return to_enum(:find_each_ordered, sort_key, batch_size: batch_size) unless block_given?
|
14
|
+
relation = self
|
15
|
+
batch_order = arel_attribute(sort_key).asc
|
16
|
+
relation = relation.reorder(batch_order).limit(batch_size)
|
17
|
+
records = relation.to_a
|
18
|
+
|
19
|
+
last_offset = nil
|
20
|
+
seen_ids = []
|
21
|
+
while records.any?
|
22
|
+
records_size = records.size
|
23
|
+
last_record = records.last
|
24
|
+
unless last_record.has_attribute?(sort_key)
|
25
|
+
raise ArgumentError.new("Sort key '#{sort_key}' not included in the custom select clause")
|
26
|
+
end
|
27
|
+
batch_offset = last_record.public_send(sort_key)
|
28
|
+
if batch_offset.nil?
|
29
|
+
raise UnsortableNullValue, "#{last_record.class} with id #{last_record.id} has a NULL value for sort_key '#{sort_key}'"
|
30
|
+
end
|
31
|
+
if batch_offset != last_offset
|
32
|
+
seen_ids.clear
|
33
|
+
end
|
34
|
+
last_offset = batch_offset
|
35
|
+
# since more than one order could have the same sort_key value, we
|
36
|
+
# need to exclude the ones we've seen from the next query
|
37
|
+
new_excludes = records.select{|r|
|
38
|
+
r.public_send(sort_key) == batch_offset
|
39
|
+
}.map(&:id)
|
40
|
+
seen_ids.concat(new_excludes)
|
41
|
+
|
42
|
+
records.each{|r| yield r}
|
43
|
+
|
44
|
+
break if records_size < batch_size
|
45
|
+
|
46
|
+
records = relation.where(
|
47
|
+
relation.table[sort_key].gteq(batch_offset)
|
48
|
+
).where.not(id: seen_ids).to_a
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
class UnsortableNullValue < StandardError; end
|
53
|
+
end
|
54
|
+
|
55
|
+
Relation.include(FindEachOrdered)
|
56
|
+
end
|
metadata
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord-find_each_ordered
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joshua Flanagan
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-10-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.17'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.17'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: sqlite3
|
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: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
description: Allow custom sort order with find_each behavior
|
84
|
+
email:
|
85
|
+
- joshuaflanagan@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- activerecord-find_each_ordered.gemspec
|
96
|
+
- bin/console
|
97
|
+
- bin/setup
|
98
|
+
- lib/active_record/find_each_ordered.rb
|
99
|
+
- lib/activerecord/find_each_ordered.rb
|
100
|
+
homepage: https://github.com/joshuaflanagan/activerecord-find_each_ordered
|
101
|
+
licenses:
|
102
|
+
- MIT
|
103
|
+
metadata: {}
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
requirements: []
|
119
|
+
rubyforge_project:
|
120
|
+
rubygems_version: 2.7.6
|
121
|
+
signing_key:
|
122
|
+
specification_version: 4
|
123
|
+
summary: Allow custom sort order with find_each behavior
|
124
|
+
test_files: []
|