delete_in_batches 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +9 -1
- data/lib/delete_in_batches.rb +8 -14
- data/lib/delete_in_batches/version.rb +1 -1
- metadata +17 -26
- data/.gitignore +0 -17
- data/Gemfile +0 -4
- data/Rakefile +0 -8
- data/delete_in_batches.gemspec +0 -27
- data/test/delete_in_batches_test.rb +0 -55
- data/test/test_helper.rb +0 -31
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c19c0abdd26b2ea2eea3ef9852d800f29077e7da89ecf3268068d4bc1c9a5032
|
4
|
+
data.tar.gz: cfec1154bed9e3f406bb8fe601676204d6000f9fd569a1111c58e89d55c8953c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a1f174a03a098551d0542075d4a043ae523d17d3340802d010309c671f05a1a51c98b0ff1aba00adcce48a7715232e123b69a68d2fb3534440368c5fdfadfee
|
7
|
+
data.tar.gz: 198d18d925e870567895eaf3373090b7155d4442c386ab37ad70e18efa4f9c0c5495f8034ad3070c7bf3c5c2a6bfb1c0a16228b7f62b402b7ec3472be4bf6644
|
data/README.md
CHANGED
@@ -1,6 +1,14 @@
|
|
1
1
|
# delete_in_batches
|
2
2
|
|
3
|
-
:fire: The fastest way to delete
|
3
|
+
:fire: The fastest way to delete 100k+ rows with ActiveRecord
|
4
|
+
|
5
|
+
**Note: This gem is not needed in Rails 5+.** Instead, you can do:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
Tweet.where(user_id: 1).in_batches(of: 10000).delete_all
|
9
|
+
```
|
10
|
+
|
11
|
+
[![Build Status](https://travis-ci.org/ankane/delete_in_batches.svg?branch=master)](https://travis-ci.org/ankane/delete_in_batches)
|
4
12
|
|
5
13
|
## Slow
|
6
14
|
|
data/lib/delete_in_batches.rb
CHANGED
@@ -2,27 +2,21 @@ require "delete_in_batches/version"
|
|
2
2
|
require "active_record"
|
3
3
|
|
4
4
|
module DeleteInBatches
|
5
|
-
|
6
|
-
def delete_in_batches(options = {}, &block)
|
5
|
+
def delete_in_batches(options = {})
|
7
6
|
batch_size = options[:batch_size] || 10000
|
8
7
|
|
9
|
-
#
|
10
|
-
|
11
|
-
sql
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
end
|
17
|
-
else
|
18
|
-
select(pk).limit(batch_size).to_sql
|
19
|
-
end
|
8
|
+
pk = "#{quoted_table_name}.#{quoted_primary_key}"
|
9
|
+
sql_proc = proc { select(pk).limit(batch_size).to_sql }
|
10
|
+
sql = connection.try(:unprepared_statement, &sql_proc) || sql_proc.call
|
11
|
+
|
12
|
+
if %w(MySQL Mysql2 Mysql2Spatial).include?(connection.adapter_name)
|
13
|
+
sql = "SELECT * FROM (#{sql}) AS t"
|
14
|
+
end
|
20
15
|
|
21
16
|
while connection.delete("DELETE FROM #{quoted_table_name} WHERE #{pk} IN (#{sql})") == batch_size
|
22
17
|
yield if block_given?
|
23
18
|
end
|
24
19
|
end
|
25
|
-
|
26
20
|
end
|
27
21
|
|
28
22
|
ActiveRecord::Base.send :extend, DeleteInBatches
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: delete_in_batches
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kane
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-09-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -28,16 +28,16 @@ dependencies:
|
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -56,18 +56,18 @@ dependencies:
|
|
56
56
|
name: minitest
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - "
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
61
|
+
version: '0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - "
|
66
|
+
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
68
|
+
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: sqlite3
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - ">="
|
@@ -80,23 +80,16 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
|
-
description:
|
84
|
-
email:
|
85
|
-
- andrew@chartkick.com
|
83
|
+
description:
|
84
|
+
email: andrew@chartkick.com
|
86
85
|
executables: []
|
87
86
|
extensions: []
|
88
87
|
extra_rdoc_files: []
|
89
88
|
files:
|
90
|
-
- ".gitignore"
|
91
|
-
- Gemfile
|
92
89
|
- LICENSE.txt
|
93
90
|
- README.md
|
94
|
-
- Rakefile
|
95
|
-
- delete_in_batches.gemspec
|
96
91
|
- lib/delete_in_batches.rb
|
97
92
|
- lib/delete_in_batches/version.rb
|
98
|
-
- test/delete_in_batches_test.rb
|
99
|
-
- test/test_helper.rb
|
100
93
|
homepage: https://github.com/ankane/delete_in_batches
|
101
94
|
licenses:
|
102
95
|
- MIT
|
@@ -109,7 +102,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
109
102
|
requirements:
|
110
103
|
- - ">="
|
111
104
|
- !ruby/object:Gem::Version
|
112
|
-
version: '
|
105
|
+
version: '2.2'
|
113
106
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
107
|
requirements:
|
115
108
|
- - ">="
|
@@ -117,10 +110,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
117
110
|
version: '0'
|
118
111
|
requirements: []
|
119
112
|
rubyforge_project:
|
120
|
-
rubygems_version: 2.
|
113
|
+
rubygems_version: 2.7.7
|
121
114
|
signing_key:
|
122
115
|
specification_version: 4
|
123
|
-
summary: The fastest way to delete
|
124
|
-
test_files:
|
125
|
-
- test/delete_in_batches_test.rb
|
126
|
-
- test/test_helper.rb
|
116
|
+
summary: The fastest way to delete 100k+ rows with ActiveRecord
|
117
|
+
test_files: []
|
data/.gitignore
DELETED
data/Gemfile
DELETED
data/Rakefile
DELETED
data/delete_in_batches.gemspec
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'delete_in_batches/version'
|
5
|
-
|
6
|
-
Gem::Specification.new do |spec|
|
7
|
-
spec.name = "delete_in_batches"
|
8
|
-
spec.version = DeleteInBatches::VERSION
|
9
|
-
spec.authors = ["Andrew Kane"]
|
10
|
-
spec.email = ["andrew@chartkick.com"]
|
11
|
-
spec.summary = %q{The fastest way to delete millions of rows with ActiveRecord}
|
12
|
-
spec.description = %q{The fastest way to delete millions of rows with ActiveRecord}
|
13
|
-
spec.homepage = "https://github.com/ankane/delete_in_batches"
|
14
|
-
spec.license = "MIT"
|
15
|
-
|
16
|
-
spec.files = `git ls-files -z`.split("\x0")
|
17
|
-
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
-
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
-
spec.require_paths = ["lib"]
|
20
|
-
|
21
|
-
spec.add_dependency "activerecord"
|
22
|
-
|
23
|
-
spec.add_development_dependency "bundler", "~> 1.5"
|
24
|
-
spec.add_development_dependency "rake"
|
25
|
-
spec.add_development_dependency "minitest", "~> 4.7"
|
26
|
-
spec.add_development_dependency "pg"
|
27
|
-
end
|
@@ -1,55 +0,0 @@
|
|
1
|
-
require_relative "test_helper"
|
2
|
-
|
3
|
-
class TestDeleteInBatches < Minitest::Unit::TestCase
|
4
|
-
|
5
|
-
def test_basic
|
6
|
-
10.times do
|
7
|
-
Tweet.create(user_id: 1)
|
8
|
-
end
|
9
|
-
Tweet.create(user_id: 2)
|
10
|
-
|
11
|
-
Tweet.where(user_id: 1).delete_in_batches(batch_size: 2)
|
12
|
-
|
13
|
-
assert_equal 1, Tweet.count
|
14
|
-
assert_equal 2, Tweet.first.user_id
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_all
|
18
|
-
Tweet.create(user_id: 1)
|
19
|
-
|
20
|
-
Tweet.delete_in_batches
|
21
|
-
|
22
|
-
assert_equal 0, Tweet.count
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_progress
|
26
|
-
10.times do
|
27
|
-
Tweet.create(user_id: 1)
|
28
|
-
end
|
29
|
-
|
30
|
-
i = 0
|
31
|
-
Tweet.where(user_id: 1).delete_in_batches(batch_size: 2) do
|
32
|
-
i += 1
|
33
|
-
end
|
34
|
-
|
35
|
-
assert_equal 5, i
|
36
|
-
end
|
37
|
-
|
38
|
-
def test_association
|
39
|
-
user = User.create!
|
40
|
-
user.tweets.create!
|
41
|
-
|
42
|
-
user.tweets.delete_in_batches
|
43
|
-
|
44
|
-
assert_equal 0, user.tweets.count
|
45
|
-
end
|
46
|
-
|
47
|
-
def test_join
|
48
|
-
Tweet.create(user_id: 1)
|
49
|
-
|
50
|
-
Tweet.joins(:user).where(users: {id: 1}).delete_in_batches
|
51
|
-
|
52
|
-
assert_equal 0, Tweet.count
|
53
|
-
end
|
54
|
-
|
55
|
-
end
|
data/test/test_helper.rb
DELETED
@@ -1,31 +0,0 @@
|
|
1
|
-
require "bundler/setup"
|
2
|
-
Bundler.require(:default)
|
3
|
-
require "minitest/autorun"
|
4
|
-
require "minitest/pride"
|
5
|
-
|
6
|
-
ENV["RACK_ENV"] = "test"
|
7
|
-
|
8
|
-
# for debugging
|
9
|
-
# ActiveRecord::Base.logger = Logger.new(STDOUT)
|
10
|
-
|
11
|
-
# rails does this in activerecord/lib/active_record/railtie.rb
|
12
|
-
ActiveRecord::Base.default_timezone = :utc
|
13
|
-
ActiveRecord::Base.time_zone_aware_attributes = true
|
14
|
-
|
15
|
-
# migrations
|
16
|
-
ActiveRecord::Base.establish_connection :adapter => "postgresql", :database => "delete_in_batches_test"
|
17
|
-
|
18
|
-
ActiveRecord::Migration.create_table :tweets, :force => true do |t|
|
19
|
-
t.integer :user_id
|
20
|
-
end
|
21
|
-
|
22
|
-
ActiveRecord::Migration.create_table :users, :force => true do |t|
|
23
|
-
end
|
24
|
-
|
25
|
-
class Tweet < ActiveRecord::Base
|
26
|
-
belongs_to :user
|
27
|
-
end
|
28
|
-
|
29
|
-
class User < ActiveRecord::Base
|
30
|
-
has_many :tweets
|
31
|
-
end
|