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

Squash some apps' migrations #523

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
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),
]
Loading