delete_you_later 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8e04a2c1c7bea058d3f9f20ccc2097f55e20dc0ff6750d61dad474d3131026e6
4
+ data.tar.gz: 80e0a4538e72478d1bc1e17e1bde24f42c47966894de1bf432b2fae180dbfd83
5
+ SHA512:
6
+ metadata.gz: 8a50d035dde78ed449d056f462afa114ae6bedc8d1d22e6c8d537590de616d35649c00cb8abc7bfd051c3756dc72a4dd36164e138e036f7ae72fbac936d435d3
7
+ data.tar.gz: a20e77223e1d598568bf364fb116a2e8e19b29cb2345a3d2b75143f71ec2b99b842940aa6d6a20fbb04ac11d307ce2da56bd395dd6a1da0e6ae3eb731285b355
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.3
7
+ before_install: gem install bundler -v 2.0.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in delete_you_later.gemspec
4
+ gemspec
@@ -0,0 +1,167 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ delete_you_later (0.1.0)
5
+ activerecord (>= 5)
6
+ railties (>= 5)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actioncable (6.0.2.1)
12
+ actionpack (= 6.0.2.1)
13
+ nio4r (~> 2.0)
14
+ websocket-driver (>= 0.6.1)
15
+ actionmailbox (6.0.2.1)
16
+ actionpack (= 6.0.2.1)
17
+ activejob (= 6.0.2.1)
18
+ activerecord (= 6.0.2.1)
19
+ activestorage (= 6.0.2.1)
20
+ activesupport (= 6.0.2.1)
21
+ mail (>= 2.7.1)
22
+ actionmailer (6.0.2.1)
23
+ actionpack (= 6.0.2.1)
24
+ actionview (= 6.0.2.1)
25
+ activejob (= 6.0.2.1)
26
+ mail (~> 2.5, >= 2.5.4)
27
+ rails-dom-testing (~> 2.0)
28
+ actionpack (6.0.2.1)
29
+ actionview (= 6.0.2.1)
30
+ activesupport (= 6.0.2.1)
31
+ rack (~> 2.0, >= 2.0.8)
32
+ rack-test (>= 0.6.3)
33
+ rails-dom-testing (~> 2.0)
34
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
35
+ actiontext (6.0.2.1)
36
+ actionpack (= 6.0.2.1)
37
+ activerecord (= 6.0.2.1)
38
+ activestorage (= 6.0.2.1)
39
+ activesupport (= 6.0.2.1)
40
+ nokogiri (>= 1.8.5)
41
+ actionview (6.0.2.1)
42
+ activesupport (= 6.0.2.1)
43
+ builder (~> 3.1)
44
+ erubi (~> 1.4)
45
+ rails-dom-testing (~> 2.0)
46
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
47
+ activejob (6.0.2.1)
48
+ activesupport (= 6.0.2.1)
49
+ globalid (>= 0.3.6)
50
+ activemodel (6.0.2.1)
51
+ activesupport (= 6.0.2.1)
52
+ activerecord (6.0.2.1)
53
+ activemodel (= 6.0.2.1)
54
+ activesupport (= 6.0.2.1)
55
+ activestorage (6.0.2.1)
56
+ actionpack (= 6.0.2.1)
57
+ activejob (= 6.0.2.1)
58
+ activerecord (= 6.0.2.1)
59
+ marcel (~> 0.3.1)
60
+ activesupport (6.0.2.1)
61
+ concurrent-ruby (~> 1.0, >= 1.0.2)
62
+ i18n (>= 0.7, < 2)
63
+ minitest (~> 5.1)
64
+ tzinfo (~> 1.1)
65
+ zeitwerk (~> 2.2)
66
+ builder (3.2.4)
67
+ coderay (1.1.2)
68
+ concurrent-ruby (1.1.6)
69
+ crass (1.0.6)
70
+ diff-lcs (1.3)
71
+ erubi (1.9.0)
72
+ globalid (0.4.2)
73
+ activesupport (>= 4.2.0)
74
+ i18n (1.8.2)
75
+ concurrent-ruby (~> 1.0)
76
+ loofah (2.4.0)
77
+ crass (~> 1.0.2)
78
+ nokogiri (>= 1.5.9)
79
+ mail (2.7.1)
80
+ mini_mime (>= 0.1.1)
81
+ marcel (0.3.3)
82
+ mimemagic (~> 0.3.2)
83
+ method_source (0.9.2)
84
+ mimemagic (0.3.4)
85
+ mini_mime (1.0.2)
86
+ mini_portile2 (2.4.0)
87
+ minitest (5.14.0)
88
+ nio4r (2.5.2)
89
+ nokogiri (1.10.8)
90
+ mini_portile2 (~> 2.4.0)
91
+ pry (0.11.3)
92
+ coderay (~> 1.1.0)
93
+ method_source (~> 0.9.0)
94
+ rack (2.2.2)
95
+ rack-test (1.1.0)
96
+ rack (>= 1.0, < 3)
97
+ rails (6.0.2.1)
98
+ actioncable (= 6.0.2.1)
99
+ actionmailbox (= 6.0.2.1)
100
+ actionmailer (= 6.0.2.1)
101
+ actionpack (= 6.0.2.1)
102
+ actiontext (= 6.0.2.1)
103
+ actionview (= 6.0.2.1)
104
+ activejob (= 6.0.2.1)
105
+ activemodel (= 6.0.2.1)
106
+ activerecord (= 6.0.2.1)
107
+ activestorage (= 6.0.2.1)
108
+ activesupport (= 6.0.2.1)
109
+ bundler (>= 1.3.0)
110
+ railties (= 6.0.2.1)
111
+ sprockets-rails (>= 2.0.0)
112
+ rails-dom-testing (2.0.3)
113
+ activesupport (>= 4.2.0)
114
+ nokogiri (>= 1.6)
115
+ rails-html-sanitizer (1.3.0)
116
+ loofah (~> 2.3)
117
+ railties (6.0.2.1)
118
+ actionpack (= 6.0.2.1)
119
+ activesupport (= 6.0.2.1)
120
+ method_source
121
+ rake (>= 0.8.7)
122
+ thor (>= 0.20.3, < 2.0)
123
+ rake (13.0.1)
124
+ rspec (3.9.0)
125
+ rspec-core (~> 3.9.0)
126
+ rspec-expectations (~> 3.9.0)
127
+ rspec-mocks (~> 3.9.0)
128
+ rspec-core (3.9.1)
129
+ rspec-support (~> 3.9.1)
130
+ rspec-expectations (3.9.0)
131
+ diff-lcs (>= 1.2.0, < 2.0)
132
+ rspec-support (~> 3.9.0)
133
+ rspec-mocks (3.9.1)
134
+ diff-lcs (>= 1.2.0, < 2.0)
135
+ rspec-support (~> 3.9.0)
136
+ rspec-support (3.9.2)
137
+ sprockets (4.0.0)
138
+ concurrent-ruby (~> 1.0)
139
+ rack (> 1, < 3)
140
+ sprockets-rails (3.2.1)
141
+ actionpack (>= 4.0)
142
+ activesupport (>= 4.0)
143
+ sprockets (>= 3.0.0)
144
+ sqlite3 (1.4.2)
145
+ thor (1.0.1)
146
+ thread_safe (0.3.6)
147
+ tzinfo (1.2.6)
148
+ thread_safe (~> 0.1)
149
+ websocket-driver (0.7.1)
150
+ websocket-extensions (>= 0.1.0)
151
+ websocket-extensions (0.1.4)
152
+ zeitwerk (2.2.2)
153
+
154
+ PLATFORMS
155
+ ruby
156
+
157
+ DEPENDENCIES
158
+ bundler
159
+ delete_you_later!
160
+ pry (~> 0.11.3)
161
+ rails (>= 3.0)
162
+ rake
163
+ rspec (~> 3.0)
164
+ sqlite3
165
+
166
+ BUNDLED WITH
167
+ 2.0.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Josh Brody
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.
@@ -0,0 +1,62 @@
1
+ # DeleteYouLater
2
+
3
+ Deletes/destroys associated records in the background, because destroying a billion records in the foreground just will not work.
4
+
5
+ ## Why
6
+
7
+ Because you probably have destroy callbacks and those are the devil. 😈🔱
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'delete_you_later'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ class Post < ApplicationRecord
25
+ has_many :posts
26
+ has_many :likes
27
+
28
+ destroy_dependents_later :posts
29
+ delete_dependents_later :likes
30
+ end
31
+ ```
32
+
33
+ ## Configuration
34
+
35
+ Drop into an initializer:
36
+
37
+ ```ruby
38
+ DeleteYouLater.configure do |config|
39
+ config.scope = :published # default is not scoped
40
+ config.batch_size = 100_000 # default is 100
41
+ end
42
+ ```
43
+
44
+ You can change these at runtime:
45
+
46
+ ```ruby
47
+ class Post < ApplicationRecord
48
+ has_many :posts
49
+
50
+ destroy_dependents_later :posts, batch_size: 10, scope: :recent
51
+ end
52
+ ```
53
+
54
+ ## Contributing
55
+
56
+ This isn't one-size-fits-all but one-size-fits-joshmn's-clients. It might help you too, if not at least give you inspiration for modeling this in your application. I've used this for billions of records with success. Don't ask about my failures.
57
+
58
+ Bug reports and pull requests are kindly welcomed on GitHub at https://github.com/joshmn/delete_you_later
59
+
60
+ ## License
61
+
62
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require_relative '../lib/delete_you_later/version'
5
+
6
+ version = DeleteYouLater::VERSION
7
+ system("gem build delete_you_later.gemspec")
8
+ system("gem push delete_you_later-#{version}.gem")
9
+ system("rm delete_you_later-#{version}.gem")
10
+ system("git tag -a v#{version} -m 'version #{version}'")
11
+ system("git push origin v#{version}")
@@ -0,0 +1,34 @@
1
+ $:.push File.expand_path("lib", __dir__)
2
+
3
+ # Maintain your gem's version:
4
+ require "delete_you_later/version"
5
+
6
+ # Describe your gem and declare its dependencies:
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "delete_you_later"
9
+ spec.version = DeleteYouLater::VERSION
10
+ spec.authors = ["Josh Brody"]
11
+ spec.email = ["josh@josh.mn"]
12
+ spec.homepage = "https://github.com/joshmn/delete_you_later"
13
+ spec.summary = "Delete/destroy associated ActiveRecord records in the background with ease."
14
+ spec.description = spec.summary
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.require_paths = ['lib']
21
+ spec.files += Dir['lib/**/*.rb']
22
+
23
+ spec.required_ruby_version = ">= 2.4"
24
+
25
+ spec.add_dependency "railties", ">= 5"
26
+ spec.add_dependency "activerecord", ">= 5"
27
+
28
+ spec.add_development_dependency "bundler"
29
+ spec.add_development_dependency "rake"
30
+ spec.add_development_dependency 'pry', '~> 0.11.3'
31
+ spec.add_development_dependency 'rails', '>= 3.0'
32
+ spec.add_development_dependency 'sqlite3'
33
+ spec.add_development_dependency 'rspec', '~> 3.0'
34
+ end
@@ -0,0 +1,20 @@
1
+ require 'ostruct'
2
+
3
+ require "rails/all"
4
+
5
+ require 'delete_you_later/model'
6
+ require 'delete_you_later/delete_later_job'
7
+ require 'delete_you_later/destroy_later_job'
8
+ require 'delete_you_later/railtie'
9
+ require 'delete_you_later/version'
10
+
11
+ module DeleteYouLater
12
+ class << self
13
+ attr_accessor :configuration
14
+ end
15
+
16
+ # sorry.
17
+ def self.configuration
18
+ @configuration ||= OpenStruct.new(batch_size: 100, scope: nil)
19
+ end
20
+ end
@@ -0,0 +1,23 @@
1
+ module DeleteYouLater
2
+ class DeleteLaterJob < ActiveJob::Base
3
+ def perform(model_name, model_id, assoc, scope: DeleteYouLater.configuration.scope, batch_size: DeleteYouLater.configuration.batch_size)
4
+ model = model_name.constantize
5
+ association = model.reflect_on_association(assoc.to_sym)
6
+ dependent = association.klass
7
+ foreign_key = association.foreign_key
8
+ last_seen = 0
9
+ loop do
10
+ ids = association.klass.select(:id).where(foreign_key => model_id).where("id > ?", last_seen)
11
+ if scope
12
+ ids = ids.public_send(scope)
13
+ end
14
+ ids = ids.limit(batch_size).pluck(:id)
15
+ break if ids.empty?
16
+ last_seen = ids.last
17
+ dependent.where(id: ids).find_each do |record|
18
+ record.delete
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ module DeleteYouLater
2
+ class DestroyLaterJob < ActiveJob::Base
3
+ def perform(model_name, model_id, assoc, scope: DeleteYouLater.configuration.scope, batch_size: DeleteYouLater.configuration.batch_size)
4
+ model = model_name.constantize
5
+ association = model.reflect_on_association(assoc.to_sym)
6
+ dependent = association.klass
7
+ foreign_key = association.foreign_key
8
+ last_seen = 0
9
+ loop do
10
+ ids = association.klass.select(:id).where(foreign_key => model_id).where("id > ?", last_seen)
11
+ if scope
12
+ ids = ids.public_send(scope)
13
+ end
14
+ ids = ids.limit(batch_size).pluck(:id)
15
+ break if ids.empty?
16
+ last_seen = ids.last
17
+ dependent.where(id: ids).find_each do |record|
18
+ record.destroy!
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ module DeleteYouLater
2
+ module Model
3
+ def delete_dependents_later(assoc, scope: DeleteYouLater.configuration.scope, batch_size: DeleteYouLater.configuration.batch_size)
4
+ klass = self.name
5
+
6
+ after_commit on: :destroy do
7
+ if send(assoc).exists?
8
+ DeleteLaterJob.perform_later klass, id, assoc, scope: scope, batch_size: batch_size
9
+ end
10
+ end
11
+ end
12
+
13
+ def destroy_dependents_later(assoc, scope: DeleteYouLater.configuration.scope, batch_size: DeleteYouLater.configuration.batch_size)
14
+ klass = self.name
15
+
16
+ after_commit on: :destroy do
17
+ if send(assoc).exists?
18
+ DestroyLaterJob.perform_later klass, id, assoc, scope: scope, batch_size: batch_size
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ module DeleteYouLater
2
+ class Railtie < ::Rails::Railtie
3
+ ActiveSupport.on_load(:active_record) { extend DeleteYouLater::Model }
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module DeleteYouLater
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,172 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: delete_you_later
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Josh Brody
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-03-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
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: '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: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.11.3
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.11.3
83
+ - !ruby/object:Gem::Dependency
84
+ name: rails
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: sqlite3
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '3.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '3.0'
125
+ description: Delete/destroy associated ActiveRecord records in the background with
126
+ ease.
127
+ email:
128
+ - josh@josh.mn
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".gitignore"
134
+ - ".rspec"
135
+ - ".travis.yml"
136
+ - Gemfile
137
+ - Gemfile.lock
138
+ - LICENSE.txt
139
+ - README.md
140
+ - Rakefile
141
+ - bin/release
142
+ - delete_you_later.gemspec
143
+ - lib/delete_you_later.rb
144
+ - lib/delete_you_later/delete_later_job.rb
145
+ - lib/delete_you_later/destroy_later_job.rb
146
+ - lib/delete_you_later/model.rb
147
+ - lib/delete_you_later/railtie.rb
148
+ - lib/delete_you_later/version.rb
149
+ homepage: https://github.com/joshmn/delete_you_later
150
+ licenses:
151
+ - MIT
152
+ metadata: {}
153
+ post_install_message:
154
+ rdoc_options: []
155
+ require_paths:
156
+ - lib
157
+ required_ruby_version: !ruby/object:Gem::Requirement
158
+ requirements:
159
+ - - ">="
160
+ - !ruby/object:Gem::Version
161
+ version: '2.4'
162
+ required_rubygems_version: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ requirements: []
168
+ rubygems_version: 3.0.3
169
+ signing_key:
170
+ specification_version: 4
171
+ summary: Delete/destroy associated ActiveRecord records in the background with ease.
172
+ test_files: []