diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-06-18 14:18:50 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-06-18 14:18:50 +0300 |
commit | 8c7f4e9d5f36cff46365a7f8c4b9c21578c1e781 (patch) | |
tree | a77e7fe7a93de11213032ed4ab1f33a3db51b738 /app/services/projects/propagate_service_template.rb | |
parent | 00b35af3db1abfe813a778f643dad221aad51fca (diff) |
Add latest changes from gitlab-org/gitlab@13-1-stable-ee
Diffstat (limited to 'app/services/projects/propagate_service_template.rb')
-rw-r--r-- | app/services/projects/propagate_service_template.rb | 48 |
1 files changed, 22 insertions, 26 deletions
diff --git a/app/services/projects/propagate_service_template.rb b/app/services/projects/propagate_service_template.rb index 0483c951f1e..4adcda042d1 100644 --- a/app/services/projects/propagate_service_template.rb +++ b/app/services/projects/propagate_service_template.rb @@ -26,7 +26,7 @@ module Projects def propagate_projects_with_template loop do - batch = Project.uncached { project_ids_batch } + batch = Project.uncached { project_ids_without_integration } bulk_create_from_template(batch) unless batch.empty? @@ -35,40 +35,36 @@ module Projects end def bulk_create_from_template(batch) - service_list = batch.map do |project_id| - service_hash.values << project_id - end + service_list = ServiceList.new(batch, service_hash).to_array Project.transaction do - results = bulk_insert(Service, service_hash.keys << 'project_id', service_list) + results = bulk_insert(*service_list) if data_fields_present? - data_list = results.map { |row| data_hash.values << row['id'] } + data_list = DataList.new(results, data_fields_hash, template.data_fields.class).to_array - bulk_insert(template.data_fields.class, data_hash.keys << 'service_id', data_list) + bulk_insert(*data_list) end run_callbacks(batch) end end - def project_ids_batch - Project.connection.select_values( - <<-SQL - SELECT id - FROM projects - WHERE NOT EXISTS ( - SELECT true - FROM services - WHERE services.project_id = projects.id - AND services.type = #{ActiveRecord::Base.connection.quote(template.type)} - ) - AND projects.pending_delete = false - AND projects.archived = false - LIMIT #{BATCH_SIZE} - SQL - ) + # rubocop: disable CodeReuse/ActiveRecord + def project_ids_without_integration + services = Service + .select('1') + .where('services.project_id = projects.id') + .where(type: template.type) + + Project + .where('NOT EXISTS (?)', services) + .where(pending_delete: false) + .where(archived: false) + .limit(BATCH_SIZE) + .pluck(:id) end + # rubocop: enable CodeReuse/ActiveRecord def bulk_insert(klass, columns, values_array) items_to_insert = values_array.map { |array| Hash[columns.zip(array)] } @@ -77,11 +73,11 @@ module Projects end def service_hash - @service_hash ||= template.as_json(methods: :type, except: %w[id template project_id]) + @service_hash ||= template.to_service_hash end - def data_hash - @data_hash ||= template.data_fields.as_json(only: template.data_fields.class.column_names).except('id', 'service_id') + def data_fields_hash + @data_fields_hash ||= template.to_data_fields_hash end # rubocop: disable CodeReuse/ActiveRecord |