PropagateServiceTemplate should not use loop

Summary

In https://gitlab.com/gitlab-org/gitlab-ce/issues/63595 we saw a disruptive bug that caused a huge number of duplicate records in services table in a loop that didn't break.

The issue was narrowed down to one of the SQL query being cached by ActiveRecord.

First issue is that in PropagateServiceTemplate we use a loop and this can be risky if we don't ensure we always exit (which is exactly what happened with the bug).

Second issue is that PropagateServiceTemplate#project_ids_batch uses a manually generated SQL statement which was being cached automatically by ActiveRecord causing it to always return the same results.

From the conversation here https://gitlab.com/gitlab-org/gitlab-ce/issues/63595#note_185519294 we should simplify the algorithm to use iteration instead of loops (e.g. using ActiveRecord#find_in_batches)

The end goal is to be able to propagate a service template without the need to temporarily disable the cache.

Steps to reproduce

Note that in order to reproduce the bug https://gitlab.com/gitlab-org/gitlab-ce/issues/63595 it's required to have more than 100 projects created.

Edited Oct 05, 2025 by 🤖 GitLab Bot 🤖
Assignee Loading
Time tracking Loading