Skip to content

Commit 4eea347

Browse files
dilin-spmilind-shakya-sp
authored andcommitted
Set PeriodicTask.name max_length = 191 to avoid issues with MySQL indexes.
Add readme Add test settings
1 parent d96675d commit 4eea347

File tree

5 files changed

+62
-4
lines changed

5 files changed

+62
-4
lines changed

README.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,17 @@ pip command::
229229

230230
$ pip install https://github.com/celery/django-celery-beat/zipball/master#egg=django-celery-beat
231231

232+
Issues with mysql
233+
-----------------
234+
If you want to run ``django-celery-beat`` with MySQL, you might run into some issues.
235+
236+
One such issue is when you try to run ``python manage.py migrate django_celery_beat``, you might get the following error::
237+
django.db.utils.OperationalError: (1071, 'Specified key was too long; max key length is 767 bytes')
238+
To get around this issue, you can set::
239+
DJANGO_CELERY_BEAT_NAME_MAX_LENGTH=191
240+
(or any other value if any other db other than MySQL is causing similar issues.)
241+
max_length of **191** seems to work for MySQL.
242+
232243
.. |build-status| image:: https://secure.travis-ci.org/celery/django-celery-beat.svg?branch=master
233244
:alt: Build status
234245
:target: https://travis-ci.org/celery/django-celery-beat

django_celery_beat/migrations/0001_initial.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from django.db import migrations, models
66
import django.db.models.deletion
7+
from django.conf import settings
78

89

910
class Migration(migrations.Migration):
@@ -71,8 +72,15 @@ class Migration(migrations.Migration):
7172
auto_created=True, primary_key=True,
7273
serialize=False, verbose_name='ID')),
7374
('name', models.CharField(
74-
help_text='Useful description', max_length=200,
75-
unique=True, verbose_name='name')),
75+
help_text='Useful description',
76+
max_length=getattr(
77+
settings,
78+
'DJANGO_CELERY_BEAT_NAME_MAX_LENGTH',
79+
200
80+
),
81+
unique=True,
82+
verbose_name='name'
83+
)),
7684
('task', models.CharField(
7785
max_length=200, verbose_name='task name')),
7886
('args', models.TextField(

django_celery_beat/migrations/0002_auto_20161118_0346.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from django.db import migrations, models
66
import django.db.models.deletion
7+
import django_celery_beat.models
78

89

910
class Migration(migrations.Migration):
@@ -50,3 +51,14 @@ class Migration(migrations.Migration):
5051
to='django_celery_beat.SolarSchedule', verbose_name='solar'),
5152
),
5253
]
54+
custom_operations = [
55+
migrations.AddIndex(
56+
model_name='periodictask',
57+
index=django_celery_beat.models.CeleryMySQLIndex(
58+
fields=['name'],
59+
name='django_cele_name_9c39ec_idx'
60+
)
61+
)
62+
]
63+
64+
operations = operations + custom_operations

django_celery_beat/models.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
import timezone_field
77
from celery import schedules
88
from celery.five import python_2_unicode_compatible
9+
from django.conf import settings
910
from django.core.exceptions import MultipleObjectsReturned, ValidationError
1011
from django.core.validators import MaxValueValidator
1112
from django.db import models
1213
from django.db.models import signals
14+
from django.db.models.indexes import Index
1315
from django.utils.translation import ugettext_lazy as _
1416

1517
from . import managers, validators
@@ -39,6 +41,23 @@ def cronexp(field):
3941
return field and str(field).replace(' ', '') or '*'
4042

4143

44+
class CeleryMySQLIndex(Index):
45+
def create_sql(self, model, schema_editor, using=''):
46+
sql_create_index = 'CREATE INDEX %(name)s ON %(table)s (%(columns)s(%(size)d))%(extra)s'
47+
sql_parameters = self.get_sql_create_template_values(
48+
model,
49+
schema_editor,
50+
using
51+
)
52+
sql_parameters['size'] = getattr(
53+
settings,
54+
'DJANGO_CELERY_BEAT_NAME_MAX_LENGTH',
55+
200
56+
)
57+
sql = sql_create_index % sql_parameters
58+
return sql
59+
60+
4261
@python_2_unicode_compatible
4362
class SolarSchedule(models.Model):
4463
"""Schedule following astronomical patterns."""
@@ -248,8 +267,14 @@ class PeriodicTask(models.Model):
248267
"""Model representing a periodic task."""
249268

250269
name = models.CharField(
251-
_('name'), max_length=200, unique=True,
252-
help_text=_('Useful description'),
270+
_('name'),
271+
max_length=getattr(
272+
settings,
273+
'DJANGO_CELERY_BEAT_NAME_MAX_LENGTH',
274+
200
275+
),
276+
unique=True,
277+
help_text=_('Useful description')
253278
)
254279
task = models.CharField(_('task name'), max_length=200)
255280
interval = models.ForeignKey(
@@ -316,6 +341,7 @@ class Meta:
316341

317342
verbose_name = _('periodic task')
318343
verbose_name_plural = _('periodic tasks')
344+
indexes = [CeleryMySQLIndex(fields=['name'])]
319345

320346
def validate_unique(self, *args, **kwargs):
321347
super(PeriodicTask, self).validate_unique(*args, **kwargs)

t/proj/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,4 @@
122122
# https://docs.djangoproject.com/en/1.9/howto/static-files/
123123

124124
STATIC_URL = '/static/'
125+
DJANGO_CELERY_BEAT_NAME_MAX_LENGTH = 191

0 commit comments

Comments
 (0)