From 14ac75d10d69f10944946997a51742ffab0bdb1b Mon Sep 17 00:00:00 2001
From: Jessica Tallon <jessica@megworld.co.uk>
Date: Thu, 17 Jul 2014 14:58:24 +0100
Subject: [PATCH] Fix #894 - index User.username field

---
 mediagoblin/db/migrations.py | 13 +++++++++++++
 mediagoblin/db/models.py     |  2 +-
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/mediagoblin/db/migrations.py b/mediagoblin/db/migrations.py
index 8e0b509..e580e08 100644
--- a/mediagoblin/db/migrations.py
+++ b/mediagoblin/db/migrations.py
@@ -789,3 +789,16 @@ def fix_privilege_user_association_table(db):
         privilege_user_assoc.c.core__privilege_id.alter(name="user")
 
     db.commit()
+
+@RegisterMigration(22, MIGRATIONS)
+def add_index_username_field(db):
+    """
+    This indexes the User.username field which is frequently queried
+    for example a user logging in. This solves the issue #894
+    """
+    metadata = MetaData(bind=db.bind)
+    user_table = inspect_table(metadata, "core__users")
+
+    user_table.c.username.alter(index=True)
+
+    db.commit()
\ No newline at end of file
diff --git a/mediagoblin/db/models.py b/mediagoblin/db/models.py
index e388bd5..643d5d4 100644
--- a/mediagoblin/db/models.py
+++ b/mediagoblin/db/models.py
@@ -57,7 +57,7 @@ class User(Base, UserMixin):
     __tablename__ = "core__users"
 
     id = Column(Integer, primary_key=True)
-    username = Column(Unicode, nullable=False, unique=True)
+    username = Column(Unicode, nullable=False, unique=True, index=True)
     # Note: no db uniqueness constraint on email because it's not
     # reliable (many email systems case insensitive despite against
     # the RFC) and because it would be a mess to implement at this
-- 
1.8.3.1

