retention: Add .restored_timestamp to ArchiveTransaction.

This is just generally useful for tracking and debugging.
This commit is contained in:
Mateusz Mandera 2024-05-08 03:10:56 +02:00 committed by Tim Abbott
parent 74cdd6148e
commit 224ea3aaed
4 changed files with 25 additions and 1 deletions

View File

@ -587,6 +587,7 @@ def restore_data_from_archive(archive_transaction: ArchiveTransaction) -> int:
restore_attachment_messages_from_archive(archive_transaction.id) restore_attachment_messages_from_archive(archive_transaction.id)
archive_transaction.restored = True archive_transaction.restored = True
archive_transaction.restored_timestamp = timezone_now()
archive_transaction.save() archive_transaction.save()
logger.info("Finished. Restored %s messages", len(msg_ids)) logger.info("Finished. Restored %s messages", len(msg_ids))

View File

@ -0,0 +1,17 @@
# Generated by Django 5.0.6 on 2024-05-08 00:59
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("zerver", "0518_merge"),
]
operations = [
migrations.AddField(
model_name="archivetransaction",
name="restored_timestamp",
field=models.DateTimeField(db_index=True, null=True),
),
]

View File

@ -91,6 +91,7 @@ class ArchiveTransaction(models.Model):
timestamp = models.DateTimeField(default=timezone_now, db_index=True) timestamp = models.DateTimeField(default=timezone_now, db_index=True)
# Marks if the data archived in this transaction has been restored: # Marks if the data archived in this transaction has been restored:
restored = models.BooleanField(default=False, db_index=True) restored = models.BooleanField(default=False, db_index=True)
restored_timestamp = models.DateTimeField(null=True, db_index=True)
type = models.PositiveSmallIntegerField(db_index=True) type = models.PositiveSmallIntegerField(db_index=True)
# Valid types: # Valid types:

View File

@ -2,6 +2,7 @@ from datetime import datetime, timedelta
from typing import Any, Dict, List, Optional, Tuple from typing import Any, Dict, List, Optional, Tuple
from unittest import mock from unittest import mock
import time_machine
from django.conf import settings from django.conf import settings
from django.utils.timezone import now as timezone_now from django.utils.timezone import now as timezone_now
from typing_extensions import override from typing_extensions import override
@ -466,11 +467,15 @@ class TestArchiveMessagesGeneral(ArchiveMessagesTestingBase):
transactions = ArchiveTransaction.objects.all() transactions = ArchiveTransaction.objects.all()
self.assert_length(transactions, 2) # With chunk_size 4, there should be 2 transactions self.assert_length(transactions, 2) # With chunk_size 4, there should be 2 transactions
now = timezone_now()
with time_machine.travel(now, tick=False):
restore_all_data_from_archive() restore_all_data_from_archive()
transactions[0].refresh_from_db() transactions[0].refresh_from_db()
transactions[1].refresh_from_db() transactions[1].refresh_from_db()
self.assertTrue(transactions[0].restored) self.assertTrue(transactions[0].restored)
self.assertTrue(transactions[1].restored) self.assertTrue(transactions[1].restored)
self.assertEqual(transactions[0].restored_timestamp, now)
self.assertEqual(transactions[1].restored_timestamp, now)
archive_messages(chunk_size=10) archive_messages(chunk_size=10)
self._verify_archive_data(expired_msg_ids, expired_usermsg_ids) self._verify_archive_data(expired_msg_ids, expired_usermsg_ids)