debian-mirror-gitlab/db/fixtures/development/02_users.rb

85 lines
2.1 KiB
Ruby
Raw Normal View History

2019-12-26 22:10:19 +05:30
# frozen_string_literal: true
class Gitlab::Seeder::Users
include ActionView::Helpers::NumberHelper
RANDOM_USERS_COUNT = 20
MASS_USERS_COUNT = ENV['CI'] ? 10 : 1_000_000
attr_reader :opts
def initialize(opts = {})
@opts = opts
end
def seed!
Sidekiq::Testing.inline! do
create_mass_users!
create_random_users!
end
end
private
def create_mass_users!
2020-11-24 15:15:51 +05:30
encrypted_password = Devise::Encryptor.digest(User, random_password)
2019-12-26 22:10:19 +05:30
Gitlab::Seeder.with_mass_insert(MASS_USERS_COUNT, User) do
ActiveRecord::Base.connection.execute <<~SQL
INSERT INTO users (username, name, email, confirmed_at, projects_limit, encrypted_password)
SELECT
2020-01-01 13:55:28 +05:30
'#{Gitlab::Seeder::MASS_INSERT_USER_START}' || seq,
2019-12-26 22:10:19 +05:30
'Seed user ' || seq,
'seed_user' || seq || '@example.com',
to_timestamp(seq),
#{MASS_USERS_COUNT},
'#{encrypted_password}'
FROM generate_series(1, #{MASS_USERS_COUNT}) AS seq
SQL
end
relation = User.where(admin: false)
Gitlab::Seeder.with_mass_insert(relation.count, Namespace) do
ActiveRecord::Base.connection.execute <<~SQL
INSERT INTO namespaces (name, path, owner_id)
SELECT
username,
username,
id
FROM users WHERE NOT admin
SQL
end
2020-11-24 15:15:51 +05:30
puts '==========================================================='
puts "INFO: Password for newly created users is: #{random_password}"
puts '==========================================================='
2019-12-26 22:10:19 +05:30
end
def create_random_users!
RANDOM_USERS_COUNT.times do |i|
begin
User.create!(
username: FFaker::Internet.user_name,
name: FFaker::Name.name,
email: FFaker::Internet.email,
confirmed_at: DateTime.now,
2020-11-24 15:15:51 +05:30
password: random_password
2019-12-26 22:10:19 +05:30
)
print '.'
rescue ActiveRecord::RecordInvalid
print 'F'
end
end
end
2020-11-24 15:15:51 +05:30
def random_password
@random_password ||= SecureRandom.hex.slice(0,16)
end
2019-12-26 22:10:19 +05:30
end
Gitlab::Seeder.quiet do
users = Gitlab::Seeder::Users.new
users.seed!
end