4

After setting up history for a model using django-simple-history, I wanted to run populate_history to populate the history table based on the existing contents of the table. However, other users have already made a number of changes, causing the history table to be partially populated. Running populate_history --auto simply results in message Existing history found, skipping model.

I wish to retain the existing history, but populate history for all records not currently stored in the history. Is there a way to do this?

beldaz
  • 4,299
  • 3
  • 43
  • 63

2 Answers2

3

I ended up writing a modified script based on populate_history. It identified all objects with no historical records, and added them in to the history table. A simple version (no batching) is below.

from django.apps import apps

from simple_history.utils import get_history_manager_for_model, get_history_model_for_model

def populate_model_history(model):
  history = get_history_model_for_model(model)
  history_manager = get_history_manager_for_model(model)

  # Insert historical records for objects without existing history
  # NOTE: A better approach would be to do this in batches, as in populate_history.py
  for instance in model.objects.exclude(pk__in=history.objects.values_list(model._meta.pk.name)):
    history_manager.bulk_history_create([instance], batch_size=1)

model = apps.get_model('app', 'my_model')
populate_model_history(model)
beldaz
  • 4,299
  • 3
  • 43
  • 63
1

Inspired by @beldaz and the tests in simple-history, I simply call populate_history as a migration operation

from io import StringIO
from django.conf import settings
from django.core import management
from django.db import migrations, models
import django.db.models.deletion
import simple_history.models
import uuid


def forwards(apps, _):
    # backfill history
    management.call_command(
        "populate_history", "invoicing.Payment", stdout=StringIO(), stderr=StringIO()
    )


class Migration(migrations.Migration):
    dependencies = [
        # <autofilled by simple history>
    ]

    operations = [
        # <autofilled by simple history>,
        migrations.RunPython(forwards)
    ]
theannouncer
  • 1,148
  • 16
  • 28