From 4ab64f76a27078ad00b3ad8b9e84dfd4d88b33b5 Mon Sep 17 00:00:00 2001 From: "J. Cliff Dyer" Date: Tue, 10 Oct 2017 21:43:07 -0400 Subject: [PATCH] Add BigAutoField for BlockCompletion primary key. --- lms/djangoapps/completion/apps.py | 1 - .../completion/migrations/0001_initial.py | 16 ++++++---- lms/djangoapps/completion/models.py | 8 +++++ openedx/core/djangolib/fields.py | 32 ++++++++++++++++++- 4 files changed, 49 insertions(+), 8 deletions(-) diff --git a/lms/djangoapps/completion/apps.py b/lms/djangoapps/completion/apps.py index c419c69c8f..69babe6b73 100644 --- a/lms/djangoapps/completion/apps.py +++ b/lms/djangoapps/completion/apps.py @@ -12,4 +12,3 @@ class CompletionAppConfig(AppConfig): """ name = 'lms.djangoapps.completion' verbose_name = 'Completion' - pass diff --git a/lms/djangoapps/completion/migrations/0001_initial.py b/lms/djangoapps/completion/migrations/0001_initial.py index 7683f80d3b..d150cfc5f0 100644 --- a/lms/djangoapps/completion/migrations/0001_initial.py +++ b/lms/djangoapps/completion/migrations/0001_initial.py @@ -2,12 +2,19 @@ from __future__ import unicode_literals from django.db import migrations, models -import lms.djangoapps.completion.models import django.utils.timezone from django.conf import settings import model_utils.fields + +import lms.djangoapps.completion.models import openedx.core.djangoapps.xmodule_django.models +# pylint: disable=ungrouped-imports +try: + from django.models import BigAutoField # New in django 1.10 +except ImportError: + from openedx.core.djangolib.fields import BigAutoField +# pylint: enable=ungrouped-imports class Migration(migrations.Migration): @@ -19,9 +26,9 @@ class Migration(migrations.Migration): migrations.CreateModel( name='BlockCompletion', fields=[ - ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), ('created', model_utils.fields.AutoCreatedField(default=django.utils.timezone.now, verbose_name='created', editable=False)), ('modified', model_utils.fields.AutoLastModifiedField(default=django.utils.timezone.now, verbose_name='modified', editable=False)), + ('id', BigAutoField(serialize=False, primary_key=True)), ('course_key', openedx.core.djangoapps.xmodule_django.models.CourseKeyField(max_length=255)), ('block_key', openedx.core.djangoapps.xmodule_django.models.UsageKeyField(max_length=255)), ('block_type', models.CharField(max_length=64)), @@ -35,9 +42,6 @@ class Migration(migrations.Migration): ), migrations.AlterIndexTogether( name='blockcompletion', - index_together=set([ - ('course_key', 'block_type', 'user'), - ('user', 'course_key', 'modified'), - ]), + index_together=set([('course_key', 'block_type', 'user'), ('user', 'course_key', 'modified')]), ), ] diff --git a/lms/djangoapps/completion/models.py b/lms/djangoapps/completion/models.py index a914879c46..9561f6fb6d 100644 --- a/lms/djangoapps/completion/models.py +++ b/lms/djangoapps/completion/models.py @@ -13,6 +13,13 @@ from opaque_keys.edx.keys import CourseKey from openedx.core.djangoapps.xmodule_django.models import CourseKeyField, UsageKeyField +# pylint: disable=ungrouped-imports +try: + from django.models import BigAutoField # New in django 1.10 +except ImportError: + from openedx.core.djangolib.fields import BigAutoField +# pylint: enable=ungrouped-imports + def validate_percent(value): """ @@ -106,6 +113,7 @@ class BlockCompletion(TimeStampedModel, models.Model): only track binary completion, where 1.0 indicates that the block is complete, and 0.0 indicates that the block is incomplete. """ + id = BigAutoField(primary_key=True) # pylint: disable=invalid-name user = models.ForeignKey(User) course_key = CourseKeyField(max_length=255) block_key = UsageKeyField(max_length=255) diff --git a/openedx/core/djangolib/fields.py b/openedx/core/djangolib/fields.py index f1087c55d5..a93a350661 100644 --- a/openedx/core/djangolib/fields.py +++ b/openedx/core/djangolib/fields.py @@ -5,7 +5,9 @@ from django.db import models class CharNullField(models.CharField): - """CharField that stores NULL but returns ''""" + """ + CharField that stores NULL but returns '' + """ description = "CharField that stores NULL but returns ''" @@ -26,3 +28,31 @@ class CharNullField(models.CharField): return None else: return value + + +class BigAutoField(models.AutoField): + """ + AutoField that uses BigIntegers. + + This exists in Django as of version 1.10. + """ + + def db_type(self, connection): + """ + The type of the field to insert into the database. + """ + conn_module = type(connection).__module__ + if "mysql" in conn_module: + return "bigint AUTO_INCREMENT" + elif "postgres" in conn_module: + return "bigserial" + else: + return super(BigAutoField, self).db_type(connection) + + def rel_db_type(self, connection): + """ + The type to be used by relations pointing to this field. + + Not used until Django 1.10. + """ + return "bigint"