delete_in_batches 0.2.0 → 0.2.1

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: 1f67278c3ef5c894ad549056a5f5b877e69742eb830b69aa1e468da9b109705f
4
- data.tar.gz: 4aee16921a6b72a72b392a46bd03f69c6de5b939ffa9c1fd1f17f9f0d3933da6
3
+ metadata.gz: 3a9e277e2aab5a09816e7df3578eb2a585d44e2fd1fe28a81c9f7606e9e6a6b9
4
+ data.tar.gz: 6df5995b465ef126c9f745d1a1553a7c90f24e65f04263fe673bde2c6593286e
5
5
  SHA512:
6
- metadata.gz: 386a6ed82ebdc123f9f286abeb4d0f15645a3d82ee013c48058ba9b230cfb1b05d4ccca2306bc0b06e3568c9e9cb5a22bd2f26c1cfb978ff083997111703e315
7
- data.tar.gz: 2d17af5375794e73ed3e2c8a41a3b5b8fc96f2e8807905194970fc79b63e500df5799618d3daf8bf560cba619534be8d71fd131813deb43fe043e3c56d924259
6
+ metadata.gz: c2cc30e03280311704551c681d57dac4badf6bb483834396bf8d7e4a0034cef9d5d910aab93c449936f1be6332ff49addba4f39d56c2fb769cd53f9f60691051
7
+ data.tar.gz: '08085780689fef205afe684d4d82a2deccb7325bac761bf47e0d7164e57d7a7965195469ea1b3396832bacd981284e290bbe58320107edd8f88ceb0099b0c109'
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.2.1 (2021-03-06)
2
+
3
+ - Added warning for non-Postgres databases
4
+ - Added `sleep` option
5
+
1
6
  ## 0.2.0 (2020-05-27)
2
7
 
3
8
  - Improved Active Record integration
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2014-2020 Andrew Kane
1
+ Copyright (c) 2014-2021 Andrew Kane
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # delete_in_batches
2
2
 
3
- :fire: The fastest way to delete 100k+ rows with ActiveRecord
3
+ :fire: Fast batch deletes for Active Record and Postgres
4
4
 
