Skip to content

Commit

Permalink
Merge pull request #19535 from Fryguy/no_seed_on_rails_s
Browse files Browse the repository at this point in the history
Remove seed on rails server start in development.
  • Loading branch information
jrafanie authored Nov 22, 2019
2 parents b1749cd + 56fab2e commit 0c596c4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 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 @@ -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?

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
42 changes: 41 additions & 1 deletion spec/lib/evm_database_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 0c596c4

Please sign in to comment.