Skip to content

Optimize Routable.where_full_path_in by avoiding joins

module Routable
  extend ActiveSupport::Concern

  class_methods do
   def where_full_path_in(paths, use_includes: true)
      return none if paths.empty?

      wheres = paths.map do |path|
        "(LOWER(routes.path) = LOWER(#{connection.quote(path)}))"
      end

      route =
        if use_includes
          includes(:route).references(:routes)
        else
          joins(:route)
        end

      route
        .where(wheres.join(' OR '))
        .allow_cross_joins_across_databases(url: "https://gitlab.com/gitlab-org/gitlab/-/issues/420046")
    end

  end
end

This method involved joining projects/namespaces with routes table, which should be avoided as we are building out Cells.