From 079289c53c8b495a43177011e9db286c1bb9c74c Mon Sep 17 00:00:00 2001
From: Joseph Schorr <joseph.schorr@coreos.com>
Date: Wed, 12 Nov 2014 14:51:24 -0500
Subject: [PATCH] HACK: Don't join on the visibility table because it is
 horrendously slow for some odd reason.

---
 data/model/legacy.py | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/data/model/legacy.py b/data/model/legacy.py
index 89fdff74a..9050830ef 100644
--- a/data/model/legacy.py
+++ b/data/model/legacy.py
@@ -823,8 +823,10 @@ def _filter_to_repos_for_user(query, username=None, namespace=None,
     if namespace:
       where_clause = where_clause & (Namespace.username == namespace)
 
+  # TODO(jschorr, jake): Figure out why the old join on Visibility was so darn slow and
+  # remove this hack.
   if include_public:
-    new_clause = (Visibility.name == 'public')
+    new_clause = (Repository.visibility == _get_public_repo_visibility())
     if where_clause:
       where_clause = where_clause | new_clause
     else:
@@ -833,6 +835,16 @@ def _filter_to_repos_for_user(query, username=None, namespace=None,
   return query.where(where_clause)
 
 
+_public_repo_visibility_cache = None
+def _get_public_repo_visibility():
+  global _public_repo_visibility_cache
+
+  if not _public_repo_visibility_cache:
+    _public_repo_visibility_cache = Visibility.get(name='public')
+
+  return _public_repo_visibility_cache
+
+
 def get_matching_repositories(repo_term, username=None):
   namespace_term = repo_term
   name_term = repo_term
@@ -1224,8 +1236,6 @@ def find_create_or_link_image(docker_image_id, repository, username, translation
                            .join(ImageStorage)
                            .switch(Image)
                            .join(Repository)
-                           .join(Visibility)
-                           .switch(Repository)
                            .join(RepositoryPermission, JOIN_LEFT_OUTER)
                            .switch(Repository)
                            .join(Namespace, on=(Repository.namespace_user == Namespace.id))