2020-06-19 12:07:18 +02:00
|
|
|
from django.db import migrations
|
2022-05-27 23:33:51 +02:00
|
|
|
from django.db.backends.postgresql.schema import BaseDatabaseSchemaEditor
|
2020-06-19 12:07:18 +02:00
|
|
|
from django.db.migrations.state import StateApps
|
|
|
|
from django.db.models import Count
|
|
|
|
|
|
|
|
|
2022-05-27 23:33:51 +02:00
|
|
|
def clear_duplicate_reactions(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor) -> None:
|
2020-06-19 12:07:18 +02:00
|
|
|
"""Zulip's data model for reactions has enforced via code,
|
|
|
|
nontransactionally, that they can only react with one emoji_code
|
|
|
|
for a given reaction_type. This fixes any that were stored in the
|
|
|
|
database via a race; the next migration will add the appropriate
|
|
|
|
database-level unique constraint.
|
|
|
|
"""
|
2021-02-12 08:20:45 +01:00
|
|
|
Reaction = apps.get_model("zerver", "Reaction")
|
2020-06-19 12:07:18 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
duplicate_reactions = (
|
|
|
|
Reaction.objects.all()
|
|
|
|
.values("user_profile_id", "message_id", "reaction_type", "emoji_code")
|
2021-02-12 08:20:45 +01:00
|
|
|
.annotate(Count("id"))
|
2021-02-12 08:19:30 +01:00
|
|
|
.filter(id__count__gt=1)
|
|
|
|
)
|
2020-06-19 12:07:18 +02:00
|
|
|
for duplicate_reaction in duplicate_reactions:
|
2021-02-12 08:20:45 +01:00
|
|
|
duplicate_reaction.pop("id__count")
|
2020-06-19 12:07:18 +02:00
|
|
|
to_cleanup = Reaction.objects.filter(**duplicate_reaction)[1:]
|
|
|
|
for reaction in to_cleanup:
|
|
|
|
reaction.delete()
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-06-19 12:07:18 +02:00
|
|
|
class Migration(migrations.Migration):
|
|
|
|
dependencies = [
|
2021-02-12 08:20:45 +01:00
|
|
|
("zerver", "0286_merge_0260_0285"),
|
2020-06-19 12:07:18 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
operations = [
|
2021-02-12 08:19:30 +01:00
|
|
|
migrations.RunPython(
|
|
|
|
clear_duplicate_reactions, reverse_code=migrations.RunPython.noop, elidable=True
|
|
|
|
),
|
2020-06-19 12:07:18 +02:00
|
|
|
]
|