Skip to content

Commit

Permalink
Remove seed on rails server start in development.
Browse files Browse the repository at this point in the history
In a production setting, evmserverd will be resposible for seeding the
database.  In a development setting, we should not be seeding when we
start the UI.  Developers should instead be encouraged to use bin/update
or call db:seed directly when changes are made.  Since most developers
need to call bin/update before starting a rails server, in order to
build the UI assets, this is part of the normal workflow.

This change should eliminate about 1 second at rails server boot in
development.
  • Loading branch information
Fryguy committed Nov 20, 2019
1 parent 5121c7c commit 97a7929
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
1 change: 1 addition & 0 deletions app/models/mixins/miq_web_server_worker_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def preload_for_console
end

def preload_for_worker_role
raise "Expected database to be seeded via `rake db:seed`." unless EvmDatabase.seeded_primordially?
configure_secret_token
end

Expand Down
20 changes: 19 additions & 1 deletion lib/evm_database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,26 @@ def self.seed_rest
seed(OTHER_SEEDABLE_CLASSES)
end

# Returns whether or not a primordial seed has completed.
def self.seeded_primordially?
# While not technically accurate, as someone could just insert a record
# directly, this is the simplest check at the moment to guess whether or not
# a primordial seed has completed.
MiqDatabase.any? && MiqRegion.in_my_region.any?
end

# Returns whether or not a full seed has completed.
def self.seeded?
# While not technically accurate, as someone could just insert a record
# directly, this is the simplest check at the moment to guess whether or not
# a full seed has completed.
#
# MiqAction was chosen because it cannot be added by a user directly.
seeded_primordially? && MiqAction.in_my_region.any?
end

def self.skip_seeding?
ENV['SKIP_SEEDING'] && MiqDatabase.any?
ENV['SKIP_SEEDING'] && seeded_primordially?
end
private_class_method :skip_seeding?

Expand Down
3 changes: 0 additions & 3 deletions lib/vmdb/initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ def self.init
# * command line(rails server)
# * debugger
if defined?(Rails::Server)
# preload_for_worker_role depends on seeding, principally MiqDatabase
EvmDatabase.seed_primordial

MiqUiWorker.preload_for_worker_role
MiqServer.my_server.starting_server_record
MiqServer.my_server.update(:status => "started")
Expand Down
40 changes: 40 additions & 0 deletions spec/lib/evm_database_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,46 @@
end
end

describe ".seeded_primordially?" do
it "when not seeded" do
expect(EvmDatabase.seeded_primordially?).to be false
end

it "when seeded primordially" do
# Simulate part of a primordial seed
described_class.seed(["MiqDatabase", "MiqRegion"])

expect(EvmDatabase.seeded_primordially?).to be true
end

it "when fully seeded" do
# Simulate part of a primordial seed and part of a full seed
described_class.seed(["MiqDatabase", "MiqRegion", "MiqAction"])

expect(EvmDatabase.seeded_primordially?).to be true
end
end

describe ".seeded?" do
it "when not seeded" do
expect(EvmDatabase.seeded?).to be false
end

it "when seeded primordially" do
# Simulate part of a primordial seed
described_class.seed(["MiqDatabase", "MiqRegion"])

expect(EvmDatabase.seeded?).to be false
end

it "when fully seeded" do
# Simulate part of a primordial seed and part of a full seed
described_class.seed(["MiqDatabase", "MiqRegion", "MiqAction"])

expect(EvmDatabase.seeded?).to be true
end
end

describe ".skip_seeding? (private)" do
it "will not skip when SKIP_SEEDING is not set" do
expect(ENV).to receive(:[]).with("SKIP_SEEDING").and_return(nil)
Expand Down

0 comments on commit 97a7929

Please sign in to comment.