authentik.tenants.migrations.0001_initial

  1# Generated by Django 4.2.7 on 2023-11-15 10:53
  2
  3import uuid
  4
  5import django.db.models.deletion
  6from django.db import migrations, models
  7
  8import authentik.lib.utils.time
  9from authentik.lib.config import CONFIG
 10
 11
 12def create_default_tenant(apps, schema_editor):
 13    db_alias = schema_editor.connection.alias
 14
 15    Tenant = apps.get_model("authentik_tenants", "Tenant")
 16    Tenant.objects.using(db_alias).create(
 17        schema_name=CONFIG.get("postgresql.default_schema", "public"),
 18        name="Default",
 19        ready=True,
 20        avatars=CONFIG.get("avatars", "gravatar,initials"),
 21        default_user_change_name=CONFIG.get_bool("default_user_change_name", True),
 22        default_user_change_email=CONFIG.get_bool("default_user_change_email", False),
 23        default_user_change_username=CONFIG.get_bool("default_user_change_username", False),
 24        footer_links=CONFIG.get("footer_links", default=[]),
 25        gdpr_compliance=CONFIG.get_bool("gdpr_compliance", True),
 26        impersonation=CONFIG.get_bool("impersonation", True),
 27    )
 28
 29
 30class Migration(migrations.Migration):
 31    initial = True
 32
 33    dependencies = []
 34
 35    operations = [
 36        migrations.CreateModel(
 37            name="Tenant",
 38            fields=[
 39                (
 40                    "schema_name",
 41                    models.CharField(
 42                        db_index=True,
 43                        max_length=63,
 44                        unique=True,
 45                    ),
 46                ),
 47                (
 48                    "tenant_uuid",
 49                    models.UUIDField(
 50                        default=uuid.uuid4, editable=False, primary_key=True, serialize=False
 51                    ),
 52                ),
 53                ("name", models.TextField()),
 54                ("ready", models.BooleanField(default=False)),
 55                (
 56                    "avatars",
 57                    models.TextField(
 58                        default="gravatar,initials",
 59                        help_text="Configure how authentik should show avatars for users.",
 60                    ),
 61                ),
 62                (
 63                    "default_user_change_name",
 64                    models.BooleanField(
 65                        default=True, help_text="Enable the ability for users to change their name."
 66                    ),
 67                ),
 68                (
 69                    "default_user_change_email",
 70                    models.BooleanField(
 71                        default=False,
 72                        help_text="Enable the ability for users to change their email address.",
 73                    ),
 74                ),
 75                (
 76                    "default_user_change_username",
 77                    models.BooleanField(
 78                        default=False,
 79                        help_text="Enable the ability for users to change their username.",
 80                    ),
 81                ),
 82                (
 83                    "event_retention",
 84                    models.TextField(
 85                        default="days=365",
 86                        help_text="Events will be deleted after this duration.(Format: weeks=3;days=2;hours=3,seconds=2).",
 87                        validators=[authentik.lib.utils.time.timedelta_string_validator],
 88                    ),
 89                ),
 90                (
 91                    "footer_links",
 92                    models.JSONField(
 93                        blank=True,
 94                        default=list,
 95                        help_text="The option configures the footer links on the flow executor pages.",
 96                    ),
 97                ),
 98                (
 99                    "gdpr_compliance",
100                    models.BooleanField(
101                        default=True,
102                        help_text="When enabled, all the events caused by a user will be deleted upon the user's deletion.",
103                    ),
104                ),
105                (
106                    "impersonation",
107                    models.BooleanField(
108                        default=True, help_text="Globally enable/disable impersonation."
109                    ),
110                ),
111            ],
112            options={
113                "verbose_name": "Tenant",
114                "verbose_name_plural": "Tenants",
115            },
116        ),
117        migrations.CreateModel(
118            name="Domain",
119            fields=[
120                (
121                    "id",
122                    models.AutoField(
123                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
124                    ),
125                ),
126                ("domain", models.CharField(db_index=True, max_length=253, unique=True)),
127                ("is_primary", models.BooleanField(db_index=True, default=True)),
128                (
129                    "tenant",
130                    models.ForeignKey(
131                        on_delete=django.db.models.deletion.CASCADE,
132                        related_name="domains",
133                        to="authentik_tenants.tenant",
134                    ),
135                ),
136            ],
137            options={
138                "verbose_name": "Domain",
139                "verbose_name_plural": "Domains",
140            },
141        ),
142        migrations.RunPython(code=create_default_tenant, reverse_code=migrations.RunPython.noop),
143    ]
def create_default_tenant(apps, schema_editor):
13def create_default_tenant(apps, schema_editor):
14    db_alias = schema_editor.connection.alias
15
16    Tenant = apps.get_model("authentik_tenants", "Tenant")
17    Tenant.objects.using(db_alias).create(
18        schema_name=CONFIG.get("postgresql.default_schema", "public"),
19        name="Default",
20        ready=True,
21        avatars=CONFIG.get("avatars", "gravatar,initials"),
22        default_user_change_name=CONFIG.get_bool("default_user_change_name", True),
23        default_user_change_email=CONFIG.get_bool("default_user_change_email", False),
24        default_user_change_username=CONFIG.get_bool("default_user_change_username", False),
25        footer_links=CONFIG.get("footer_links", default=[]),
26        gdpr_compliance=CONFIG.get_bool("gdpr_compliance", True),
27        impersonation=CONFIG.get_bool("impersonation", True),
28    )
class Migration(django.db.migrations.migration.Migration):
 31class Migration(migrations.Migration):
 32    initial = True
 33
 34    dependencies = []
 35
 36    operations = [
 37        migrations.CreateModel(
 38            name="Tenant",
 39            fields=[
 40                (
 41                    "schema_name",
 42                    models.CharField(
 43                        db_index=True,
 44                        max_length=63,
 45                        unique=True,
 46                    ),
 47                ),
 48                (
 49                    "tenant_uuid",
 50                    models.UUIDField(
 51                        default=uuid.uuid4, editable=False, primary_key=True, serialize=False
 52                    ),
 53                ),
 54                ("name", models.TextField()),
 55                ("ready", models.BooleanField(default=False)),
 56                (
 57                    "avatars",
 58                    models.TextField(
 59                        default="gravatar,initials",
 60                        help_text="Configure how authentik should show avatars for users.",
 61                    ),
 62                ),
 63                (
 64                    "default_user_change_name",
 65                    models.BooleanField(
 66                        default=True, help_text="Enable the ability for users to change their name."
 67                    ),
 68                ),
 69                (
 70                    "default_user_change_email",
 71                    models.BooleanField(
 72                        default=False,
 73                        help_text="Enable the ability for users to change their email address.",
 74                    ),
 75                ),
 76                (
 77                    "default_user_change_username",
 78                    models.BooleanField(
 79                        default=False,
 80                        help_text="Enable the ability for users to change their username.",
 81                    ),
 82                ),
 83                (
 84                    "event_retention",
 85                    models.TextField(
 86                        default="days=365",
 87                        help_text="Events will be deleted after this duration.(Format: weeks=3;days=2;hours=3,seconds=2).",
 88                        validators=[authentik.lib.utils.time.timedelta_string_validator],
 89                    ),
 90                ),
 91                (
 92                    "footer_links",
 93                    models.JSONField(
 94                        blank=True,
 95                        default=list,
 96                        help_text="The option configures the footer links on the flow executor pages.",
 97                    ),
 98                ),
 99                (
100                    "gdpr_compliance",
101                    models.BooleanField(
102                        default=True,
103                        help_text="When enabled, all the events caused by a user will be deleted upon the user's deletion.",
104                    ),
105                ),
106                (
107                    "impersonation",
108                    models.BooleanField(
109                        default=True, help_text="Globally enable/disable impersonation."
110                    ),
111                ),
112            ],
113            options={
114                "verbose_name": "Tenant",
115                "verbose_name_plural": "Tenants",
116            },
117        ),
118        migrations.CreateModel(
119            name="Domain",
120            fields=[
121                (
122                    "id",
123                    models.AutoField(
124                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
125                    ),
126                ),
127                ("domain", models.CharField(db_index=True, max_length=253, unique=True)),
128                ("is_primary", models.BooleanField(db_index=True, default=True)),
129                (
130                    "tenant",
131                    models.ForeignKey(
132                        on_delete=django.db.models.deletion.CASCADE,
133                        related_name="domains",
134                        to="authentik_tenants.tenant",
135                    ),
136                ),
137            ],
138            options={
139                "verbose_name": "Domain",
140                "verbose_name_plural": "Domains",
141            },
142        ),
143        migrations.RunPython(code=create_default_tenant, reverse_code=migrations.RunPython.noop),
144    ]

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.

