From e042445ecfe37d64e92a682cd34a9a415ecdb5cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Jes=C3=BAs=20Pe=C3=B1a=20Rodr=C3=ADguez?= Date: Wed, 11 Jun 2025 13:34:21 +0200 Subject: [PATCH] fix(migration): create site stuff before socialaccount (#7999) --- api/docker-entrypoint.sh | 4 + ...k_and_fix_socialaccount_sites_migration.py | 80 +++++++++++++++++++ api/src/backend/config/celery.py | 7 ++ api/src/backend/manage.py | 6 ++ 4 files changed, 97 insertions(+) create mode 100644 api/src/backend/api/management/commands/check_and_fix_socialaccount_sites_migration.py diff --git a/api/docker-entrypoint.sh b/api/docker-entrypoint.sh index 0dff279d08..4ff9cc4cd1 100755 --- a/api/docker-entrypoint.sh +++ b/api/docker-entrypoint.sh @@ -3,6 +3,10 @@ apply_migrations() { echo "Applying database migrations..." + + # Fix Inconsistent migration history after adding sites app + poetry run python manage.py check_and_fix_socialaccount_sites_migration --database admin + poetry run python manage.py migrate --database admin } diff --git a/api/src/backend/api/management/commands/check_and_fix_socialaccount_sites_migration.py b/api/src/backend/api/management/commands/check_and_fix_socialaccount_sites_migration.py new file mode 100644 index 0000000000..361780683b --- /dev/null +++ b/api/src/backend/api/management/commands/check_and_fix_socialaccount_sites_migration.py @@ -0,0 +1,80 @@ +from django.contrib.sites.models import Site +from django.core.management.base import BaseCommand +from django.db import DEFAULT_DB_ALIAS, connection, connections, transaction +from django.db.migrations.recorder import MigrationRecorder + + +def table_exists(table_name): + with connection.cursor() as cursor: + cursor.execute( + """ + SELECT EXISTS ( + SELECT 1 FROM information_schema.tables + WHERE table_name = %s + ) + """, + [table_name], + ) + return cursor.fetchone()[0] + + +class Command(BaseCommand): + help = "Fix migration inconsistency between socialaccount and sites" + + def add_arguments(self, parser): + parser.add_argument( + "--database", + default=DEFAULT_DB_ALIAS, + help="Specifies the database to operate on.", + ) + + def handle(self, *args, **options): + db = options["database"] + connection = connections[db] + recorder = MigrationRecorder(connection) + + applied = set(recorder.applied_migrations()) + + has_social = ("socialaccount", "0001_initial") in applied + + with connection.cursor() as cursor: + cursor.execute( + """ + SELECT EXISTS ( + SELECT FROM information_schema.tables + WHERE table_name = 'django_site' + ); + """ + ) + site_table_exists = cursor.fetchone()[0] + + if has_social and not site_table_exists: + self.stdout.write( + f"Detected inconsistency in '{db}'. Creating 'django_site' table manually..." + ) + + with transaction.atomic(using=db): + with connection.schema_editor() as schema_editor: + schema_editor.create_model(Site) + + recorder.record_applied("sites", "0001_initial") + recorder.record_applied("sites", "0002_alter_domain_unique") + + self.stdout.write( + "Fixed: 'django_site' table created and migrations registered." + ) + + # Ensure the relationship table also exists + if not table_exists("socialaccount_socialapp_sites"): + self.stdout.write( + "Detected missing 'socialaccount_socialapp_sites' table. Creating manually..." + ) + with connection.schema_editor() as schema_editor: + from allauth.socialaccount.models import SocialApp + + schema_editor.create_model( + SocialApp._meta.get_field("sites").remote_field.through + ) + self.stdout.write( + "Fixed: 'socialaccount_socialapp_sites' table created." + ) diff --git a/api/src/backend/config/celery.py b/api/src/backend/config/celery.py index 4c667b7bc5..b3a0ab4b68 100644 --- a/api/src/backend/config/celery.py +++ b/api/src/backend/config/celery.py @@ -1,6 +1,13 @@ +import warnings + from celery import Celery, Task from config.env import env +# Suppress specific warnings from django-rest-auth: https://github.com/iMerica/dj-rest-auth/issues/684 +warnings.filterwarnings( + "ignore", category=UserWarning, module="dj_rest_auth.registration.serializers" +) + BROKER_VISIBILITY_TIMEOUT = env.int("DJANGO_BROKER_VISIBILITY_TIMEOUT", default=86400) celery_app = Celery("tasks") diff --git a/api/src/backend/manage.py b/api/src/backend/manage.py index 590fbeb713..b4b959d5b1 100755 --- a/api/src/backend/manage.py +++ b/api/src/backend/manage.py @@ -3,6 +3,12 @@ import os import sys +import warnings + +# Suppress specific warnings from django-rest-auth: https://github.com/iMerica/dj-rest-auth/issues/684 +warnings.filterwarnings( + "ignore", category=UserWarning, module="dj_rest_auth.registration.serializers" +) def main():