From f1f95521c7c12ffee3b24719efd8ea4161381ce1 Mon Sep 17 00:00:00 2001 From: Kevin Meinhardt Date: Fri, 22 Nov 2024 20:17:51 +0100 Subject: [PATCH] Fix error when users.table does not exist yet during initialize --- src/olympia/amo/management/commands/initialize.py | 10 +++++++--- src/olympia/amo/tests/test_commands.py | 10 ++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/olympia/amo/management/commands/initialize.py b/src/olympia/amo/management/commands/initialize.py index 40e25df0dc26..2604ed0408ad 100644 --- a/src/olympia/amo/management/commands/initialize.py +++ b/src/olympia/amo/management/commands/initialize.py @@ -3,6 +3,8 @@ from django.conf import settings from django.core.management import call_command +from olympia.users.models import UserProfile + from .. import BaseDataCommand @@ -25,9 +27,11 @@ def add_arguments(self, parser): ) def local_admin_exists(self): - from olympia.users.models import UserProfile - - return UserProfile.objects.filter(email=settings.LOCAL_ADMIN_EMAIL).exists() + try: + return UserProfile.objects.filter(email=settings.LOCAL_ADMIN_EMAIL).exists() + except Exception as e: + logging.error(f'Error checking if local admin exists: {e}') + return False def handle(self, *args, **options): """ diff --git a/src/olympia/amo/tests/test_commands.py b/src/olympia/amo/tests/test_commands.py index 2ec85b074156..d51d720ac2a3 100644 --- a/src/olympia/amo/tests/test_commands.py +++ b/src/olympia/amo/tests/test_commands.py @@ -350,6 +350,16 @@ def test_handle_migration_failure(self): ], ) + @mock.patch('olympia.amo.management.commands.initialize.UserProfile.objects.filter') + def test_handle_mysql_exception(self, mock_filter): + mock_filter.return_value.exists.side_effect = Exception('test') + + call_command('initialize') + self._assert_commands_called_in_order( + self.mocks['mock_call_command'], + [self.mock_commands.data_seed], + ) + class TestBaseDataCommand(BaseTestDataCommand): def setUp(self):