delete_in_batches 0.0.1 → 0.0.2

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
  SHA1:
3
- metadata.gz: a63cc69d5539bdab7a0cbc60bf015049ca8ce729
4
- data.tar.gz: 6f213288c1f9a52666af9d9cb01b5ec615ad02ee
3
+ metadata.gz: 9334c1787ed0531d63d596f13af0d01497980b50
4
+ data.tar.gz: efc570b218f5feece5842f12976f544f9cbfe003
5
5
  SHA512:
6
- metadata.gz: b9a00e37f6272dff4674fcbbbaea290b8c047265a51718c07fce8bf2ca5584288341db59b377fe5be3132b3fd0aeef85a1ad05487f86a2f4fcd4071dc6db29d2
7
- data.tar.gz: a02e0da81efae6cb1d4782a4401b83d9133bb3253e3d96e4f817e34834e4168ad4ac67f5eb54bfc28673433b25e01cbd91da406a20e3727d9258ecd502be0df0
6
+ metadata.gz: 08496a8fe18729a87919ad9870ed77f9fe355c409fcafc10f6aa827acf5ad8f936408227fea792d9d1e3c9ab1f9ad75f5f0d138f49a0bd59b3ca9b07056f1b47
7
+ data.tar.gz: d0a79745e4cf3d55a7cc17ea9586a6f1ca6f151a673a63fe6b07e41beb497236e6c3c5404064161a3ac3fd4da19931cab1bed41059c59774239b1c98d1f1f8ac
data/Rakefile CHANGED
@@ -1 +1,8 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ task :default => :test
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "test"
7
+ t.pattern = "test/**/*_test.rb"
8
+ end
@@ -18,6 +18,10 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.add_dependency "activerecord"
22
+
21
23
  spec.add_development_dependency "bundler", "~> 1.5"
22
24
  spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "minitest", "~> 4.7"
26
+ spec.add_development_dependency "pg"
23
27
  end
@@ -1,4 +1,5 @@
1
1
  require "delete_in_batches/version"
2
+ require "active_record"
2
3
 
3
4
  module DeleteInBatches
4
5
 
@@ -6,17 +7,18 @@ module DeleteInBatches
6
7
  batch_size = options[:batch_size] || 10000
7
8
 
8
9
  # TODO dry
10
+ pk = "#{quoted_table_name}.#{quoted_primary_key}"
9
11
  sql =
10
12
  if connection.respond_to?(:unprepared_statement)
11
13
  # ActiveRecord 4
12
14
  connection.unprepared_statement do
13
- select(:id).limit(batch_size).to_sql
15
+ select(pk).limit(batch_size).to_sql
14
16
  end
15
17
  else
16
- select(:id).limit(batch_size).to_sql
18
+ select(pk).limit(batch_size).to_sql
17
19
  end
18
20
 
19
- while connection.delete("DELETE FROM #{table_name} WHERE id IN (#{sql})") == batch_size
21
+ while connection.delete("DELETE FROM #{quoted_table_name} WHERE #{pk} IN (#{sql})") == batch_size
20
22
  yield if block_given?
21
23
  end
22
24
  end
@@ -1,3 +1,3 @@
1
1
  module DeleteInBatches
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,55 @@
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
@@ -0,0 +1,31 @@
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: delete_in_batches
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
@@ -10,6 +10,20 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2014-01-28 00:00:00.000000000 Z
12
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'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -38,6 +52,34 @@ dependencies:
38
52
  - - ">="
39
53
  - !ruby/object:Gem::Version
40
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: '4.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.7'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pg
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'
41
83
  description: The fastest way to delete millions of rows with ActiveRecord
42
84
  email:
43
85
  - andrew@chartkick.com
@@ -53,6 +95,8 @@ files:
53
95
  - delete_in_batches.gemspec
54
96
  - lib/delete_in_batches.rb
55
97
  - lib/delete_in_batches/version.rb
98
+ - test/delete_in_batches_test.rb
99
+ - test/test_helper.rb
56
100
  homepage: https://github.com/ankane/delete_in_batches
57
101
  licenses:
58
102
  - MIT
@@ -77,4 +121,6 @@ rubygems_version: 2.2.0
77
121
  signing_key:
78
122
  specification_version: 4
79
123
  summary: The fastest way to delete millions of rows with ActiveRecord
80
- test_files: []
124
+ test_files:
125
+ - test/delete_in_batches_test.rb
126
+ - test/test_helper.rb