5
- [![Build Status](https://travis-ci.org/ankane/delete_in_batches.svg?branch=master)](https://travis-ci.org/ankane/delete_in_batches)
5
+ [![Build Status](https://github.com/ankane/delete_in_batches/workflows/build/badge.svg?branch=master)](https://github.com/ankane/delete_in_batches/actions)
6
6
 
7
7
  ## Installation
8
8
 
@@ -12,31 +12,12 @@ Add this line to your application’s Gemfile:
12
12
  gem 'delete_in_batches'
13
13
  ```
14
14
 
15
- ## Slow
15
+ ## How to Use
16
16
 
17
- ```ruby
18
- Tweet.where(user_id: 1).delete_all
19
- # DELETE FROM tweets WHERE user_id = 1
20
- ```
21
-
22
- The database performs the delete in a transaction - either all the records are deleted (query completes) or none are, due to [multiversion concurrency control](http://en.wikipedia.org/wiki/Multiversion_concurrency_control).
23
-
24
- ## Faster
25
-
26
- ```ruby
27
- Tweet.where(user_id: 1).in_batches(of: 10000).delete_all
28
- # SELECT tweets.id FROM tweets WHERE user_id = 1 ORDER BY id LIMIT 1000
29
- # DELETE FROM tweets WHERE user_id = 1 AND id IN (1, 2, 3, ...)
30
- # ...
31
- ```
32
-
33
- ## Fastest
17
+ Delete rows in batches
34
18
 
35
19
  ```ruby
36
20
  Tweet.where(user_id: 1).delete_in_batches
37
- # DELETE FROM tweets WHERE id IN (SELECT id FROM tweets WHERE user_id = 1 LIMIT 10000)
38
- # DELETE FROM tweets WHERE id IN (SELECT id FROM tweets WHERE user_id = 1 LIMIT 10000)
39
- # ...
40
21
  ```
41
22
 
42
23
  **Important:** Be sure to test your query before running it in production
@@ -47,6 +28,12 @@ Change the batch size
47
28
  Tweet.where(user_id: 1).delete_in_batches(batch_size: 50000) # defaults to 10000
48
29
  ```
49
30
 
31
+ Sleep between batches
32
+
33
+ ```ruby
34
+ Tweet.where(user_id: 1).delete_in_batches(sleep: 0.01)
35
+ ```
36
+
50
37
  Show progress
51
38
 
52
39
  ```ruby
@@ -67,6 +54,12 @@ To delete all rows in a table, `TRUNCATE` is fastest.
67
54
  ActiveRecord::Base.connection.execute("TRUNCATE tweets")
68
55
  ```
69
56
 
57
+ ## History
58
+
59
+ View the [changelog](https://github.com/ankane/delete_in_batches/blob/master/CHANGELOG.md)
60
+
61
+ **Note:** This project originally had the description “the fastest way to delete 100k+ rows with ActiveRecord” but a single `DELETE` statement will likely be faster. See [this discussion](https://github.com/ankane/delete_in_batches/issues/4) for more details.
62
+
70
63
  ## Contributing
71
64
 
72
65
  Everyone is encouraged to help improve this project. Here are a few ways you can help:
@@ -75,3 +68,12 @@ Everyone is encouraged to help improve this project. Here are a few ways you can
75
68
  - Fix bugs and [submit pull requests](https://github.com/ankane/delete_in_batches/pulls)
76
69
  - Write, clarify, or fix documentation
77
70
  - Suggest or add new features
71
+
72
+ To get started with development:
73
+
74
+ ```sh
75
+ git clone https://github.com/ankane/delete_in_batches.git
76
+ cd delete_in_batches
77
+ bundle install
78
+ bundle exec rake test
79
+ ```
@@ -5,6 +5,7 @@ require "active_support"
5
5
  require "delete_in_batches/version"
6
6
 
7
7
  module DeleteInBatches
8
+ # TODO use keyword arguments
8
9
  def delete_in_batches(options = {})
9
10
  batch_size = options[:batch_size] || 10000
10
11
 
@@ -16,8 +17,14 @@ module DeleteInBatches
16
17
  sql = "SELECT * FROM (#{sql}) AS t"
17
18
  end
18
19
 
20
+ unless connection.adapter_name =~ /postg/i
21
+ # TODO raise error
22
+ warn "[delete_in_batches] Use in_batches(of: #{batch_size.to_i}).delete_all instead of this gem for non-Postgres databases"
23
+ end
24
+
19
25
  while connection.delete("DELETE FROM #{quoted_table_name} WHERE #{pk} IN (#{sql})") == batch_size
20
26
  yield if block_given?
27
+ sleep(options[:sleep]) if options[:sleep]
21
28
  end
22
29
  end
23
30
  end
@@ -1,3 +1,3 @@
1
1
  module DeleteInBatches
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
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.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-28 00:00:00.000000000 Z
11
+ date: 2021-03-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -24,64 +24,8 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '5'
27
- - !ruby/object:Gem::Dependency
28
- name: bundler
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
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: rake
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: minitest
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: sqlite3
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
- description:
84
- email: andrew@chartkick.com
27
+ description:
28
+ email: andrew@ankane.org
85
29
  executables: []
86
30
  extensions: []
87
31
  extra_rdoc_files: []
@@ -95,7 +39,7 @@ homepage: https://github.com/ankane/delete_in_batches
95
39
  licenses:
96
40
  - MIT
97
41
  metadata: {}
98
- post_install_message:
42
+ post_install_message:
99
43
  rdoc_options: []
100
44
  require_paths:
101
45
  - lib
@@ -110,8 +54,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
54
  - !ruby/object:Gem::Version
111
55
  version: '0'
112
56
  requirements: []
113
- rubygems_version: 3.1.2
114
- signing_key:
57
+ rubygems_version: 3.2.3
58
+ signing_key:
115
59
  specification_version: 4
116
- summary: The fastest way to delete 100k+ rows with ActiveRecord
60
+ summary: Fast batch deletes for Active Record and Postgres
117
61
  test_files: []