diff --git a/zerver/migrations/0180_usermessage_add_active_mobile_push_notification.py b/zerver/migrations/0180_usermessage_add_active_mobile_push_notification.py index 7998f926e0..631683503a 100644 --- a/zerver/migrations/0180_usermessage_add_active_mobile_push_notification.py +++ b/zerver/migrations/0180_usermessage_add_active_mobile_push_notification.py @@ -1,10 +1,12 @@ # Generated by Django 1.11.14 on 2018-08-01 23:05 import bitfield.models -from django.db import migrations +from django.db import migrations, models +from django.db.models import Q class Migration(migrations.Migration): + atomic = False dependencies = [ ("zerver", "0179_rename_to_digest_emails_enabled"), @@ -55,12 +57,13 @@ class Migration(migrations.Migration): default=0, ), ), - migrations.RunSQL( - """ - CREATE INDEX zerver_usermessage_active_mobile_push_notification_id - ON zerver_usermessage (user_profile_id, message_id) - WHERE (flags & 4096) != 0; - """, - reverse_sql="DROP INDEX zerver_usermessage_active_mobile_push_notification_id;", + migrations.AddIndex( + model_name="usermessage", + index=models.Index( + "user_profile", + "message", + condition=Q(flags__andnz=4096), + name="zerver_usermessage_active_mobile_push_notification_id", + ), ), ] diff --git a/zerver/models.py b/zerver/models.py index 626f2d2831..a45f835f18 100644 --- a/zerver/models.py +++ b/zerver/models.py @@ -3125,6 +3125,18 @@ class AbstractUserMessage(models.Model): class UserMessage(AbstractUserMessage): message: Message = models.ForeignKey(Message, on_delete=CASCADE) + class Meta(AbstractUserMessage.Meta): + indexes = [ + models.Index( + "user_profile", + "message", + condition=Q( + flags__andnz=AbstractUserMessage.flags.active_mobile_push_notification.mask + ), + name="zerver_usermessage_active_mobile_push_notification_id", + ), + ] + def get_usermessage_by_message_id( user_profile: UserProfile, message_id: int