mirror of
https://github.com/goauthentik/authentik.git
synced 2026-08-30 18:51:39 -07:00
74 lines
2.6 KiB
Python
74 lines
2.6 KiB
Python
# Generated by Django 5.2.17 on 2026-08-30 21:31
|
|
|
|
import django.db.models.deletion
|
|
from django.db import migrations, models
|
|
|
|
|
|
def migrate_plex_token(apps, schema_editor):
|
|
"""Move each source's plex_token into a Secret object"""
|
|
db_alias = schema_editor.connection.alias
|
|
PlexSource = apps.get_model("authentik_sources_plex", "PlexSource")
|
|
Secret = apps.get_model("authentik_secrets", "Secret")
|
|
names = set(Secret.objects.using(db_alias).values_list("name", flat=True))
|
|
for source in PlexSource.objects.using(db_alias).exclude(_plex_token=""):
|
|
base = f"{source.name} Plex token"
|
|
name, idx = base, 2
|
|
while name in names:
|
|
name = f"{base} ({idx})"
|
|
idx += 1
|
|
names.add(name)
|
|
source.secret = Secret.objects.using(db_alias).create(
|
|
name=name,
|
|
value=source._plex_token,
|
|
type="multiline" if "\n" in source._plex_token else "text",
|
|
)
|
|
source.save(update_fields=["secret"])
|
|
|
|
|
|
def rollback_plex_token(apps, schema_editor):
|
|
"""Copy secret values back into the legacy column, so a downgrade keeps values
|
|
set or rotated after the upgrade"""
|
|
db_alias = schema_editor.connection.alias
|
|
PlexSource = apps.get_model("authentik_sources_plex", "PlexSource")
|
|
for source in PlexSource.objects.using(db_alias).exclude(secret=None).select_related("secret"):
|
|
source._plex_token = source.secret.value
|
|
source.save(update_fields=["_plex_token"])
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
dependencies = [
|
|
("authentik_secrets", "0001_initial"),
|
|
("authentik_sources_plex", "0006_migrate_groupplexsourceconnection"),
|
|
]
|
|
|
|
operations = [
|
|
migrations.AlterField(
|
|
model_name="plexsource",
|
|
name="plex_token",
|
|
field=models.TextField(
|
|
db_column="plex_token", help_text="Plex token used to check friends"
|
|
),
|
|
),
|
|
migrations.RenameField(
|
|
model_name="plexsource",
|
|
old_name="plex_token",
|
|
new_name="_plex_token",
|
|
),
|
|
migrations.AddField(
|
|
model_name="plexsource",
|
|
name="secret",
|
|
field=models.ForeignKey(
|
|
blank=True,
|
|
default=None,
|
|
help_text="Plex token used to check friends",
|
|
null=True,
|
|
on_delete=django.db.models.deletion.PROTECT,
|
|
related_name="plex_sources",
|
|
to="authentik_secrets.secret",
|
|
verbose_name="Plex token",
|
|
),
|
|
),
|
|
migrations.RunPython(migrate_plex_token, rollback_plex_token),
|
|
]
|