Skip to content

Commit b9487f5

Browse files
Add custom index
Add migration for custom index.
1 parent e1149ed commit b9487f5

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from django.core.validators import MaxValueValidator
1212
from django.db import models
1313
from django.db.models import signals
14+
from django.db.models.indexes import Index
1415
from django.utils.translation import ugettext_lazy as _
1516

1617
from . import managers, validators
@@ -40,6 +41,23 @@ def cronexp(field):
4041
return field and str(field).replace(' ', '') or '*'
4142

4243

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+
4361
@python_2_unicode_compatible
4462
class SolarSchedule(models.Model):
4563
"""Schedule following astronomical patterns."""
@@ -323,6 +341,7 @@ class Meta:
323341

324342
verbose_name = _('periodic task')
325343
verbose_name_plural = _('periodic tasks')
344+
indexes = [CeleryMySQLIndex(fields=['name'])]
326345

327346
def validate_unique(self, *args, **kwargs):
328347
super(PeriodicTask, self).validate_unique(*args, **kwargs)

0 commit comments

Comments
 (0)