authentik.core.migrations.0058_setup

 1# Generated by Django 5.2.13 on 2026-04-21 18:49
 2from django.apps.registry import Apps
 3
 4from django.db.backends.base.schema import BaseDatabaseSchemaEditor
 5
 6from django.db import migrations
 7
 8
 9def check_is_already_setup(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
10    from django.conf import settings
11    from authentik.flows.models import FlowAuthenticationRequirement
12
13    VersionHistory = apps.get_model("authentik_admin", "VersionHistory")
14    Flow = apps.get_model("authentik_flows", "Flow")
15    User = apps.get_model("authentik_core", "User")
16
17    db_alias = schema_editor.connection.alias
18
19    # Upgrading from a previous version
20    if not settings.TEST and VersionHistory.objects.using(db_alias).count() > 1:
21        return True
22    # OOBE flow sets itself to this authentication requirement once finished
23    if (
24        Flow.objects.using(db_alias)
25        .filter(
26            slug="initial-setup", authentication=FlowAuthenticationRequirement.REQUIRE_SUPERUSER
27        )
28        .exists()
29    ):
30        return True
31    # non-akadmin and non-guardian anonymous user exist
32    if (
33        User.objects.using(db_alias)
34        .exclude(username="akadmin")
35        .exclude(username="AnonymousUser")
36        .exists()
37    ):
38        return True
39    return False
40
41
42def update_setup_flag(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
43    from authentik.core.apps import Setup
44    from authentik.tenants.utils import get_current_tenant
45
46    is_already_setup = check_is_already_setup(apps, schema_editor)
47    if is_already_setup:
48        tenant = get_current_tenant()
49        tenant.flags[Setup().key] = True
50        tenant.save()
51
52
53class Migration(migrations.Migration):
54
55    dependencies = [
56        ("authentik_core", "0057_remove_user_groups_remove_user_user_permissions_and_more"),
57        # 0024_flow_authentication adds the `authentication` field.
58        ("authentik_flows", "0024_flow_authentication"),
59    ]
60
61    operations = [migrations.RunPython(update_setup_flag, migrations.RunPython.noop)]
def check_is_already_setup( apps: django.apps.registry.Apps, schema_editor: django.db.backends.base.schema.BaseDatabaseSchemaEditor):
10def check_is_already_setup(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
11    from django.conf import settings
12    from authentik.flows.models import FlowAuthenticationRequirement
13
14    VersionHistory = apps.get_model("authentik_admin", "VersionHistory")
15    Flow = apps.get_model("authentik_flows", "Flow")
16    User = apps.get_model("authentik_core", "User")
17
18    db_alias = schema_editor.connection.alias
19
20    # Upgrading from a previous version
21    if not settings.TEST and VersionHistory.objects.using(db_alias).count() > 1:
22        return True
23    # OOBE flow sets itself to this authentication requirement once finished
24    if (
25        Flow.objects.using(db_alias)
26        .filter(
27            slug="initial-setup", authentication=FlowAuthenticationRequirement.REQUIRE_SUPERUSER
28        )
29        .exists()
30    ):
31        return True
32    # non-akadmin and non-guardian anonymous user exist
33    if (
34        User.objects.using(db_alias)
35        .exclude(username="akadmin")
36        .exclude(username="AnonymousUser")
37        .exists()
38    ):
39        return True
40    return False
def update_setup_flag( apps: django.apps.registry.Apps, schema_editor: django.db.backends.base.schema.BaseDatabaseSchemaEditor):
43def update_setup_flag(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
44    from authentik.core.apps import Setup
45    from authentik.tenants.utils import get_current_tenant
46
47    is_already_setup = check_is_already_setup(apps, schema_editor)
48    if is_already_setup:
49        tenant = get_current_tenant()
50        tenant.flags[Setup().key] = True
51        tenant.save()
class Migration(django.db.migrations.migration.Migration):
54class Migration(migrations.Migration):
55
56    dependencies = [
57        ("authentik_core", "0057_remove_user_groups_remove_user_user_permissions_and_more"),
58        # 0024_flow_authentication adds the `authentication` field.
59        ("authentik_flows", "0024_flow_authentication"),
60    ]
61
62    operations = [migrations.RunPython(update_setup_flag, migrations.RunPython.noop)]

The base class for all migrations.

Migration files will import this from django.db.migrations.Migration and subclass it as a class called Migration. It will have one or more of the following attributes:

  • operations: A list of Operation instances, probably from django.db.migrations.operations
  • dependencies: A list of tuples of (app_path, migration_name)
  • run_before: A list of tuples of (app_path, migration_name)
  • replaces: A list of migration_names

Note that all migrations come out of migrations and into the Loader or Graph as instances, having been initialized with their app label and name.

dependencies = [('authentik_core', '0057_remove_user_groups_remove_user_user_permissions_and_more'), ('authentik_flows', '0024_flow_authentication')]
operations = [<RunPython <function update_setup_flag>, <function RunPython.noop>>]