blob: 66d9fe0ae90da469b98bd7ba3d178514f93df014 [file] [log] [blame]
Mitja Nikolause7af9562018-12-10 16:31:46 +01001# -*- coding: utf-8 -*-
2"""Migrations to add a default site which is required for the allauth plugin."""
3# pylint: disable=invalid-name
4import logging
5
6from django.conf import settings
7from django.contrib.sites.models import Site
8from django.db import migrations, IntegrityError
9
10LOGGER = logging.getLogger(__name__)
11
12
13def create_default_site(apps, schema_editor):
14 """Create a default site instance if it does not exist."""
15 # pylint: disable=unused-argument
16
17 if not Site.objects.filter(id=settings.SITE_ID).exists():
18 try:
19 Site.objects.create(id=settings.SITE_ID)
20 except IntegrityError as e:
21 LOGGER.error(
22 "Failed to create a site with id %d. Either adapt the SITE_ID "
23 "setting or remove the currently existing site.",
24 settings.SITE_ID,
25 )
26 raise e
27
28
29class Migration(migrations.Migration):
30 """Run the migration script."""
31
32 dependencies = [
33 ("sites", "0002_alter_domain_unique"),
34 ("crashreport_stats", "0005_remove_manual_default_value"),
35 ]
36
37 operations = [migrations.RunPython(create_default_site)]