diff --git a/app/models/mixins/miq_web_server_worker_mixin.rb b/app/models/mixins/miq_web_server_worker_mixin.rb index da0eefe20b1..82c50d49a44 100644 --- a/app/models/mixins/miq_web_server_worker_mixin.rb +++ b/app/models/mixins/miq_web_server_worker_mixin.rb @@ -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 diff --git a/lib/evm_database.rb b/lib/evm_database.rb index b07c60bac7e..0b364837d2b 100644 --- a/lib/evm_database.rb +++ b/lib/evm_database.rb @@ -78,8 +78,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? diff --git a/lib/vmdb/initializer.rb b/lib/vmdb/initializer.rb index 1f1e7c08faa..4556bd98ea8 100644 --- a/lib/vmdb/initializer.rb +++ b/lib/vmdb/initializer.rb @@ -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") diff --git a/spec/lib/evm_database_spec.rb b/spec/lib/evm_database_spec.rb index d8e305e6441..c5aa95e3328 100644 --- a/spec/lib/evm_database_spec.rb +++ b/spec/lib/evm_database_spec.rb @@ -73,6 +73,46 @@ end end + def simulate_primordial_seed + described_class.seed(["MiqDatabase", "MiqRegion"]) + end + + def simulate_full_seed + described_class.seed(["MiqDatabase", "MiqRegion", "MiqAction"]) + end + + describe ".seeded_primordially?" do + it "when not seeded" do + expect(EvmDatabase.seeded_primordially?).to be false + end + + it "when seeded primordially" do + simulate_primordial_seed + expect(EvmDatabase.seeded_primordially?).to be true + end + + it "when fully seeded" do + simulate_full_seed + 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_primordial_seed + expect(EvmDatabase.seeded?).to be false + end + + it "when fully seeded" do + simulate_full_seed + 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) @@ -85,7 +125,7 @@ end it "will skip when SKIP_SEEDING is set and the database is seeded" do - MiqDatabase.seed + simulate_primordial_seed expect(ENV).to receive(:[]).with("SKIP_SEEDING").and_return("true") expect(described_class.send(:skip_seeding?)).to be_truthy end