diff --git a/README.rst b/README.rst index e6f49b39..92d57afb 100644 --- a/README.rst +++ b/README.rst @@ -269,6 +269,17 @@ pip command:: $ pip install https://github.com/celery/django-celery-beat/zipball/master#egg=django-celery-beat +Issues with mysql +----------------- + If you want to run ``django-celery-beat`` with MySQL, you might run into some issues. + + One such issue is when you try to run ``python manage.py migrate django_celery_beat``, you might get the following error:: + django.db.utils.OperationalError: (1071, 'Specified key was too long; max key length is 767 bytes') + To get around this issue, you can set:: + DJANGO_CELERY_BEAT_NAME_MAX_LENGTH=191 + (or any other value if any other db other than MySQL is causing similar issues.) + max_length of **191** seems to work for MySQL. + TZ Awareness: ------------- diff --git a/django_celery_beat/migrations/0001_initial.py b/django_celery_beat/migrations/0001_initial.py index e882d4d9..efdf1737 100644 --- a/django_celery_beat/migrations/0001_initial.py +++ b/django_celery_beat/migrations/0001_initial.py @@ -4,6 +4,7 @@ from django.db import migrations, models import django.db.models.deletion +from django.conf import settings class Migration(migrations.Migration): @@ -71,8 +72,15 @@ class Migration(migrations.Migration): auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('name', models.CharField( - help_text='Useful description', max_length=200, - unique=True, verbose_name='name')), + help_text='Useful description', + max_length=getattr( + settings, + 'DJANGO_CELERY_BEAT_NAME_MAX_LENGTH', + 200 + ), + unique=True, + verbose_name='name' + )), ('task', models.CharField( max_length=200, verbose_name='task name')), ('args', models.TextField( diff --git a/django_celery_beat/models.py b/django_celery_beat/models.py index 66a6f23e..34ba9793 100644 --- a/django_celery_beat/models.py +++ b/django_celery_beat/models.py @@ -317,7 +317,12 @@ class PeriodicTask(models.Model): """Model representing a periodic task.""" name = models.CharField( - max_length=200, unique=True, + max_length=getattr( + settings, + 'DJANGO_CELERY_BEAT_NAME_MAX_LENGTH', + 200 + ), + unique=True, verbose_name=_('Name'), help_text=_('Short Description For This Task'), ) diff --git a/t/unit/test_models.py b/t/unit/test_models.py index c96b366a..0adc5350 100644 --- a/t/unit/test_models.py +++ b/t/unit/test_models.py @@ -1,14 +1,52 @@ from __future__ import absolute_import, unicode_literals +import importlib import os -from django.test import TestCase from django.apps import apps -from django.db.migrations.state import ProjectState from django.db.migrations.autodetector import MigrationAutodetector from django.db.migrations.loader import MigrationLoader from django.db.migrations.questioner import NonInteractiveMigrationQuestioner +from django.db.migrations.state import ProjectState +from django.test import TestCase, override_settings +from django.utils import six + +from django_celery_beat import migrations as beat_migrations, models + + +class ModelMigrationTests(TestCase): + def test_periodic_task_name_max_length_defaults_to_200_in_model(self): + six.moves.reload_module(models) + self.assertEqual( + 200, models.PeriodicTask._meta.get_field('name').max_length) + + @override_settings(DJANGO_CELERY_BEAT_NAME_MAX_LENGTH=191) + def test_periodic_task_name_max_length_changed_to_191_in_model(self): + six.moves.reload_module(models) + self.assertEqual( + 191, models.PeriodicTask._meta.get_field('name').max_length) + + def test_periodic_task_name_max_length_defaults_to_200_in_migration(self): + initial_migration_module = importlib.import_module( + 'django_celery_beat.migrations.0001_initial') + six.moves.reload_module(initial_migration_module) + initial_migration = initial_migration_module.Migration + periodic_task_creation = initial_migration.operations[2] + fields = dict(periodic_task_creation.fields) + + self.assertEqual('PeriodicTask', periodic_task_creation.name) + self.assertEqual(200, fields['name'].max_length) + + @override_settings(DJANGO_CELERY_BEAT_NAME_MAX_LENGTH=191) + def test_periodic_task_name_max_length_changed_to_191_in_migration(self): + initial_migration_module = importlib.import_module( + 'django_celery_beat.migrations.0001_initial') + six.moves.reload_module(initial_migration_module) + initial_migration = initial_migration_module.Migration + periodic_task_creation = initial_migration.operations[2] + fields = dict(periodic_task_creation.fields) -from django_celery_beat import migrations as beat_migrations + self.assertEqual('PeriodicTask', periodic_task_creation.name) + self.assertEqual(191, fields['name'].max_length) class MigrationTests(TestCase): diff --git a/tox.ini b/tox.ini index 9150580c..013cb48e 100644 --- a/tox.ini +++ b/tox.ini @@ -41,7 +41,8 @@ sitepackages = False recreate = False commands = pip list - py.test -xv + py.test -xv --ignore=t/unit/test_models.py + py.test -xv -k 'ModelMigrationTests' [testenv:upgradebeat111] basepython = python2.7 @@ -170,4 +171,4 @@ commands = pip install -U https://github.com/celery/celery/zipball/master#egg=celery pip install -U https://github.com/celery/kombu/zipball/master#egg=kombu pip install Django==2.0 - py.test -x --cov=django_celery_beat --cov-report=xml --no-cov-on-fail + py.test -x --cov=django_celery_beat --cov-report=xml --no-cov-on-fail --ignore=t/unit/test_models.py