2017-11-16 00:44:00 +01:00
|
|
|
from django.db import migrations, models
|
2022-05-27 23:33:51 +02:00
|
|
|
from django.db.backends.postgresql.schema import BaseDatabaseSchemaEditor
|
2017-07-12 17:28:27 +02:00
|
|
|
from django.db.migrations.state import StateApps
|
|
|
|
from django.db.models import Max
|
|
|
|
from django.utils.timezone import now as timezone_now
|
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2022-05-27 23:33:51 +02:00
|
|
|
def backfill_subscription_log_events(
|
|
|
|
apps: StateApps, schema_editor: BaseDatabaseSchemaEditor
|
|
|
|
) -> None:
|
2017-07-12 17:28:27 +02:00
|
|
|
migration_time = timezone_now()
|
2021-02-12 08:20:45 +01:00
|
|
|
RealmAuditLog = apps.get_model("zerver", "RealmAuditLog")
|
|
|
|
Subscription = apps.get_model("zerver", "Subscription")
|
|
|
|
Message = apps.get_model("zerver", "Message")
|
2017-07-18 21:56:23 +02:00
|
|
|
objects_to_create = []
|
2017-07-12 17:28:27 +02:00
|
|
|
|
2017-07-18 20:31:02 +02:00
|
|
|
subs_query = Subscription.objects.select_related(
|
2021-02-12 08:19:30 +01:00
|
|
|
"user_profile", "user_profile__realm", "recipient"
|
|
|
|
).filter(recipient__type=2)
|
2017-07-18 20:31:02 +02:00
|
|
|
for sub in subs_query:
|
2017-07-18 20:36:18 +02:00
|
|
|
entry = RealmAuditLog(
|
|
|
|
realm=sub.user_profile.realm,
|
|
|
|
modified_user=sub.user_profile,
|
|
|
|
modified_stream_id=sub.recipient.type_id,
|
|
|
|
event_last_message_id=0,
|
2021-02-12 08:20:45 +01:00
|
|
|
event_type="subscription_created",
|
2017-07-18 20:36:18 +02:00
|
|
|
event_time=migration_time,
|
2021-02-12 08:19:30 +01:00
|
|
|
backfilled=True,
|
|
|
|
)
|
2017-07-18 20:36:18 +02:00
|
|
|
objects_to_create.append(entry)
|
|
|
|
RealmAuditLog.objects.bulk_create(objects_to_create)
|
|
|
|
objects_to_create = []
|
2017-07-12 17:28:27 +02:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
event_last_message_id = Message.objects.aggregate(Max("id"))["id__max"]
|
2017-07-12 17:28:27 +02:00
|
|
|
migration_time_for_deactivation = timezone_now()
|
2017-07-18 20:31:02 +02:00
|
|
|
for sub in subs_query.filter(active=False):
|
2017-07-18 20:36:18 +02:00
|
|
|
entry = RealmAuditLog(
|
|
|
|
realm=sub.user_profile.realm,
|
|
|
|
modified_user=sub.user_profile,
|
|
|
|
modified_stream_id=sub.recipient.type_id,
|
|
|
|
event_last_message_id=event_last_message_id,
|
2021-02-12 08:20:45 +01:00
|
|
|
event_type="subscription_deactivated",
|
2017-07-18 20:36:18 +02:00
|
|
|
event_time=migration_time_for_deactivation,
|
2021-02-12 08:19:30 +01:00
|
|
|
backfilled=True,
|
|
|
|
)
|
2017-07-18 20:36:18 +02:00
|
|
|
objects_to_create.append(entry)
|
|
|
|
RealmAuditLog.objects.bulk_create(objects_to_create)
|
|
|
|
objects_to_create = []
|
2017-07-12 17:28:27 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2022-05-27 23:33:51 +02:00
|
|
|
def reverse_code(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
RealmAuditLog = apps.get_model("zerver", "RealmAuditLog")
|
|
|
|
RealmAuditLog.objects.filter(event_type="subscription_created").delete()
|
|
|
|
RealmAuditLog.objects.filter(event_type="subscription_deactivated").delete()
|
2017-07-12 17:28:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
dependencies = [
|
2021-02-12 08:20:45 +01:00
|
|
|
("zerver", "0092_create_scheduledemail"),
|
2017-07-12 17:28:27 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
operations = [
|
|
|
|
migrations.AddField(
|
2021-02-12 08:20:45 +01:00
|
|
|
model_name="realmauditlog",
|
|
|
|
name="event_last_message_id",
|
2017-07-12 17:28:27 +02:00
|
|
|
field=models.IntegerField(null=True),
|
|
|
|
),
|
2021-02-12 08:19:30 +01:00
|
|
|
migrations.RunPython(
|
|
|
|
backfill_subscription_log_events, reverse_code=reverse_code, elidable=True
|
|
|
|
),
|
2017-07-12 17:28:27 +02:00
|
|
|
]
|