Commit 833f82dc by Ned Batchelder Committed by GitHub

Merge pull request #14519 from edx/nedbat/idempotent-email-uniqueness-constraint

An idempotent migration to add an email uniqueness constraint
parents 19d6e21a 98b250b6
...@@ -959,6 +959,9 @@ INSTALLED_APPS = ( ...@@ -959,6 +959,9 @@ INSTALLED_APPS = (
# management of user-triggered async tasks (course import/export, etc.) # management of user-triggered async tasks (course import/export, etc.)
'user_tasks', 'user_tasks',
# Unusual migrations
'database_fixups',
) )
......
"""
This app exists solely to host unusual database migrations.
"""
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
# We used to have a uniqueness constraint on auth_user.email:
# https://github.com/edx/edx-platform/commit/c52727b0e0fb241d8211900975d3b69fe5a1bd57
#
# That constraint was lost in the upgrade from Django 1.4->1.8. This migration
# adds it back. But because it might already exist in databases created
# long-enough ago, we have to do it idempotently. So we check for the
# existence of the constraint before creating it.
def add_email_uniqueness_constraint(apps, schema_editor):
# Do we already have an email uniqueness constraint?
cursor = schema_editor.connection.cursor()
constraints = schema_editor.connection.introspection.get_constraints(cursor, "auth_user")
email_constraint = constraints.get("email", {})
if email_constraint.get("columns") == ["email"] and email_constraint.get("unique") == True:
# We already have the constraint, we're done.
return
# We don't have the constraint, make it.
schema_editor.execute("create unique index email on auth_user (email)")
class Migration(migrations.Migration):
dependencies = [
]
operations = [
migrations.RunPython(add_email_uniqueness_constraint)
]
...@@ -26,9 +26,5 @@ class Migration(migrations.Migration): ...@@ -26,9 +26,5 @@ class Migration(migrations.Migration):
# ) # )
operations = [ operations = [
migrations.RunSQL( # Nothing to do.
# Do nothing:
"select 1",
"select 1"
)
] ]
...@@ -2164,6 +2164,9 @@ INSTALLED_APPS = ( ...@@ -2164,6 +2164,9 @@ INSTALLED_APPS = (
# Ability to detect and special-case crawler behavior # Ability to detect and special-case crawler behavior
'openedx.core.djangoapps.crawlers', 'openedx.core.djangoapps.crawlers',
# Unusual migrations
'database_fixups',
) )
# Migrations which are not in the standard module "migrations" # Migrations which are not in the standard module "migrations"
......
...@@ -51,7 +51,7 @@ class StudentModuleHistoryExtendedRouter(object): ...@@ -51,7 +51,7 @@ class StudentModuleHistoryExtendedRouter(object):
""" """
if model_name is not None: if model_name is not None:
model = hints.get('model') model = hints.get('model')
if self._is_csmh(model): if model is not None and self._is_csmh(model):
return db == self.DATABASE_NAME return db == self.DATABASE_NAME
if db == self.DATABASE_NAME: if db == self.DATABASE_NAME:
return False return False
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment