2019-11-15 07:06:12 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Gitlab::Seeder::Users
|
|
|
|
include ActionView::Helpers::NumberHelper
|
|
|
|
|
|
|
|
RANDOM_USERS_COUNT = 20
|
2022-03-30 08:09:14 -04:00
|
|
|
MASS_NAMESPACES_COUNT = ENV['CI'] ? 1 : 100
|
2019-11-15 07:06:12 -05:00
|
|
|
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-03-28 20:09:12 -04:00
|
|
|
create_mass_namespaces!
|
2019-11-15 07:06:12 -05:00
|
|
|
create_random_users!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def create_mass_users!
|
2020-09-18 02:09:31 -04:00
|
|
|
encrypted_password = Devise::Encryptor.digest(User, random_password)
|
2019-11-15 07:06:12 -05:00
|
|
|
|
|
|
|
Gitlab::Seeder.with_mass_insert(MASS_USERS_COUNT, User) do
|
|
|
|
ActiveRecord::Base.connection.execute <<~SQL
|
2022-03-28 20:09:12 -04:00
|
|
|
INSERT INTO users (username, name, email, state, confirmed_at, projects_limit, encrypted_password)
|
2019-11-15 07:06:12 -05:00
|
|
|
SELECT
|
2019-11-20 07:06:01 -05:00
|
|
|
'#{Gitlab::Seeder::MASS_INSERT_USER_START}' || seq,
|
2019-11-15 07:06:12 -05:00
|
|
|
'Seed user ' || seq,
|
|
|
|
'seed_user' || seq || '@example.com',
|
2022-03-28 20:09:12 -04:00
|
|
|
'active',
|
2019-11-15 07:06:12 -05:00
|
|
|
to_timestamp(seq),
|
|
|
|
#{MASS_USERS_COUNT},
|
|
|
|
'#{encrypted_password}'
|
|
|
|
FROM generate_series(1, #{MASS_USERS_COUNT}) AS seq
|
2022-03-28 20:09:12 -04:00
|
|
|
ON CONFLICT DO NOTHING;
|
2019-11-15 07:06:12 -05:00
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
|
|
|
relation = User.where(admin: false)
|
2022-03-28 20:09:12 -04:00
|
|
|
Gitlab::Seeder.with_mass_insert(relation.count, 'user namespaces') do
|
2019-11-15 07:06:12 -05:00
|
|
|
ActiveRecord::Base.connection.execute <<~SQL
|
2021-12-06 16:10:14 -05:00
|
|
|
INSERT INTO namespaces (name, path, owner_id, type)
|
2019-11-15 07:06:12 -05:00
|
|
|
SELECT
|
|
|
|
username,
|
|
|
|
username,
|
2021-12-06 16:10:14 -05:00
|
|
|
id,
|
|
|
|
'User'
|
2019-11-15 07:06:12 -05:00
|
|
|
FROM users WHERE NOT admin
|
2022-03-28 20:09:12 -04:00
|
|
|
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-11-15 07:06:12 -05:00
|
|
|
SQL
|
|
|
|
end
|
2020-09-18 02:09:31 -04:00
|
|
|
|
|
|
|
puts '==========================================================='
|
|
|
|
puts "INFO: Password for newly created users is: #{random_password}"
|
|
|
|
puts '==========================================================='
|
2019-11-15 07:06:12 -05:00
|
|
|
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-09-18 02:09:31 -04:00
|
|
|
password: random_password
|
2019-11-15 07:06:12 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
print '.'
|
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
print 'F'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-09-18 02:09:31 -04:00
|
|
|
|
2022-03-28 20:09:12 -04:00
|
|
|
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-09-18 02:09:31 -04:00
|
|
|
def random_password
|
|
|
|
@random_password ||= SecureRandom.hex.slice(0,16)
|
|
|
|
end
|
2019-11-15 07:06:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
Gitlab::Seeder.quiet do
|
|
|
|
users = Gitlab::Seeder::Users.new
|
|
|
|
users.seed!
|
|
|
|
end
|