Skip to content

Instantly share code, notes, and snippets.

@kingbuzzman
Created May 22, 2011 02:46
Show Gist options
  • Select an option

  • Save kingbuzzman/985127 to your computer and use it in GitHub Desktop.

Select an option

Save kingbuzzman/985127 to your computer and use it in GitHub Desktop.
__init__.py
from django.db.models.signals import post_syncdb
from django.contrib.auth import models as auth_models
def fix_db(sender, **kwargs):
from django.db import connection, transaction
cursor = connection.cursor()
# Data modifying operation - commit required
print '* Better the auth_user table'
cursor.execute("ALTER TABLE auth_user ALTER COLUMN username TYPE VARCHAR(255);")
cursor.execute("ALTER TABLE auth_user ALTER COLUMN email TYPE VARCHAR(255);")
# delete the indexes if they exists
cursor.execute("DROP INDEX IF EXISTS auth_user_username_idx;")
cursor.execute("DROP INDEX IF EXISTS auth_user_username_upper_idx;")
cursor.execute("DROP INDEX IF EXISTS auth_user_email_idx;")
cursor.execute("DROP INDEX IF EXISTS auth_user_email_upper_idx;")
# delete the constrain if it exists
cursor.execute("ALTER TABLE auth_user DROP CONSTRAINT IF EXISTS auth_user_email_key;")
# create constraint
cursor.execute("ALTER TABLE auth_user ADD CONSTRAINT auth_user_email_key UNIQUE (email);")
# create indexes
cursor.execute("CREATE INDEX auth_user_username_idx ON auth_user (username);")
cursor.execute("CREATE INDEX auth_user_username_upper_idx ON auth_user ((upper(username)));")
cursor.execute("CREATE INDEX auth_user_email_idx ON auth_user (email);")
cursor.execute("CREATE INDEX auth_user_email_upper_idx ON auth_user ((upper(email)));")
transaction.commit_unless_managed()
post_syncdb.connect(fix_db, sender=auth_models)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment