Add --spec option for data_update generator (#11245)
This commit is contained in:
parent
c7c6764933
commit
9ad57a8d28
3 changed files with 41 additions and 2 deletions
|
|
@ -40,6 +40,26 @@ module DataUpdateScripts
|
|||
end
|
||||
```
|
||||
|
||||
The generator will also automatically create the corresponding spec file.
|
||||
|
||||
```ruby
|
||||
require "rails_helper"
|
||||
require Rails.root.join(
|
||||
"20201103042915_backfill_column_for_articles.rb",
|
||||
)
|
||||
|
||||
describe DataUpdateScripts::BackfillColumnForArticles do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
||||
```
|
||||
|
||||
While we encourage adding tests for data update scripts, you can skip spec
|
||||
creation by adding the `--no-spec` option to the `rails generate` command:
|
||||
|
||||
```
|
||||
rails generate data_update BackfillColumnForArticles --no-spec
|
||||
```
|
||||
|
||||
Once your script is in place then you can either run `rails data_updates:run`
|
||||
manually or you can let our setup script handle it. In our local
|
||||
[bin/setup](https://github.com/forem/forem/blob/master/bin/setup) script you
|
||||
|
|
|
|||
|
|
@ -1,11 +1,22 @@
|
|||
class DataUpdateGenerator < Rails::Generators::NamedBase
|
||||
source_root File.expand_path("templates", __dir__)
|
||||
class_option :spec, type: :boolean, default: true
|
||||
|
||||
def create_data_update_file
|
||||
template(
|
||||
"data_update.rb.tt",
|
||||
File.join("lib", "data_update_scripts", class_path,
|
||||
"#{Time.current.utc.strftime('%Y%m%d%H%M%S')}_#{file_name}.rb"),
|
||||
File.join("lib", "data_update_scripts", class_path, script_name),
|
||||
)
|
||||
|
||||
return unless options["spec"]
|
||||
|
||||
template(
|
||||
"data_update_spec.rb.tt",
|
||||
File.join("spec", "lib", "data_update_scripts", class_path, "#{file_name}_spec.rb"),
|
||||
)
|
||||
end
|
||||
|
||||
def script_name
|
||||
@script_name ||= "#{Time.current.utc.strftime('%Y%m%d%H%M%S')}_#{file_name}.rb"
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -0,0 +1,8 @@
|
|||
require "rails_helper"
|
||||
require Rails.root.join(
|
||||
"<%= script_name %>",
|
||||
)
|
||||
|
||||
describe DataUpdateScripts::<%= class_name %> do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
||||
Loading…
Add table
Reference in a new issue