Skip to content

`Project#create_labels` runs `Label.templates` which is very slow

This code does the following:

def create_labels
  Label.templates.each do |label|
    params = label.attributes.except('id', 'template', 'created_at', 'updated_at')
    Labels::FindOrCreateService.new(nil, self, params).execute(skip_authorization: true)
  end
end

There are a few problems here:

  • labels.template defaults to false, but allows NULL values (this should not be allowed)
  • labels.template is never set to true for GitLab.com, wasting time querying this data
  • labels.template should have a partial index with clause WHERE (template IS TRUE) for PostgreSQL (a regular index is less space efficient)

Since for GitLab.com we probably never use this I also propose adding a method called Label.has_templates? which returns true or false based on the template count, caching the result in Redis. We then modify Project#create_labels to skip its work if no templates exist.