nochmal 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nochmal
4
+ # This class handles including our rake task
5
+ class Railtie < Rails::Railtie
6
+ rake_tasks do
7
+ load "tasks/nochmal.rake"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nochmal
4
+ # Handles Reuploading of all attachments
5
+ # from: (required) = ActiveStorage service name (e.g. local)
6
+ # to: (optional) = ActiveStorage service name (e.g. aws) if left empty,
7
+ # the currently configured service will be used.
8
+ class Reupload
9
+ attr_reader :active_storage, :from_service, :to_service
10
+
11
+ def initialize(from: nil, to: nil, helper: nil)
12
+ @active_storage = helper || Adapters::ActiveStorage.new(from: from, to: to)
13
+ @notes = []
14
+ end
15
+
16
+ def all
17
+ handle_each_model(:reupload)
18
+ end
19
+
20
+ def list
21
+ handle_each_model(:list)
22
+ end
23
+
24
+ def count
25
+ handle_each_model(:count)
26
+ end
27
+
28
+ private
29
+
30
+ def handle_each_model(action) # rubocop:disable Metrics/MethodLength
31
+ @mode = action
32
+ active_storage.setup(@mode)
33
+
34
+ Output.reupload(models) do
35
+ models.each do |model|
36
+ next if skip_model?(model)
37
+
38
+ reupload_model(model)
39
+ end
40
+ end
41
+
42
+ @notes << active_storage.general_notes
43
+ Output.notes(@notes.compact)
44
+ active_storage.teardown
45
+ end
46
+
47
+ def reupload_model(model)
48
+ Output.model(model) do
49
+ active_storage.attachment_types_for(model).each do |type|
50
+ reupload_type(model, type)
51
+ end
52
+ end
53
+
54
+ active_storage.model_completed(model)
55
+ end
56
+
57
+ def reupload_type(model, type) # rubocop:disable Metrics/MethodLength,Metrics/AbcSize
58
+ collection = active_storage.collection(model, type)
59
+ @notes << active_storage.type_notes(model, type)
60
+
61
+ Output.type(type, collection.count, @mode) do
62
+ collection.find_each do |item|
63
+ result = perform(item, type)
64
+
65
+ @notes << result[:message]
66
+ Output.print_result_indicator(result[:status])
67
+ active_storage.item_completed(item, type, result[:status])
68
+ end
69
+ end
70
+
71
+ active_storage.type_completed(model, type)
72
+ end
73
+
74
+ def perform(attachment, type)
75
+ case @mode
76
+ when :reupload then active_storage.reupload(attachment, type)
77
+ when :list then active_storage.list(attachment)
78
+ when :count then active_storage.count
79
+ end
80
+ end
81
+
82
+ ## helper-functions
83
+
84
+ def skip_model?(model)
85
+ !model.table_exists? || # no table
86
+ active_storage.attachment_types_for(model).all? do |type| # no uploads of any kind (type)
87
+ active_storage.empty_collection?(model, type)
88
+ end
89
+ end
90
+
91
+ def models
92
+ active_storage.models_with_attachments
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nochmal
4
+ VERSION = "0.2.0"
5
+ end
data/lib/nochmal.rb ADDED
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.unshift File.dirname(__FILE__)
4
+
5
+ # This module is the base of the nochmal gem
6
+ module Nochmal
7
+ module Adapters
8
+ autoload :Base, "nochmal/adapters/base"
9
+ autoload :ActiveStorage, "nochmal/adapters/active_storage"
10
+ autoload :Carrierwave, "nochmal/adapters/carrierwave"
11
+ autoload :CarrierwaveAnalyze, "nochmal/adapters/carrierwave_analyze"
12
+ autoload :CarrierwaveMigration, "nochmal/adapters/carrierwave_migration"
13
+ autoload :CarrierwaveResume, "nochmal/adapters/carrierwave_resume"
14
+ end
15
+
16
+ module MigrationData
17
+ # ar-models to track data
18
+ autoload :Meta, "nochmal/migration_data/meta"
19
+ autoload :Status, "nochmal/migration_data/status"
20
+
21
+ # migration to back ar-models
22
+ autoload :CreateMigrationTables, "nochmal/migration_data/create_tables"
23
+ autoload :DropMigrationTables, "nochmal/migration_data/drop_tables"
24
+
25
+ # exceptions
26
+ autoload :Incomplete, "nochmal/migration_data/incomplete"
27
+ autoload :StatusExists, "nochmal/migration_data/status_exists"
28
+ end
29
+
30
+ autoload :Reupload, "nochmal/reupload"
31
+ autoload :Output, "nochmal/output"
32
+
33
+ autoload :VERSION, "nochmal/version"
34
+ end
35
+
36
+ require_relative "nochmal/railtie" if defined?(Rails)
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../nochmal/reupload"
4
+
5
+ namespace :nochmal do
6
+ desc "Reuploads attachments from ENV['REUPLOAD_FROM'] to ENV['REUPLOAD_TO'] or configured active_storage service"
7
+ task :reupload, %i[from to] => :environment do |_t, args|
8
+ from = args[:from] || ENV["REUPLOAD_FROM"] || raise("The ENV variable REUPLOAD_FROM is required")
9
+ to = args[:to] || ENV["REUPLOAD_TO"]
10
+
11
+ Nochmal::Reupload.new(from: from, to: to).all
12
+ end
13
+
14
+ desc "List attachments that would be reuploaded from ENV['REUPLOAD_FROM']"
15
+ task :list, [:from] => :environment do |_t, args|
16
+ from = args[:from] || ENV["REUPLOAD_FROM"] || raise("The ENV variable REUPLOAD_FROM is required")
17
+
18
+ Nochmal::Reupload.new(from: from).list
19
+ end
20
+
21
+ desc "Count attachments that would be reuploaded from ENV['REUPLOAD_FROM']"
22
+ task :count, [:from] => :environment do |_t, args|
23
+ from = args[:from] || ENV["REUPLOAD_FROM"] || raise("The ENV variable REUPLOAD_FROM is required")
24
+
25
+ Nochmal::Reupload.new(from: from).count
26
+ end
27
+
28
+ namespace :carrierwave do
29
+ desc "Analyse uploaders and suggest change to migrate from carrierwave to active_storage"
30
+ task :analyze, %i[to] => :environment do |_t, args|
31
+ to = args[:to] || ENV["REUPLOAD_TO"]
32
+
33
+ Nochmal::Reupload.new(to: to, helper: Nochmal::Adapters::CarrierwaveAnalyze.new).all
34
+ end
35
+
36
+ desc "Migrate uploads from carrierwave to active_storage"
37
+ task :migrate => :environment do
38
+ Nochmal::Reupload.new(helper: Nochmal::Adapters::CarrierwaveMigration.new).all
39
+ end
40
+
41
+ desc "Resume migrating uploads from carrierwave to active_storage"
42
+ task :resume => :environment do
43
+ Nochmal::Reupload.new(helper: Nochmal::Adapters::CarrierwaveResume.new).all
44
+ end
45
+
46
+ desc "Count uploads to be migrated from carrierwave to active_storage"
47
+ task :count => :environment do
48
+ Nochmal::Reupload.new(helper: Nochmal::Adapters::CarrierwaveMigration.new).count
49
+ end
50
+ end
51
+ end
data/nochmal.gemspec ADDED
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/nochmal/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "nochmal"
7
+ spec.version = Nochmal::VERSION
8
+ spec.authors = ["Thomas Burkhalter"]
9
+ spec.email = ["new.mysteryman@gmail.com"]
10
+
11
+ spec.summary = "Handles ActiveStorage attachment reuploading."
12
+ spec.description = "Adds a rake task to reupload ActiveStorage attachments to a new storage."
13
+ spec.homepage = "https://github.com/puzzle/nochmal"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = spec.homepage
19
+ spec.metadata["changelog_uri"] = "https://github.com/puzzle/nochmal/blob/main/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ # Uncomment to register a new dependency of your gem
31
+ # spec.add_dependency "example-gem", "~> 1.0"
32
+
33
+ spec.add_dependency "pastel" # terminal colors
34
+ spec.add_dependency "rails", ">= 6.0"
35
+
36
+ spec.add_development_dependency "codecov"
37
+ spec.add_development_dependency "dotenv"
38
+ spec.add_development_dependency "rake", "~> 13.0"
39
+ spec.add_development_dependency "rspec", "~> 3.0"
40
+ spec.add_development_dependency "rspec-its"
41
+ spec.add_development_dependency "rspec-rails"
42
+ spec.add_development_dependency "rspec-rake"
43
+ spec.add_development_dependency "rubocop", "~> 1.7"
44
+ spec.add_development_dependency "rubocop-discourse", "~> 2.4"
45
+ spec.add_development_dependency "rubocop-performance", "~> 1.6"
46
+ spec.add_development_dependency "rubocop-rspec", "~> 2.5.0"
47
+ spec.add_development_dependency "simplecov"
48
+ spec.add_development_dependency "sqlite3"
49
+
50
+ # For more information and examples about making a new gem, checkout our
51
+ # guide at: https://bundler.io/guides/creating_gem.html
52
+ end
metadata ADDED
@@ -0,0 +1,290 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nochmal
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Thomas Burkhalter
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-04-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pastel
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: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '6.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '6.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: codecov
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: dotenv
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: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '13.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '13.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
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: rspec-its
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-rails
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec-rake
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rubocop
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '1.7'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '1.7'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rubocop-discourse
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '2.4'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '2.4'
167
+ - !ruby/object:Gem::Dependency
168
+ name: rubocop-performance
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: '1.6'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: '1.6'
181
+ - !ruby/object:Gem::Dependency
182
+ name: rubocop-rspec
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - "~>"
186
+ - !ruby/object:Gem::Version
187
+ version: 2.5.0
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - "~>"
193
+ - !ruby/object:Gem::Version
194
+ version: 2.5.0
195
+ - !ruby/object:Gem::Dependency
196
+ name: simplecov
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - ">="
200
+ - !ruby/object:Gem::Version
201
+ version: '0'
202
+ type: :development
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - ">="
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
209
+ - !ruby/object:Gem::Dependency
210
+ name: sqlite3
211
+ requirement: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - ">="
214
+ - !ruby/object:Gem::Version
215
+ version: '0'
216
+ type: :development
217
+ prerelease: false
218
+ version_requirements: !ruby/object:Gem::Requirement
219
+ requirements:
220
+ - - ">="
221
+ - !ruby/object:Gem::Version
222
+ version: '0'
223
+ description: Adds a rake task to reupload ActiveStorage attachments to a new storage.
224
+ email:
225
+ - new.mysteryman@gmail.com
226
+ executables: []
227
+ extensions: []
228
+ extra_rdoc_files: []
229
+ files:
230
+ - ".envrc"
231
+ - ".github/workflows/main.yml"
232
+ - ".gitignore"
233
+ - ".rspec"
234
+ - ".rubocop.yml"
235
+ - ".tool-versions"
236
+ - CHANGELOG.md
237
+ - CODE_OF_CONDUCT.md
238
+ - Gemfile
239
+ - LICENSE.txt
240
+ - README.md
241
+ - Rakefile
242
+ - bin/console
243
+ - bin/rake
244
+ - bin/rspec
245
+ - bin/setup
246
+ - lib/nochmal.rb
247
+ - lib/nochmal/adapters/active_storage.rb
248
+ - lib/nochmal/adapters/base.rb
249
+ - lib/nochmal/adapters/carrierwave.rb
250
+ - lib/nochmal/adapters/carrierwave_analyze.rb
251
+ - lib/nochmal/adapters/carrierwave_migration.rb
252
+ - lib/nochmal/adapters/carrierwave_resume.rb
253
+ - lib/nochmal/migration_data/create_tables.rb
254
+ - lib/nochmal/migration_data/incomplete.rb
255
+ - lib/nochmal/migration_data/meta.rb
256
+ - lib/nochmal/migration_data/status.rb
257
+ - lib/nochmal/migration_data/status_exists.rb
258
+ - lib/nochmal/output.rb
259
+ - lib/nochmal/railtie.rb
260
+ - lib/nochmal/reupload.rb
261
+ - lib/nochmal/version.rb
262
+ - lib/tasks/nochmal.rake
263
+ - nochmal.gemspec
264
+ homepage: https://github.com/puzzle/nochmal
265
+ licenses:
266
+ - MIT
267
+ metadata:
268
+ homepage_uri: https://github.com/puzzle/nochmal
269
+ source_code_uri: https://github.com/puzzle/nochmal
270
+ changelog_uri: https://github.com/puzzle/nochmal/blob/main/CHANGELOG.md
271
+ post_install_message:
272
+ rdoc_options: []
273
+ require_paths:
274
+ - lib
275
+ required_ruby_version: !ruby/object:Gem::Requirement
276
+ requirements:
277
+ - - ">="
278
+ - !ruby/object:Gem::Version
279
+ version: 2.5.0
280
+ required_rubygems_version: !ruby/object:Gem::Requirement
281
+ requirements:
282
+ - - ">="
283
+ - !ruby/object:Gem::Version
284
+ version: '0'
285
+ requirements: []
286
+ rubygems_version: 3.2.3
287
+ signing_key:
288
+ specification_version: 4
289
+ summary: Handles ActiveStorage attachment reuploading.
290
+ test_files: []