initial = True
dependencies = []
operations = [<CreateModel name='Tenant', fields=[('schema_name', <django.db.models.fields.CharField>), ('tenant_uuid', <django.db.models.fields.UUIDField>), ('name', <django.db.models.fields.TextField>), ('ready', <django.db.models.fields.BooleanField>), ('avatars', <django.db.models.fields.TextField>), ('default_user_change_name', <django.db.models.fields.BooleanField>), ('default_user_change_email', <django.db.models.fields.BooleanField>), ('default_user_change_username', <django.db.models.fields.BooleanField>), ('event_retention', <django.db.models.fields.TextField>), ('footer_links', <django.db.models.fields.json.JSONField>), ('gdpr_compliance', <django.db.models.fields.BooleanField>), ('impersonation', <django.db.models.fields.BooleanField>)], options={'verbose_name': 'Tenant', 'verbose_name_plural': 'Tenants'}>, <CreateModel name='Domain', fields=[('id', <django.db.models.fields.AutoField>), ('domain', <django.db.models.fields.CharField>), ('is_primary', <django.db.models.fields.BooleanField>), ('tenant', <django.db.models.fields.related.ForeignKey>)], options={'verbose_name': 'Domain', 'verbose_name_plural': 'Domains'}>, <RunPython code=<function create_default_tenant>, reverse_code=<function RunPython.noop>>]