From 23c8a98157691d230866e0fa1d82c25d66634bf4 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Tue, 22 Feb 2022 22:15:28 -0800 Subject: [PATCH] models: Manage index from migration 0180 with Django. Signed-off-by: Anders Kaseorg --- ...age_add_active_mobile_push_notification.py | 19 +++++++++++-------- zerver/models.py | 12 ++++++++++++ 2 files changed, 23 insertions(+), 8 deletions(-) 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