debian-mirror-gitlab/lib/gitlab/seeder.rb

123 lines
3.6 KiB
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
module Gitlab
class Seeder
2019-12-26 22:10:19 +05:30
extend ActionView::Helpers::NumberHelper
2020-01-01 13:55:28 +05:30
MASS_INSERT_PROJECT_START = 'mass_insert_project_'
MASS_INSERT_USER_START = 'mass_insert_user_'
2020-07-28 23:09:34 +05:30
REPORTED_USER_START = 'reported_user_'
2019-12-26 22:10:19 +05:30
ESTIMATED_INSERT_PER_MINUTE = 2_000_000
MASS_INSERT_ENV = 'MASS_INSERT'
module ProjectSeed
extend ActiveSupport::Concern
included do
scope :not_mass_generated, -> do
2020-01-01 13:55:28 +05:30
where.not("path LIKE '#{MASS_INSERT_PROJECT_START}%'")
2019-12-26 22:10:19 +05:30
end
end
end
module UserSeed
extend ActiveSupport::Concern
included do
scope :not_mass_generated, -> do
2020-07-28 23:09:34 +05:30
where.not("username LIKE '#{MASS_INSERT_USER_START}%' OR username LIKE '#{REPORTED_USER_START}%'")
2019-12-26 22:10:19 +05:30
end
end
end
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-10-24 23:57:45 +05:30
if estimated_minutes == 0
2019-12-26 22:10:19 +05:30
"Rough estimated time: less than a minute ⏰"
else
"Rough estimated time: #{estimated_minutes} #{humanized_minutes}"
end
end
2014-09-02 18:07:02 +05:30
def self.quiet
2019-12-26 22:10:19 +05:30
# Disable database insertion logs so speed isn't limited by ability to print to console
old_logger = ActiveRecord::Base.logger
ActiveRecord::Base.logger = nil
# Additional seed logic for models.
Project.include(ProjectSeed)
User.include(UserSeed)
2021-11-11 11:23:49 +05:30
old_perform_deliveries = ActionMailer::Base.perform_deliveries
ActionMailer::Base.perform_deliveries = false
2018-03-17 18:26:18 +05:30
2014-09-02 18:07:02 +05:30
SeedFu.quiet = true
2017-08-17 22:00:37 +05:30
2021-01-03 14:25:43 +05:30
without_statement_timeout do
2021-11-11 11:23:49 +05:30
without_new_note_notifications do
yield
end
2021-01-03 14:25:43 +05:30
end
2017-08-17 22:00:37 +05:30
2021-11-11 11:23:49 +05:30
puts "\nOK".color(:green)
ensure
2014-09-02 18:07:02 +05:30
SeedFu.quiet = false
2021-11-11 11:23:49 +05:30
ActionMailer::Base.perform_deliveries = old_perform_deliveries
2019-12-26 22:10:19 +05:30
ActiveRecord::Base.logger = old_logger
2014-09-02 18:07:02 +05:30
end
2019-03-02 22:35:43 +05:30
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-11-11 11:23:49 +05:30
def self.without_new_note_notifications
NotificationService.alias_method :original_new_note, :new_note
NotificationService.define_method(:new_note) { |note| }
2018-03-17 18:26:18 +05:30
2021-11-11 11:23:49 +05:30
yield
ensure
NotificationService.alias_method :new_note, :original_new_note
NotificationService.remove_method :original_new_note
2014-09-02 18:07:02 +05:30
end
2021-01-03 14:25:43 +05:30
def self.without_statement_timeout
ActiveRecord::Base.connection.execute('SET statement_timeout=0')
yield
ensure
ActiveRecord::Base.connection.execute('RESET statement_timeout')
end
2014-09-02 18:07:02 +05:30
end
end
2018-03-17 18:26:18 +05:30
# :nocov: