Seed database with inline ActiveJob job adapter

This commit is contained in:
Gannon McGibbon 2019-01-16 19:02:23 -05:00
parent 90536ebfb3
commit 66cc0e768f
3 changed files with 29 additions and 1 deletions

@ -1,3 +1,7 @@
* Seed database with inline ActiveJob job adapter.
*Gannon McGibbon*
* Add `rails db:system:change` command for changing databases. * Add `rails db:system:change` command for changing databases.
``` ```

@ -548,7 +548,7 @@ def config
# Blog::Engine.load_seed # Blog::Engine.load_seed
def load_seed def load_seed
seed_file = paths["db/seeds.rb"].existent.first seed_file = paths["db/seeds.rb"].existent.first
load(seed_file) if seed_file with_inline_jobs { load(seed_file) } if seed_file
end end
# Add configured load paths to Ruby's load path, and remove duplicate entries. # Add configured load paths to Ruby's load path, and remove duplicate entries.
@ -658,6 +658,18 @@ def load_config_initializer(initializer) # :doc:
end end
end end
def with_inline_jobs
queue_adapter = config.active_job.queue_adapter
ActiveSupport.on_load(:active_job) do
self.queue_adapter = :inline
end
yield
ensure
ActiveSupport.on_load(:active_job) do
self.queue_adapter = queue_adapter
end
end
def has_migrations? def has_migrations?
paths["db/migrate"].existent.any? paths["db/migrate"].existent.any?
end end

@ -879,6 +879,18 @@ def index
assert Bukkits::Engine.config.bukkits_seeds_loaded assert Bukkits::Engine.config.bukkits_seeds_loaded
end end
test "jobs are ran inline while loading seeds" do
app_file "db/seeds.rb", <<-RUBY
Rails.application.config.seed_queue_adapter = ActiveJob::Base.queue_adapter
RUBY
boot_rails
Rails.application.load_seed
assert_instance_of ActiveJob::QueueAdapters::InlineAdapter, Rails.application.config.seed_queue_adapter
assert_instance_of ActiveJob::QueueAdapters::AsyncAdapter, ActiveJob::Base.queue_adapter
end
test "skips nonexistent seed data" do test "skips nonexistent seed data" do
FileUtils.rm "#{app_path}/db/seeds.rb" FileUtils.rm "#{app_path}/db/seeds.rb"
boot_rails boot_rails