From d731800be02b20351217f183232ea25be57d6748 Mon Sep 17 00:00:00 2001 From: Dominik George <dominik.george@teckids.org> Date: Thu, 4 Nov 2021 11:54:42 +0100 Subject: [PATCH] [OAuth] Add migration for moving OAuth applications to custom model --- .../0023_oauth_application_model.py | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/aleksis/core/migrations/0023_oauth_application_model.py b/aleksis/core/migrations/0023_oauth_application_model.py index 7e5d2812f..8d57ae8c2 100644 --- a/aleksis/core/migrations/0023_oauth_application_model.py +++ b/aleksis/core/migrations/0023_oauth_application_model.py @@ -1,10 +1,25 @@ # Generated by Django 3.2.8 on 2021-11-04 09:52 +from django.apps import apps as global_apps from django.conf import settings -from django.db import migrations, models +from django.db import connection, migrations, models import django.db.models.deletion import oauth2_provider.generators +def migrate_oauth_applications(apps, schema_editor): + db_alias = schema_editor.connection.alias + + try: + OldApp = apps.get_model("oauth2_provider", "Application") + except LookupError: + return + NewApp = apps.get_model("core", "OAuthApplication") + + if connection.instrospection.table_names() & set(OldApp._meta.db_table): + NewApp.objects.using(db_alias).bulk_create( + [NewApp(**old_app) for old_app in OldApp.objects.values()] + ) + class Migration(migrations.Migration): @@ -13,9 +28,7 @@ class Migration(migrations.Migration): ('core', '0022_public_favicon'), ] - run_before = [ - ('oauth2_provider', '0001_initial'), - ] + run_before = [] operations = [ migrations.CreateModel( @@ -39,3 +52,11 @@ class Migration(migrations.Migration): }, ), ] + + if global_apps.is_installed("oauth2_provider"): + operations += [ + migrations.RunPython(migrate_oauth_applications), + migrations.RunSQL("DROP TABLE IF EXISTS oauth2_provider_application;"), + ] + else: + run_before.append(('oauth2_provider', '0001_initial')) -- GitLab