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

190 lines
6.2 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
2022-06-21 17:19:12 +05:30
MASS_NAMESPACES_COUNT = ENV['CI'] ? 1 : 100
2019-12-26 22:10:19 +05:30
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!
2022-06-21 17:19:12 +05:30
create_mass_namespaces!
2019-12-26 22:10:19 +05:30
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
2022-06-21 17:19:12 +05:30
INSERT INTO users (username, name, email, state, confirmed_at, projects_limit, encrypted_password)
2019-12-26 22:10:19 +05:30
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',
2022-06-21 17:19:12 +05:30
'active',
2019-12-26 22:10:19 +05:30
to_timestamp(seq),
#{MASS_USERS_COUNT},
'#{encrypted_password}'
FROM generate_series(1, #{MASS_USERS_COUNT}) AS seq
2022-06-21 17:19:12 +05:30
ON CONFLICT DO NOTHING;
2019-12-26 22:10:19 +05:30
SQL
end
relation = User.where(admin: false)
2022-06-21 17:19:12 +05:30
Gitlab::Seeder.with_mass_insert(relation.count, 'user namespaces') do
2019-12-26 22:10:19 +05:30
ActiveRecord::Base.connection.execute <<~SQL
2022-01-26 12:08:38 +05:30
INSERT INTO namespaces (name, path, owner_id, type)
2019-12-26 22:10:19 +05:30
SELECT
username,
username,
2022-01-26 12:08:38 +05:30
id,
'User'
2019-12-26 22:10:19 +05:30
FROM users WHERE NOT admin
2022-06-21 17:19:12 +05:30
ON CONFLICT DO NOTHING;
SQL
end
Gitlab::Seeder.with_mass_insert(relation.count, "User namespaces routes") do
ActiveRecord::Base.connection.execute <<~SQL
INSERT INTO routes (namespace_id, source_id, source_type, path, name)
SELECT id as namespace_id, id as source_id, 'Namespace', path, name
FROM namespaces WHERE type IS NULL OR type = 'User'
ON CONFLICT DO NOTHING;
2019-12-26 22:10:19 +05:30
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
2022-06-21 17:19:12 +05:30
def create_mass_namespaces!
Gitlab::Seeder.with_mass_insert(MASS_NAMESPACES_COUNT, "root namespaces and subgroups 9 levels deep") do
ActiveRecord::Base.connection.execute <<~SQL
INSERT INTO namespaces (name, path, type)
SELECT
'mass insert group level 0 - ' || seq,
'#{Gitlab::Seeder::MASS_INSERT_GROUP_START}_0_' || seq,
'Group'
FROM generate_series(1, #{MASS_NAMESPACES_COUNT}) AS seq
ON CONFLICT DO NOTHING;
SQL
(1..9).each do |idx|
count = Namespace.where("path LIKE '#{Gitlab::Seeder::MASS_INSERT_PREFIX}%'").where(type: 'Group').count * 2
Gitlab::Seeder.log_message("Creating subgroups at level #{idx}: #{count}")
ActiveRecord::Base.connection.execute <<~SQL
INSERT INTO namespaces (name, path, type, parent_id)
SELECT
'mass insert group level #{idx} - ' || seq,
'#{Gitlab::Seeder::MASS_INSERT_GROUP_START}_#{idx}_' || seq,
'Group',
namespaces.id
FROM namespaces
CROSS JOIN generate_series(1, 2) AS seq
WHERE namespaces.type='Group' AND namespaces.path like '#{Gitlab::Seeder::MASS_INSERT_GROUP_START}_#{idx-1}_%'
ON CONFLICT DO NOTHING;
SQL
end
Gitlab::Seeder.log_message("creating routes.")
ActiveRecord::Base.connection.execute <<~SQL
WITH RECURSIVE cte(source_id, namespace_id, parent_id, path, height) AS (
(
SELECT ARRAY[batch.id], batch.id, batch.parent_id, batch.path, 1
FROM
"namespaces" as batch
WHERE
"batch"."type" = 'Group' AND "batch"."parent_id" is null
)
UNION
(
SELECT array_append(cte.source_id, n.id), n.id, n.parent_id, cte.path || '/' || n.path, cte.height+1
FROM
"namespaces" as n,
"cte"
WHERE
"n"."type" = 'Group'
AND "n"."parent_id" = "cte"."namespace_id"
)
)
INSERT INTO routes (namespace_id, source_id, source_type, path, name)
SELECT cte.namespace_id as namespace_id, cte.namespace_id as source_id, 'Namespace', cte.path, cte.path FROM cte
ON CONFLICT DO NOTHING;
SQL
Gitlab::Seeder.log_message("filling traversal ids.")
ActiveRecord::Base.connection.execute <<~SQL
WITH RECURSIVE cte(source_id, namespace_id, parent_id) AS (
(
SELECT ARRAY[batch.id], batch.id, batch.parent_id
FROM
"namespaces" as batch
WHERE
"batch"."type" = 'Group' AND "batch"."parent_id" is null
)
UNION
(
SELECT array_append(cte.source_id, n.id), n.id, n.parent_id
FROM
"namespaces" as n,
"cte"
WHERE
"n"."type" = 'Group'
AND "n"."parent_id" = "cte"."namespace_id"
)
)
UPDATE namespaces
SET traversal_ids = computed.source_id FROM (SELECT namespace_id, source_id FROM cte) AS computed
where computed.namespace_id = namespaces.id AND namespaces.path LIKE '#{Gitlab::Seeder::MASS_INSERT_PREFIX}%'
SQL
Gitlab::Seeder.log_message("creating namespace settings.")
ActiveRecord::Base.connection.execute <<~SQL
INSERT INTO namespace_settings(namespace_id, created_at, updated_at)
SELECT id, now(), now() FROM namespaces
ON CONFLICT DO NOTHING;
SQL
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