Add data migration for adding a default site

The Django allauth plugin requires a site instance with an ID matching
the SITE_ID setting value to be present in the database.

Issue: HIC-289
Change-Id: Ieed16cc5b245b776d782bc48ee91c2d53e5444b8
diff --git a/crashreport_stats/migrations/0006_add_default_site.py b/crashreport_stats/migrations/0006_add_default_site.py
new file mode 100644
index 0000000..66d9fe0
--- /dev/null
+++ b/crashreport_stats/migrations/0006_add_default_site.py
@@ -0,0 +1,37 @@
+# -*- coding: utf-8 -*-
+"""Migrations to add a default site which is required for the allauth plugin."""
+# pylint: disable=invalid-name
+import logging
+
+from django.conf import settings
+from django.contrib.sites.models import Site
+from django.db import migrations, IntegrityError
+
+LOGGER = logging.getLogger(__name__)
+
+
+def create_default_site(apps, schema_editor):
+    """Create a default site instance if it does not exist."""
+    # pylint: disable=unused-argument
+
+    if not Site.objects.filter(id=settings.SITE_ID).exists():
+        try:
+            Site.objects.create(id=settings.SITE_ID)
+        except IntegrityError as e:
+            LOGGER.error(
+                "Failed to create a site with id %d. Either adapt the SITE_ID "
+                "setting or remove the currently existing site.",
+                settings.SITE_ID,
+            )
+            raise e
+
+
+class Migration(migrations.Migration):
+    """Run the migration script."""
+
+    dependencies = [
+        ("sites", "0002_alter_domain_unique"),
+        ("crashreport_stats", "0005_remove_manual_default_value"),
+    ]
+
+    operations = [migrations.RunPython(create_default_site)]