Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add delete historical management command #579

Merged
merged 2 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions eventstore/management/commands/delete_historical_records.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import logging

from dateutil.relativedelta import relativedelta
from django.apps import apps
from django.core.management.base import BaseCommand
from django.utils import timezone

logger = logging.getLogger(__name__)

VALID_MODELS = {"Event", "Message"}


class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument(
"model",
type=str,
help="Specify the model you want to delete the records for.(Event/Message)",
)
parser.add_argument(
"retention_period",
type=int,
help="Specify the retention period in months",
default=60,
)

def handle(self, *args, **options):
model_name = options["model"]
retention_period = options["retention_period"]

if model_name not in VALID_MODELS:
raise Exception("Invalid model specified")

model = apps.get_model("eventstore", model_name)

filter_date = timezone.now() - relativedelta(months=retention_period, hour=0)
count, _ = model.objects.filter(timestamp__lt=filter_date).delete()
logger.info(f"Deleted {count} {model_name.lower()}(s)")
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from io import StringIO

from dateutil.relativedelta import relativedelta
from django.core.management import call_command
from django.core.management.base import CommandError
from django.test import TestCase
from django.utils import timezone

from eventstore.models import Event, Message


class DeleteHistoricalRecordsTests(TestCase):
def call_command(self, *args, **kwargs):
out = StringIO()
call_command(
"delete_historical_records",
*args,
stdout=out,
stderr=StringIO(),
**kwargs,
)
return out.getvalue()

def create_record(self, model, id, timestamp):
record = model.objects.create(id=id)
record.timestamp = timestamp
record.save()

def test_missing_arguments(self):
self.assertRaises(CommandError, self.call_command)

def test_invalid_arguments(self):
self.assertRaises(Exception, self.call_command, "InvalidModel")

def test_delete_events(self):
running_month = timezone.now() - relativedelta(months=12, hour=12)

for i in range(12):
self.create_record(Event, i, running_month)
running_month = running_month + relativedelta(months=1)

self.call_command("Event", 6)

self.assertEqual(Event.objects.count(), 6)

def test_delete_messages(self):
running_month = timezone.now() - relativedelta(months=12, hour=12)

for i in range(12):
self.create_record(Message, i, running_month)
running_month = running_month + relativedelta(months=1)

self.call_command("Message", 6)

self.assertEqual(Message.objects.count(), 6)
Loading