authentik.stages.prompt.migrations.0009_prompt_name

 1# Generated by Django 4.1.5 on 2023-01-23 19:42
 2from uuid import uuid4
 3
 4from django.apps.registry import Apps
 5from django.db import migrations, models
 6from django.db.backends.base.schema import BaseDatabaseSchemaEditor
 7
 8
 9def set_generated_name(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
10    db_alias = schema_editor.connection.alias
11    Prompt = apps.get_model("authentik_stages_prompt", "prompt")
12
13    for prompt in Prompt.objects.using(db_alias).all():
14        name = prompt.field_key
15        stage = prompt.promptstage_set.using(db_alias).order_by("name").first()
16        if stage:
17            name += "_" + stage.name
18        else:
19            name += "_" + str(uuid4())
20        prompt.name = name
21        prompt.save()
22
23
24class Migration(migrations.Migration):
25    dependencies = [
26        ("authentik_stages_prompt", "0008_alter_prompt_type"),
27    ]
28
29    operations = [
30        migrations.AddField(
31            model_name="prompt",
32            name="name",
33            field=models.TextField(default="", unique=False, db_index=False, blank=False),
34            preserve_default=False,
35        ),
36        migrations.RunPython(code=set_generated_name),
37        migrations.AlterField(
38            model_name="prompt",
39            name="name",
40            field=models.TextField(unique=True),
41        ),
42    ]
def set_generated_name( apps: django.apps.registry.Apps, schema_editor: django.db.backends.base.schema.BaseDatabaseSchemaEditor):
10def set_generated_name(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
11    db_alias = schema_editor.connection.alias
12    Prompt = apps.get_model("authentik_stages_prompt", "prompt")
13
14    for prompt in Prompt.objects.using(db_alias).all():
15        name = prompt.field_key
16        stage = prompt.promptstage_set.using(db_alias).order_by("name").first()
17        if stage:
18            name += "_" + stage.name
19        else:
20            name += "_" + str(uuid4())
21        prompt.name = name
22        prompt.save()
class Migration(django.db.migrations.migration.Migration):
25class Migration(migrations.Migration):
26    dependencies = [
27        ("authentik_stages_prompt", "0008_alter_prompt_type"),
28    ]
29
30    operations = [
31        migrations.AddField(
32            model_name="prompt",
33            name="name",
34            field=models.TextField(default="", unique=False, db_index=False, blank=False),
35            preserve_default=False,
36        ),
37        migrations.RunPython(code=set_generated_name),
38        migrations.AlterField(
39            model_name="prompt",
40            name="name",
41            field=models.TextField(unique=True),
42        ),
43    ]

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_stages_prompt', '0008_alter_prompt_type')]
operations = [<AddField model_name='prompt', name='name', field=<django.db.models.fields.TextField>, preserve_default=False>, <RunPython code=<function set_generated_name>>, <AlterField model_name='prompt', name='name', field=<django.db.models.fields.TextField>>]