diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-05-20 17:34:42 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-05-20 17:34:42 +0300 |
commit | 9f46488805e86b1bc341ea1620b866016c2ce5ed (patch) | |
tree | f9748c7e287041e37d6da49e0a29c9511dc34768 /app/services/projects/propagate_service_template.rb | |
parent | dfc92d081ea0332d69c8aca2f0e745cb48ae5e6d (diff) |
Add latest changes from gitlab-org/gitlab@13-0-stable-ee
Diffstat (limited to 'app/services/projects/propagate_service_template.rb')
-rw-r--r-- | app/services/projects/propagate_service_template.rb | 54 |
1 files changed, 27 insertions, 27 deletions
diff --git a/app/services/projects/propagate_service_template.rb b/app/services/projects/propagate_service_template.rb index 6013b00b8c6..0483c951f1e 100644 --- a/app/services/projects/propagate_service_template.rb +++ b/app/services/projects/propagate_service_template.rb @@ -4,8 +4,10 @@ module Projects class PropagateServiceTemplate BATCH_SIZE = 100 - def self.propagate(*args) - new(*args).propagate + delegate :data_fields_present?, to: :template + + def self.propagate(template) + new(template).propagate end def initialize(template) @@ -13,15 +15,15 @@ module Projects end def propagate - return unless @template.active? - - Rails.logger.info("Propagating services for template #{@template.id}") # rubocop:disable Gitlab/RailsLogger + return unless template.active? propagate_projects_with_template end private + attr_reader :template + def propagate_projects_with_template loop do batch = Project.uncached { project_ids_batch } @@ -38,7 +40,14 @@ module Projects end Project.transaction do - bulk_insert_services(service_hash.keys << 'project_id', service_list) + results = bulk_insert(Service, service_hash.keys << 'project_id', service_list) + + if data_fields_present? + data_list = results.map { |row| data_hash.values << row['id'] } + + bulk_insert(template.data_fields.class, data_hash.keys << 'service_id', data_list) + end + run_callbacks(batch) end end @@ -52,36 +61,27 @@ module Projects SELECT true FROM services WHERE services.project_id = projects.id - AND services.type = '#{@template.type}' + AND services.type = #{ActiveRecord::Base.connection.quote(template.type)} ) AND projects.pending_delete = false AND projects.archived = false LIMIT #{BATCH_SIZE} - SQL + SQL ) end - def bulk_insert_services(columns, values_array) - ActiveRecord::Base.connection.execute( - <<-SQL.strip_heredoc - INSERT INTO services (#{columns.join(', ')}) - VALUES #{values_array.map { |tuple| "(#{tuple.join(', ')})" }.join(', ')} - SQL - ) + def bulk_insert(klass, columns, values_array) + items_to_insert = values_array.map { |array| Hash[columns.zip(array)] } + + klass.insert_all(items_to_insert, returning: [:id]) end def service_hash - @service_hash ||= - begin - template_hash = @template.as_json(methods: :type).except('id', 'template', 'project_id') - - template_hash.each_with_object({}) do |(key, value), service_hash| - value = value.is_a?(Hash) ? value.to_json : value + @service_hash ||= template.as_json(methods: :type, except: %w[id template project_id]) + end - service_hash[ActiveRecord::Base.connection.quote_column_name(key)] = - ActiveRecord::Base.connection.quote(value) - end - end + def data_hash + @data_hash ||= template.data_fields.as_json(only: template.data_fields.class.column_names).except('id', 'service_id') end # rubocop: disable CodeReuse/ActiveRecord @@ -97,11 +97,11 @@ module Projects # rubocop: enable CodeReuse/ActiveRecord def active_external_issue_tracker? - @template.issue_tracker? && !@template.default + template.issue_tracker? && !template.default end def active_external_wiki? - @template.type == 'ExternalWikiService' + template.type == 'ExternalWikiService' end end end |