summaryrefslogtreecommitdiff
path: root/db/fixtures/development/02_users.rb
blob: 6e0b37d72583dc86357f07c1d542ea6be0e10dd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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
  MASS_INSERT_USERNAME_START = 'mass_insert_user_'

  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!
    encrypted_password = Devise::Encryptor.digest(User, '12345678')

    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
          '#{MASS_INSERT_USERNAME_START}' || seq,
          '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
  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,
          password: '12345678'
        )

        print '.'
      rescue ActiveRecord::RecordInvalid
        print 'F'
      end
    end
  end
end

Gitlab::Seeder.quiet do
  users = Gitlab::Seeder::Users.new
  users.seed!
end