2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2012-11-13 15:11:56 -05:00
|
|
|
module Gitlab
|
|
|
|
class Seeder
|
2019-11-15 07:06:12 -05:00
|
|
|
extend ActionView::Helpers::NumberHelper
|
|
|
|
|
2022-03-28 20:09:12 -04:00
|
|
|
MASS_INSERT_PREFIX = 'mass_insert'
|
|
|
|
MASS_INSERT_PROJECT_START = "#{MASS_INSERT_PREFIX}_project_"
|
|
|
|
MASS_INSERT_GROUP_START = "#{MASS_INSERT_PREFIX}_group_"
|
|
|
|
MASS_INSERT_USER_START = "#{MASS_INSERT_PREFIX}_user_"
|
2020-07-02 08:09:02 -04:00
|
|
|
REPORTED_USER_START = 'reported_user_'
|
2022-03-28 20:09:12 -04:00
|
|
|
ESTIMATED_INSERT_PER_MINUTE = 250_000
|
2019-11-15 07:06:12 -05:00
|
|
|
MASS_INSERT_ENV = 'MASS_INSERT'
|
|
|
|
|
2022-03-28 20:09:12 -04:00
|
|
|
module NamespaceSeed
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
scope :not_mass_generated, -> do
|
|
|
|
where.not("path LIKE '#{MASS_INSERT_GROUP_START}%'")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-15 07:06:12 -05:00
|
|
|
module ProjectSeed
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
scope :not_mass_generated, -> do
|
2019-11-20 07:06:01 -05:00
|
|
|
where.not("path LIKE '#{MASS_INSERT_PROJECT_START}%'")
|
2019-11-15 07:06:12 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module UserSeed
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
scope :not_mass_generated, -> do
|
2020-07-02 08:09:02 -04:00
|
|
|
where.not("username LIKE '#{MASS_INSERT_USER_START}%' OR username LIKE '#{REPORTED_USER_START}%'")
|
2019-11-15 07:06:12 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-28 20:09:12 -04:00
|
|
|
def self.log_message(message)
|
|
|
|
puts "#{Time.current}: #{message}"
|
|
|
|
end
|
|
|
|
|
2019-11-15 07:06:12 -05:00
|
|
|
def self.with_mass_insert(size, model)
|
|
|
|
humanized_model_name = model.is_a?(String) ? model : model.model_name.human.pluralize(size)
|
|
|
|
|
|
|
|
if !ENV[MASS_INSERT_ENV] && !ENV['CI']
|
|
|
|
puts "\nSkipping mass insertion for #{humanized_model_name}."
|
|
|
|
puts "Consider running the seed with #{MASS_INSERT_ENV}=1"
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
humanized_size = number_with_delimiter(size)
|
|
|
|
estimative = estimated_time_message(size)
|
|
|
|
|
|
|
|
puts "\nCreating #{humanized_size} #{humanized_model_name}."
|
|
|
|
puts estimative
|
|
|
|
|
|
|
|
yield
|
|
|
|
|
|
|
|
puts "\n#{number_with_delimiter(size)} #{humanized_model_name} created!"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.estimated_time_message(size)
|
|
|
|
estimated_minutes = (size.to_f / ESTIMATED_INSERT_PER_MINUTE).round
|
|
|
|
humanized_minutes = 'minute'.pluralize(estimated_minutes)
|
|
|
|
|
2020-08-05 11:09:59 -04:00
|
|
|
if estimated_minutes == 0
|
2019-11-15 07:06:12 -05:00
|
|
|
"Rough estimated time: less than a minute ⏰"
|
|
|
|
else
|
|
|
|
"Rough estimated time: #{estimated_minutes} #{humanized_minutes} ⏰"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-13 15:11:56 -05:00
|
|
|
def self.quiet
|
2019-11-15 07:06:12 -05:00
|
|
|
# Additional seed logic for models.
|
2022-03-28 20:09:12 -04:00
|
|
|
Namespace.include(NamespaceSeed)
|
2019-11-15 07:06:12 -05:00
|
|
|
Project.include(ProjectSeed)
|
|
|
|
User.include(UserSeed)
|
|
|
|
|
2021-09-07 23:09:25 -04:00
|
|
|
old_perform_deliveries = ActionMailer::Base.perform_deliveries
|
|
|
|
ActionMailer::Base.perform_deliveries = false
|
2017-12-01 15:01:15 -05:00
|
|
|
|
2012-11-13 15:11:56 -05:00
|
|
|
SeedFu.quiet = true
|
2017-03-02 18:18:50 -05:00
|
|
|
|
2022-03-09 13:08:16 -05:00
|
|
|
without_database_logging do
|
|
|
|
without_statement_timeout do
|
|
|
|
without_new_note_notifications do
|
|
|
|
yield
|
|
|
|
end
|
2021-09-07 23:09:25 -04:00
|
|
|
end
|
2020-10-05 14:08:51 -04:00
|
|
|
end
|
2017-03-02 18:18:50 -05:00
|
|
|
|
2021-09-07 23:09:25 -04:00
|
|
|
puts "\nOK".color(:green)
|
|
|
|
ensure
|
2012-11-13 15:11:56 -05:00
|
|
|
SeedFu.quiet = false
|
2021-09-07 23:09:25 -04:00
|
|
|
ActionMailer::Base.perform_deliveries = old_perform_deliveries
|
2012-11-13 15:11:56 -05:00
|
|
|
end
|
2014-03-15 05:39:35 -04:00
|
|
|
|
2019-01-07 16:05:35 -05:00
|
|
|
def self.without_gitaly_timeout
|
|
|
|
# Remove Gitaly timeout
|
|
|
|
old_timeout = Gitlab::CurrentSettings.current_application_settings.gitaly_timeout_default
|
|
|
|
Gitlab::CurrentSettings.current_application_settings.update_columns(gitaly_timeout_default: 0)
|
|
|
|
# Otherwise we still see the default value when running seed_fu
|
|
|
|
ApplicationSetting.expire
|
|
|
|
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
Gitlab::CurrentSettings.current_application_settings.update_columns(gitaly_timeout_default: old_timeout)
|
|
|
|
ApplicationSetting.expire
|
|
|
|
end
|
|
|
|
|
2021-09-07 23:09:25 -04:00
|
|
|
def self.without_new_note_notifications
|
|
|
|
NotificationService.alias_method :original_new_note, :new_note
|
|
|
|
NotificationService.define_method(:new_note) { |note| }
|
2018-01-26 18:25:05 -05:00
|
|
|
|
2021-09-07 23:09:25 -04:00
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
NotificationService.alias_method :new_note, :original_new_note
|
|
|
|
NotificationService.remove_method :original_new_note
|
2014-03-15 05:39:35 -04:00
|
|
|
end
|
2020-10-05 14:08:51 -04:00
|
|
|
|
|
|
|
def self.without_statement_timeout
|
2022-03-09 13:08:16 -05:00
|
|
|
Gitlab::Database::EachDatabase.each_database_connection do |connection|
|
|
|
|
connection.execute('SET statement_timeout=0')
|
|
|
|
end
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
Gitlab::Database::EachDatabase.each_database_connection do |connection|
|
|
|
|
connection.execute('RESET statement_timeout')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.without_database_logging
|
|
|
|
old_loggers = Gitlab::Database.database_base_models.transform_values do |model|
|
|
|
|
model.logger
|
|
|
|
end
|
|
|
|
|
|
|
|
Gitlab::Database.database_base_models.each do |_, model|
|
|
|
|
model.logger = nil
|
|
|
|
end
|
|
|
|
|
2020-10-05 14:08:51 -04:00
|
|
|
yield
|
|
|
|
ensure
|
2022-03-09 13:08:16 -05:00
|
|
|
Gitlab::Database.database_base_models.each do |connection_name, model|
|
|
|
|
model.logger = old_loggers[connection_name]
|
|
|
|
end
|
2020-10-05 14:08:51 -04:00
|
|
|
end
|
2012-11-13 15:11:56 -05:00
|
|
|
end
|
|
|
|
end
|
2017-08-10 19:09:14 -04:00
|
|
|
# :nocov:
|