diff --git a/zerver/lib/retention.py b/zerver/lib/retention.py index 76cefc2511..a32cd751d6 100644 --- a/zerver/lib/retention.py +++ b/zerver/lib/retention.py @@ -587,6 +587,7 @@ def restore_data_from_archive(archive_transaction: ArchiveTransaction) -> int: restore_attachment_messages_from_archive(archive_transaction.id) archive_transaction.restored = True + archive_transaction.restored_timestamp = timezone_now() archive_transaction.save() logger.info("Finished. Restored %s messages", len(msg_ids)) diff --git a/zerver/migrations/0519_archivetransaction_restored_timestamp.py b/zerver/migrations/0519_archivetransaction_restored_timestamp.py new file mode 100644 index 0000000000..dee30d3edd --- /dev/null +++ b/zerver/migrations/0519_archivetransaction_restored_timestamp.py @@ -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), + ), + ] diff --git a/zerver/models/messages.py b/zerver/models/messages.py index 46547e77f2..45ff2b7d7f 100644 --- a/zerver/models/messages.py +++ b/zerver/models/messages.py @@ -91,6 +91,7 @@ class ArchiveTransaction(models.Model): timestamp = models.DateTimeField(default=timezone_now, db_index=True) # Marks if the data archived in this transaction has been restored: restored = models.BooleanField(default=False, db_index=True) + restored_timestamp = models.DateTimeField(null=True, db_index=True) type = models.PositiveSmallIntegerField(db_index=True) # Valid types: diff --git a/zerver/tests/test_retention.py b/zerver/tests/test_retention.py index 5e1536c61a..ee696dc4f2 100644 --- a/zerver/tests/test_retention.py +++ b/zerver/tests/test_retention.py @@ -2,6 +2,7 @@ from datetime import datetime, timedelta from typing import Any, Dict, List, Optional, Tuple from unittest import mock +import time_machine from django.conf import settings from django.utils.timezone import now as timezone_now from typing_extensions import override @@ -466,11 +467,15 @@ class TestArchiveMessagesGeneral(ArchiveMessagesTestingBase): transactions = ArchiveTransaction.objects.all() self.assert_length(transactions, 2) # With chunk_size 4, there should be 2 transactions - restore_all_data_from_archive() + now = timezone_now() + with time_machine.travel(now, tick=False): + restore_all_data_from_archive() transactions[0].refresh_from_db() transactions[1].refresh_from_db() self.assertTrue(transactions[0].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) self._verify_archive_data(expired_msg_ids, expired_usermsg_ids)