authentik.core.migrations.0069_useractivity

 1# Generated by Django 5.2.17 on 2026-10-30 17:52
 2
 3import django.db.models.deletion
 4from django.conf import settings
 5from django.db import migrations, models
 6
 7
 8class Migration(migrations.Migration):
 9
10    dependencies = [
11        ("authentik_core", "0068_move_source_icons"),
12        ("authentik_events", "0020_alter_event_action"),
13    ]
14
15    operations = [
16        migrations.CreateModel(
17            name="UserActivity",
18            fields=[
19                (
20                    "user",
21                    models.OneToOneField(
22                        db_constraint=False,
23                        on_delete=django.db.models.deletion.CASCADE,
24                        primary_key=True,
25                        related_name="activity",
26                        serialize=False,
27                        to=settings.AUTH_USER_MODEL,
28                    ),
29                ),
30                ("last_activity_at", models.DateTimeField()),
31            ],
32        ),
33        # Seed activity from retained successful events without relying on event retention
34        # afterward.
35        migrations.RunSQL(
36            """
37            INSERT INTO authentik_core_useractivity AS activity (user_id, last_activity_at)
38            SELECT users.id, MAX(events.created)
39            FROM authentik_core_user users
40            JOIN authentik_events_event events ON events.user->'pk' = to_jsonb(users.id)
41            WHERE events.action IN ('login', 'authorize_application')
42            GROUP BY users.id
43            ON CONFLICT (user_id) DO UPDATE
44            SET last_activity_at = GREATEST(activity.last_activity_at, EXCLUDED.last_activity_at)
45            """,
46            reverse_sql=migrations.RunSQL.noop,
47        ),
48    ]
class Migration(django.db.migrations.migration.Migration):
 9class Migration(migrations.Migration):
10
11    dependencies = [
12        ("authentik_core", "0068_move_source_icons"),
13        ("authentik_events", "0020_alter_event_action"),
14    ]
15
16    operations = [
17        migrations.CreateModel(
18            name="UserActivity",
19            fields=[
20                (
21                    "user",
22                    models.OneToOneField(
23                        db_constraint=False,
24                        on_delete=django.db.models.deletion.CASCADE,
25                        primary_key=True,
26                        related_name="activity",
27                        serialize=False,
28                        to=settings.AUTH_USER_MODEL,
29                    ),
30                ),
31                ("last_activity_at", models.DateTimeField()),
32            ],
33        ),
34        # Seed activity from retained successful events without relying on event retention
35        # afterward.
36        migrations.RunSQL(
37            """
38            INSERT INTO authentik_core_useractivity AS activity (user_id, last_activity_at)
39            SELECT users.id, MAX(events.created)
40            FROM authentik_core_user users
41            JOIN authentik_events_event events ON events.user->'pk' = to_jsonb(users.id)
42            WHERE events.action IN ('login', 'authorize_application')
43            GROUP BY users.id
44            ON CONFLICT (user_id) DO UPDATE
45            SET last_activity_at = GREATEST(activity.last_activity_at, EXCLUDED.last_activity_at)
46            """,
47            reverse_sql=migrations.RunSQL.noop,
48        ),
49    ]

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', '0068_move_source_icons'), ('authentik_events', '0020_alter_event_action')]
operations = [<CreateModel name='UserActivity', fields=[('user', <django.db.models.fields.related.OneToOneField>), ('last_activity_at', <django.db.models.fields.DateTimeField>)]>, <RunSQL "\n INSERT INTO authentik_core_useractivity AS activity (user_id, last_activity_at)\n SELECT users.id, MAX(events.created)\n FROM authentik_core_user users\n JOIN authentik_events_event events ON events.user->'pk' = to_jsonb(users.id)\n WHERE events.action IN ('login', 'authorize_application')\n GROUP BY users.id\n ON CONFLICT (user_id) DO UPDATE\n SET last_activity_at = GREATEST(activity.last_activity_at, EXCLUDED.last_activity_at)\n " reverse_sql=''>]