models: Manage index from migration 0180 with Django.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2022-02-22 22:15:28 -08:00 committed by Tim Abbott
parent cf0eb46afc
commit 23c8a98157
2 changed files with 23 additions and 8 deletions

View File

@ -1,10 +1,12 @@
# Generated by Django 1.11.14 on 2018-08-01 23:05 # Generated by Django 1.11.14 on 2018-08-01 23:05
import bitfield.models import bitfield.models
from django.db import migrations from django.db import migrations, models
from django.db.models import Q
class Migration(migrations.Migration): class Migration(migrations.Migration):
atomic = False
dependencies = [ dependencies = [
("zerver", "0179_rename_to_digest_emails_enabled"), ("zerver", "0179_rename_to_digest_emails_enabled"),
@ -55,12 +57,13 @@ class Migration(migrations.Migration):
default=0, default=0,
), ),
), ),
migrations.RunSQL( migrations.AddIndex(
""" model_name="usermessage",
CREATE INDEX zerver_usermessage_active_mobile_push_notification_id index=models.Index(
ON zerver_usermessage (user_profile_id, message_id) "user_profile",
WHERE (flags & 4096) != 0; "message",
""", condition=Q(flags__andnz=4096),
reverse_sql="DROP INDEX zerver_usermessage_active_mobile_push_notification_id;", name="zerver_usermessage_active_mobile_push_notification_id",
),
), ),
] ]

View File

@ -3125,6 +3125,18 @@ class AbstractUserMessage(models.Model):
class UserMessage(AbstractUserMessage): class UserMessage(AbstractUserMessage):
message: Message = models.ForeignKey(Message, on_delete=CASCADE) 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( def get_usermessage_by_message_id(
user_profile: UserProfile, message_id: int user_profile: UserProfile, message_id: int