Do not seed when a user exists

Previously the seed logic ran when `SEED_EMAIL` was unset and the `admin`
users email address was changed, or if `SEED_EMAIL` did not match it
anymore.

We run `db:seed` on every docker container start to make it easy to get
started with Huginn. That could lead to situations in which the
container did not start if the admin users email address was changed (the
Seeder failed due to the uniqueness constraint on `username`).

Since we "seed" the database I think it is best to bail out of the
seeding logic as long as at least one user exists.

Fixes #2362
This commit is contained in:
Dominik Sander 2018-10-03 11:55:46 +02:00
parent 6773dcf31e
commit 2cd37e2ab4

View file

@ -1,11 +1,11 @@
class Seeder class Seeder
def self.seed def self.seed
user = User.find_or_initialize_by(:email => ENV['SEED_EMAIL'].presence || "admin@example.com") if User.any?
if user.persisted? puts "At least one User already exists, not seeding."
puts "User with email '#{user.email}' already exists, not seeding."
exit exit
end end
user = User.find_or_initialize_by(:email => ENV['SEED_EMAIL'].presence || "admin@example.com")
user.username = ENV['SEED_USERNAME'].presence || "admin" user.username = ENV['SEED_USERNAME'].presence || "admin"
user.password = ENV['SEED_PASSWORD'].presence || "password" user.password = ENV['SEED_PASSWORD'].presence || "password"
user.password_confirmation = ENV['SEED_PASSWORD'].presence || "password" user.password_confirmation = ENV['SEED_PASSWORD'].presence || "password"