2018-07-17 16:50:37 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-04 10:13:33 +00:00
|
|
|
module Projects
|
2020-02-11 18:08:58 +00:00
|
|
|
class PropagateServiceTemplate
|
2017-05-04 10:13:33 +00:00
|
|
|
BATCH_SIZE = 100
|
|
|
|
|
2020-04-22 15:09:27 +00:00
|
|
|
delegate :data_fields_present?, to: :template
|
|
|
|
|
|
|
|
def self.propagate(template)
|
|
|
|
new(template).propagate
|
2017-05-04 10:13:33 +00:00
|
|
|
end
|
|
|
|
|
2020-02-11 18:08:58 +00:00
|
|
|
def initialize(template)
|
|
|
|
@template = template
|
2017-05-04 10:13:33 +00:00
|
|
|
end
|
|
|
|
|
2017-05-04 16:11:28 +00:00
|
|
|
def propagate
|
2020-04-22 15:09:27 +00:00
|
|
|
return unless template.active?
|
2017-05-04 10:13:33 +00:00
|
|
|
|
2020-02-11 18:08:58 +00:00
|
|
|
propagate_projects_with_template
|
2017-05-04 10:13:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-04-22 15:09:27 +00:00
|
|
|
attr_reader :template
|
|
|
|
|
2020-02-11 18:08:58 +00:00
|
|
|
def propagate_projects_with_template
|
2017-05-04 10:13:33 +00:00
|
|
|
loop do
|
2019-06-25 21:47:17 +00:00
|
|
|
batch = Project.uncached { project_ids_batch }
|
2017-05-04 10:13:33 +00:00
|
|
|
|
2020-02-11 18:08:58 +00:00
|
|
|
bulk_create_from_template(batch) unless batch.empty?
|
2017-05-04 10:13:33 +00:00
|
|
|
|
2017-05-04 16:11:28 +00:00
|
|
|
break if batch.size < BATCH_SIZE
|
2017-05-04 10:13:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-11 18:08:58 +00:00
|
|
|
def bulk_create_from_template(batch)
|
2017-05-05 12:43:22 +00:00
|
|
|
service_list = batch.map do |project_id|
|
2017-05-05 16:57:52 +00:00
|
|
|
service_hash.values << project_id
|
2017-05-05 07:40:44 +00:00
|
|
|
end
|
|
|
|
|
2017-05-05 14:16:02 +00:00
|
|
|
Project.transaction do
|
2020-04-22 15:09:27 +00:00
|
|
|
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
|
|
|
|
|
2017-05-05 14:16:02 +00:00
|
|
|
run_callbacks(batch)
|
|
|
|
end
|
2017-05-04 10:13:33 +00:00
|
|
|
end
|
|
|
|
|
2017-05-05 08:43:56 +00:00
|
|
|
def project_ids_batch
|
2017-05-05 08:51:25 +00:00
|
|
|
Project.connection.select_values(
|
2017-05-05 07:40:44 +00:00
|
|
|
<<-SQL
|
|
|
|
SELECT id
|
|
|
|
FROM projects
|
|
|
|
WHERE NOT EXISTS (
|
|
|
|
SELECT true
|
|
|
|
FROM services
|
|
|
|
WHERE services.project_id = projects.id
|
2020-04-22 15:09:27 +00:00
|
|
|
AND services.type = #{ActiveRecord::Base.connection.quote(template.type)}
|
2017-05-05 07:40:44 +00:00
|
|
|
)
|
2017-05-05 16:01:33 +00:00
|
|
|
AND projects.pending_delete = false
|
|
|
|
AND projects.archived = false
|
2017-05-05 08:43:56 +00:00
|
|
|
LIMIT #{BATCH_SIZE}
|
2020-04-22 15:09:27 +00:00
|
|
|
SQL
|
2017-05-05 08:51:25 +00:00
|
|
|
)
|
2017-05-05 07:40:44 +00:00
|
|
|
end
|
|
|
|
|
2020-04-22 15:09:27 +00:00
|
|
|
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])
|
2017-05-05 16:01:33 +00:00
|
|
|
end
|
|
|
|
|
2017-05-05 07:40:44 +00:00
|
|
|
def service_hash
|
2020-04-22 15:09:27 +00:00
|
|
|
@service_hash ||= template.as_json(methods: :type, except: %w[id template project_id])
|
|
|
|
end
|
2017-05-05 12:43:22 +00:00
|
|
|
|
2020-04-22 15:09:27 +00:00
|
|
|
def data_hash
|
|
|
|
@data_hash ||= template.data_fields.as_json(only: template.data_fields.class.column_names).except('id', 'service_id')
|
2017-05-04 10:13:33 +00:00
|
|
|
end
|
2017-05-05 14:16:02 +00:00
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-05-05 14:16:02 +00:00
|
|
|
def run_callbacks(batch)
|
|
|
|
if active_external_issue_tracker?
|
|
|
|
Project.where(id: batch).update_all(has_external_issue_tracker: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
if active_external_wiki?
|
|
|
|
Project.where(id: batch).update_all(has_external_wiki: true)
|
|
|
|
end
|
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-05-05 14:16:02 +00:00
|
|
|
|
|
|
|
def active_external_issue_tracker?
|
2020-04-22 15:09:27 +00:00
|
|
|
template.issue_tracker? && !template.default
|
2017-05-05 14:16:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def active_external_wiki?
|
2020-04-22 15:09:27 +00:00
|
|
|
template.type == 'ExternalWikiService'
|
2017-05-05 14:16:02 +00:00
|
|
|
end
|
2017-05-04 10:13:33 +00:00
|
|
|
end
|
|
|
|
end
|