Skip to content

Commit

Permalink
Move default_site migration to core app
Browse files Browse the repository at this point in the history
  • Loading branch information
brianhelba committed Mar 21, 2022
1 parent f007cae commit a7fb0f9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
40 changes: 40 additions & 0 deletions isic/core/migrations/0001_default_site.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from django.conf import settings
from django.db import migrations
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django.db.migrations.state import StateApps


def update_default_site(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor):
Site = apps.get_model('sites', 'Site') # noqa: N806

# A default site object may or may not exist.
# If this is a brand-new database, the post_migrate will not fire until the very end of the
# "migrate" command, so the sites app will not have created a default site object yet.
# If this is an existing database, the sites app will likely have created an default site
# object already.
Site.objects.update_or_create(
pk=settings.SITE_ID, defaults={'domain': 'api.isic-archive.com', 'name': 'ISIC Archive'}
)


def rollback_default_site(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor):
Site = apps.get_model('sites', 'Site') # noqa: N806

# This is the initial value of the default site object, as populated by the sites app.
# If it doesn't exist for some reason, there is nothing to roll back.
Site.objects.filter(pk=settings.SITE_ID).update(domain='example.com', name='example.com')


class Migration(migrations.Migration):
replaces = [
('login', '0001_default_site'),
]

dependencies = [
# This is the final sites app migration
('sites', '0002_alter_domain_unique'),
]

operations = [
migrations.RunPython(update_default_site, rollback_default_site),
]
11 changes: 11 additions & 0 deletions isic/core/migrations/0002_merge_default_site.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('core', '0001_default_site'),
('core', '0001_initial_squashed'),
]

operations = []

0 comments on commit a7fb0f9

Please sign in to comment.