-
Notifications
You must be signed in to change notification settings - Fork 475
Add unique constraints to ClockedSchedule and CrontabSchedule models #981
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| # Generated by Django 5.2.9 on 2025-12-09 09:48 | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("django_celery_beat", "0019_alter_periodictasks_options"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AlterField( | ||
| model_name="clockedschedule", | ||
| name="clocked_time", | ||
| field=models.DateTimeField( | ||
| help_text="Run the task at clocked time", | ||
| unique=True, | ||
| verbose_name="Clock Time", | ||
| ), | ||
| ), | ||
| migrations.AddConstraint( | ||
| model_name="crontabschedule", | ||
| constraint=models.UniqueConstraint( | ||
| fields=( | ||
| "minute", | ||
| "hour", | ||
| "day_of_month", | ||
| "month_of_year", | ||
| "day_of_week", | ||
| "timezone", | ||
| ), | ||
| name="unique_crontab", | ||
| ), | ||
| ), | ||
| migrations.AddConstraint( | ||
| model_name="intervalschedule", | ||
| constraint=models.UniqueConstraint( | ||
| fields=("every", "period"), name="unique_interval" | ||
| ), | ||
| ), | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -173,6 +173,9 @@ class Meta: | |
| verbose_name = _('interval') | ||
| verbose_name_plural = _('intervals') | ||
| ordering = ['period', 'every'] | ||
| constraints = [ | ||
| models.UniqueConstraint(fields=["every", "period"], name="unique_interval") | ||
| ] | ||
|
|
||
| @property | ||
| def schedule(self): | ||
|
|
@@ -214,8 +217,9 @@ class ClockedSchedule(models.Model): | |
| """clocked schedule.""" | ||
|
|
||
| clocked_time = models.DateTimeField( | ||
| verbose_name=_('Clock Time'), | ||
| help_text=_('Run the task at clocked time'), | ||
| verbose_name=_("Clock Time"), | ||
| help_text=_("Run the task at clocked time"), | ||
| unique=True, | ||
|
||
| ) | ||
|
|
||
| class Meta: | ||
|
|
@@ -314,6 +318,19 @@ class Meta: | |
| verbose_name_plural = _('crontabs') | ||
| ordering = ['month_of_year', 'day_of_month', | ||
| 'day_of_week', 'hour', 'minute', 'timezone'] | ||
| constraints = [ | ||
| models.UniqueConstraint( | ||
| fields=[ | ||
| "minute", | ||
| "hour", | ||
| "day_of_month", | ||
| "month_of_year", | ||
| "day_of_week", | ||
| "timezone", | ||
| ], | ||
| name="unique_crontab", | ||
| ) | ||
| ] | ||
|
Comment on lines
+321
to
+333
|
||
|
|
||
| @property | ||
| def human_readable(self): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The unique constraints added by this PR will prevent the creation of duplicate schedules, but the existing test
_test_duplicate_schedulesint/unit/test_models.pyexpects to be able to create duplicates (line 77 creates a second instance with the same kwargs, and line 78 expects 2 instances to exist). These tests will now fail with IntegrityError when trying to create the second duplicate instance. The tests need to be updated to either:from_scheduleproperly handles uniqueness withget_or_create