From b8095c95b315021efcbb02b0039a42124e766f39 Mon Sep 17 00:00:00 2001 From: Dan Ilin Date: Thu, 24 Aug 2017 14:49:34 -0400 Subject: [PATCH 1/4] Set PeriodicTask.name max_length = 191 to avoid issues with MySQL indexes. Add readme Add test settings Remove index related changes --- README.rst | 11 +++++++++++ django_celery_beat/migrations/0001_initial.py | 12 ++++++++++-- django_celery_beat/models.py | 7 ++++++- t/proj/settings.py | 1 + 4 files changed, 28 insertions(+), 3 deletions(-) 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/proj/settings.py b/t/proj/settings.py index b778ce92..c41a340a 100644 --- a/t/proj/settings.py +++ b/t/proj/settings.py @@ -123,4 +123,5 @@ # https://docs.djangoproject.com/en/1.9/howto/static-files/ STATIC_URL = '/static/' +DJANGO_CELERY_BEAT_NAME_MAX_LENGTH = 191 DJANGO_CELERY_BEAT_TZ_AWARE = True From d482429e011e48ac10955ba54d0c90eafd671ca7 Mon Sep 17 00:00:00 2001 From: Tobias Kroenke Date: Mon, 11 Feb 2019 11:33:38 +0100 Subject: [PATCH 2/4] test that PeriodicTask.name.max_length defaults to 200 --- t/proj/settings.py | 1 - t/unit/test_models.py | 44 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/t/proj/settings.py b/t/proj/settings.py index c41a340a..b778ce92 100644 --- a/t/proj/settings.py +++ b/t/proj/settings.py @@ -123,5 +123,4 @@ # https://docs.djangoproject.com/en/1.9/howto/static-files/ STATIC_URL = '/static/' -DJANGO_CELERY_BEAT_NAME_MAX_LENGTH = 191 DJANGO_CELERY_BEAT_TZ_AWARE = True 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): From 1dc843b468f43c8c1fff9b08fdebaeaf6ad071f1 Mon Sep 17 00:00:00 2001 From: Tobias Kroenke Date: Tue, 12 Feb 2019 12:19:49 +0100 Subject: [PATCH 3/4] test models with DJANGO_CELERY_BEAT_NAME_MAX_LENGTH=191 in own process; pep8 --- tox.ini | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 9150580c..23ebb083 100644 --- a/tox.ini +++ b/tox.ini @@ -41,7 +41,11 @@ sitepackages = False recreate = False commands = pip list - py.test -xv + py.test -xv --ignore=t/unit/test_models.py + +[testenv:models] +commands = + py.test -xv -k 'ModelMigrationTests' [testenv:upgradebeat111] basepython = python2.7 @@ -169,5 +173,9 @@ usedevelop = true 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 +<<<<<<< HEAD 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 +>>>>>>> test models with DJANGO_CELERY_BEAT_NAME_MAX_LENGTH=191 in own process; pep8 From 46dac8ed29dd8b34a1a7c4875eff8a31dac420f3 Mon Sep 17 00:00:00 2001 From: Tobias Kroenke Date: Tue, 12 Feb 2019 12:37:48 +0100 Subject: [PATCH 4/4] fix tox.ini --- tox.ini | 7 ------- 1 file changed, 7 deletions(-) diff --git a/tox.ini b/tox.ini index 23ebb083..013cb48e 100644 --- a/tox.ini +++ b/tox.ini @@ -42,9 +42,6 @@ recreate = False commands = pip list py.test -xv --ignore=t/unit/test_models.py - -[testenv:models] -commands = py.test -xv -k 'ModelMigrationTests' [testenv:upgradebeat111] @@ -173,9 +170,5 @@ usedevelop = true 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 -<<<<<<< HEAD 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 ->>>>>>> test models with DJANGO_CELERY_BEAT_NAME_MAX_LENGTH=191 in own process; pep8