-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add migration that ensures there is always a system user available
This makes migrations more robust and allows them to reference such a system user, which becomes in handy in data migrations.
- Loading branch information
Showing
2 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
django/applications/catmaid/migrations/0050_ensure_system_user.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from django.conf import settings | ||
from django.core.exceptions import ImproperlyConfigured | ||
from django.db import migrations, models | ||
|
||
from catmaid.apps import get_system_user | ||
|
||
|
||
def forward(apps, schema_editor): | ||
User = apps.get_model('auth', 'User') | ||
try: | ||
system_user = get_system_user(User) | ||
except ImproperlyConfigured: | ||
system_user_name = 'catmaid-system-user' | ||
i = 0 | ||
# Find a username that isn't taken yet and create an inactive super | ||
# user. It can't be used to log in. | ||
while True: | ||
user_name = '{}{}'.format(system_user_name, (str(i)) if i > 0 else '') | ||
if User.objects.filter(username=user_name).count() == 0: | ||
User.objects.create(username=user_name, is_active=False, | ||
is_staff=False, is_superuser=True) | ||
break | ||
|
||
|
||
class Migration(migrations.Migration): | ||
"""This migration makes sure there is at least one inactive system user in | ||
the database. This user is needed in future data migrations.""" | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('catmaid', '0049_volume_tin_representation'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(forward, migrations.RunPython.noop), | ||
] |