authentik.core.migrations.0058_setup
1# Generated by Django 5.2.13 on 2026-04-21 18:49 2from django.apps.registry import Apps 3from django.db import connections, migrations 4from django.db.backends.base.schema import BaseDatabaseSchemaEditor 5 6 7def check_is_already_setup(apps: Apps, schema_editor: BaseDatabaseSchemaEditor): 8 from django.conf import settings 9 10 from authentik.flows.models import FlowAuthenticationRequirement 11 12 Flow = apps.get_model("authentik_flows", "Flow") 13 User = apps.get_model("authentik_core", "User") 14 15 db_alias = schema_editor.connection.alias 16 17 # Upgrading from a previous version. authentik_version_history is an unmanaged table 18 # created by the system migrations, so query it directly. 19 if not settings.TEST: 20 with connections[db_alias].cursor() as cursor: 21 cursor.execute("SELECT COUNT(*) FROM authentik_version_history") 22 if cursor.fetchone()[0] > 1: 23 return True 24 # OOBE flow sets itself to this authentication requirement once finished 25 if ( 26 Flow.objects.using(db_alias) 27 .filter( 28 slug="initial-setup", authentication=FlowAuthenticationRequirement.REQUIRE_SUPERUSER 29 ) 30 .exists() 31 ): 32 return True 33 # non-akadmin and non-guardian anonymous user exist 34 if ( 35 User.objects.using(db_alias) 36 .exclude(username="akadmin") 37 .exclude(username="AnonymousUser") 38 .exists() 39 ): 40 return True 41 return False 42 43 44def update_setup_flag(apps: Apps, schema_editor: BaseDatabaseSchemaEditor): 45 from authentik.core.apps import Setup 46 47 is_already_setup = check_is_already_setup(apps, schema_editor) 48 if is_already_setup: 49 Tenant = apps.get_model("authentik_tenants", "Tenant") 50 db_alias = schema_editor.connection.alias 51 tenant = ( 52 Tenant.objects.using(db_alias) 53 .filter(schema_name=schema_editor.connection.schema_name) 54 .first() 55 ) 56 if tenant is None: 57 return 58 tenant.flags[Setup().key] = True 59 tenant.save(update_fields=["flags"], using=db_alias) 60 61 62class Migration(migrations.Migration): 63 64 dependencies = [ 65 ("authentik_core", "0057_remove_user_groups_remove_user_user_permissions_and_more"), 66 # 0024_flow_authentication adds the `authentication` field. 67 ("authentik_flows", "0024_flow_authentication"), 68 # 0006_tenant_flags adds the `flags` field this migration updates. 69 ("authentik_tenants", "0006_tenant_flags"), 70 ] 71 72 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):
8def check_is_already_setup(apps: Apps, schema_editor: BaseDatabaseSchemaEditor): 9 from django.conf import settings 10 11 from authentik.flows.models import FlowAuthenticationRequirement 12 13 Flow = apps.get_model("authentik_flows", "Flow") 14 User = apps.get_model("authentik_core", "User") 15 16 db_alias = schema_editor.connection.alias 17 18 # Upgrading from a previous version. authentik_version_history is an unmanaged table 19 # created by the system migrations, so query it directly. 20 if not settings.TEST: 21 with connections[db_alias].cursor() as cursor: 22 cursor.execute("SELECT COUNT(*) FROM authentik_version_history") 23 if cursor.fetchone()[0] > 1: 24 return True 25 # OOBE flow sets itself to this authentication requirement once finished 26 if ( 27 Flow.objects.using(db_alias) 28 .filter( 29 slug="initial-setup", authentication=FlowAuthenticationRequirement.REQUIRE_SUPERUSER 30 ) 31 .exists() 32 ): 33 return True 34 # non-akadmin and non-guardian anonymous user exist 35 if ( 36 User.objects.using(db_alias) 37 .exclude(username="akadmin") 38 .exclude(username="AnonymousUser") 39 .exists() 40 ): 41 return True 42 return False
def
update_setup_flag( apps: django.apps.registry.Apps, schema_editor: django.db.backends.base.schema.BaseDatabaseSchemaEditor):
45def update_setup_flag(apps: Apps, schema_editor: BaseDatabaseSchemaEditor): 46 from authentik.core.apps import Setup 47 48 is_already_setup = check_is_already_setup(apps, schema_editor) 49 if is_already_setup: 50 Tenant = apps.get_model("authentik_tenants", "Tenant") 51 db_alias = schema_editor.connection.alias 52 tenant = ( 53 Tenant.objects.using(db_alias) 54 .filter(schema_name=schema_editor.connection.schema_name) 55 .first() 56 ) 57 if tenant is None: 58 return 59 tenant.flags[Setup().key] = True 60 tenant.save(update_fields=["flags"], using=db_alias)
class
Migration(django.db.migrations.migration.Migration):
63class Migration(migrations.Migration): 64 65 dependencies = [ 66 ("authentik_core", "0057_remove_user_groups_remove_user_user_permissions_and_more"), 67 # 0024_flow_authentication adds the `authentication` field. 68 ("authentik_flows", "0024_flow_authentication"), 69 # 0006_tenant_flags adds the `flags` field this migration updates. 70 ("authentik_tenants", "0006_tenant_flags"), 71 ] 72 73 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.