2018-07-17 12:50:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-04 06:13:33 -04:00
|
|
|
module Projects
|
2017-05-05 12:01:33 -04:00
|
|
|
class PropagateServiceTemplate
|
2017-05-04 06:13:33 -04:00
|
|
|
BATCH_SIZE = 100
|
|
|
|
|
2017-05-04 12:11:28 -04:00
|
|
|
def self.propagate(*args)
|
|
|
|
new(*args).propagate
|
2017-05-04 06:13:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(template)
|
|
|
|
@template = template
|
|
|
|
end
|
|
|
|
|
2017-05-04 12:11:28 -04:00
|
|
|
def propagate
|
2017-05-05 12:57:52 -04:00
|
|
|
return unless @template.active?
|
2017-05-04 06:13:33 -04:00
|
|
|
|
2019-07-10 15:26:47 -04:00
|
|
|
Rails.logger.info("Propagating services for template #{@template.id}") # rubocop:disable Gitlab/RailsLogger
|
2017-05-04 06:13:33 -04:00
|
|
|
|
|
|
|
propagate_projects_with_template
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def propagate_projects_with_template
|
|
|
|
loop do
|
2019-06-25 17:47:17 -04:00
|
|
|
batch = Project.uncached { project_ids_batch }
|
2017-05-04 06:13:33 -04:00
|
|
|
|
2017-05-05 08:43:22 -04:00
|
|
|
bulk_create_from_template(batch) unless batch.empty?
|
2017-05-04 06:13:33 -04:00
|
|
|
|
2017-05-04 12:11:28 -04:00
|
|
|
break if batch.size < BATCH_SIZE
|
2017-05-04 06:13:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-05 03:40:44 -04:00
|
|
|
def bulk_create_from_template(batch)
|
2017-05-05 08:43:22 -04:00
|
|
|
service_list = batch.map do |project_id|
|
2017-05-05 12:57:52 -04:00
|
|
|
service_hash.values << project_id
|
2017-05-05 03:40:44 -04:00
|
|
|
end
|
|
|
|
|
2017-05-05 10:16:02 -04:00
|
|
|
Project.transaction do
|
2017-05-05 12:57:52 -04:00
|
|
|
bulk_insert_services(service_hash.keys << 'project_id', service_list)
|
2017-05-05 10:16:02 -04:00
|
|
|
run_callbacks(batch)
|
|
|
|
end
|
2017-05-04 06:13:33 -04:00
|
|
|
end
|
|
|
|
|
2017-05-05 04:43:56 -04:00
|
|
|
def project_ids_batch
|
2017-05-05 04:51:25 -04:00
|
|
|
Project.connection.select_values(
|
2017-05-05 03:40:44 -04:00
|
|
|
<<-SQL
|
|
|
|
SELECT id
|
|
|
|
FROM projects
|
|
|
|
WHERE NOT EXISTS (
|
|
|
|
SELECT true
|
|
|
|
FROM services
|
|
|
|
WHERE services.project_id = projects.id
|
|
|
|
AND services.type = '#{@template.type}'
|
|
|
|
)
|
2017-05-05 12:01:33 -04:00
|
|
|
AND projects.pending_delete = false
|
|
|
|
AND projects.archived = false
|
2017-05-05 04:43:56 -04:00
|
|
|
LIMIT #{BATCH_SIZE}
|
2017-05-05 03:40:44 -04:00
|
|
|
SQL
|
2017-05-05 04:51:25 -04:00
|
|
|
)
|
2017-05-05 03:40:44 -04:00
|
|
|
end
|
|
|
|
|
2017-05-05 12:01:33 -04:00
|
|
|
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
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2017-05-05 03:40:44 -04:00
|
|
|
def service_hash
|
2017-05-05 08:43:22 -04:00
|
|
|
@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
|
|
|
|
|
2017-05-05 12:57:52 -04:00
|
|
|
service_hash[ActiveRecord::Base.connection.quote_column_name(key)] =
|
2019-01-15 16:05:36 -05:00
|
|
|
ActiveRecord::Base.connection.quote(value)
|
2017-05-05 08:43:22 -04:00
|
|
|
end
|
|
|
|
end
|
2017-05-04 06:13:33 -04:00
|
|
|
end
|
2017-05-05 10:16:02 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-05-05 10:16:02 -04: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 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-05-05 10:16:02 -04:00
|
|
|
|
|
|
|
def active_external_issue_tracker?
|
2017-05-05 12:57:52 -04:00
|
|
|
@template.issue_tracker? && !@template.default
|
2017-05-05 10:16:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def active_external_wiki?
|
2017-05-05 12:01:33 -04:00
|
|
|
@template.type == 'ExternalWikiService'
|
2017-05-05 10:16:02 -04:00
|
|
|
end
|
2017-05-04 06:13:33 -04:00
|
|
|
end
|
|
|
|
end
|