From 9117c2234b69b144e5e569b628a04b28c780b005 Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 13 Jun 2023 07:34:56 +1000 Subject: [PATCH] Migration fixes (#5025) * Catch exception on is_worker_running - Exception may occur if table is not yet available - If it *does* then we can assume the worker is no running * General error catch in offload_task * Pick an earlier migration to run from? * Update initial common migration - Handle error on table duplication * Change target migration file - Ensure that part MPTT migrations have been applied! * Fix migration ref - Need 0025 - Price field needs to be available --- InvenTree/InvenTree/status.py | 9 ++++++- InvenTree/InvenTree/tasks.py | 2 ++ InvenTree/common/migrations/0001_initial.py | 27 ++++++++++++++++++++- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/InvenTree/InvenTree/status.py b/InvenTree/InvenTree/status.py index cb837a2439..a38da21b32 100644 --- a/InvenTree/InvenTree/status.py +++ b/InvenTree/InvenTree/status.py @@ -38,7 +38,14 @@ def is_worker_running(**kwargs): ) # If any results are returned, then the background worker is running! - return results.exists() + try: + result = results.exists() + except Exception: + # We may throw an exception if the database is not ready, + # or if the django_q table is not yet created (i.e. in CI testing) + result = False + + return result def check_system_health(**kwargs): diff --git a/InvenTree/InvenTree/tasks.py b/InvenTree/InvenTree/tasks.py index 9a242040e3..41aeddcbf6 100644 --- a/InvenTree/InvenTree/tasks.py +++ b/InvenTree/InvenTree/tasks.py @@ -186,6 +186,8 @@ def offload_task(taskname, *args, force_async=False, force_sync=False, **kwargs) task.run() except ImportError: raise_warning(f"WARNING: '{taskname}' not started - Function not found") + except Exception as exc: + raise_warning(f"WARNING: '{taskname}' not started due to {type(exc)}") else: if callable(taskname): diff --git a/InvenTree/common/migrations/0001_initial.py b/InvenTree/common/migrations/0001_initial.py index a3a357b86f..089844a70d 100644 --- a/InvenTree/common/migrations/0001_initial.py +++ b/InvenTree/common/migrations/0001_initial.py @@ -4,15 +4,40 @@ import django.core.validators from django.db import migrations, models +class CreateModelOrSkip(migrations.CreateModel): + """Custom migration operation to create a model if it does not already exist. + + - If the model already exists, the migration is skipped + - This class has been added to deal with some errors being thrown in CI tests + - The 'common_currency' table doesn't exist anymore anyway! + - In the future, these migrations will be squashed + """ + + def database_forwards(self, app_label, schema_editor, from_state, to_state) -> None: + """Forwards migration *attempts* to create the model, but will fail gracefully if it already exists""" + + try: + super().database_forwards(app_label, schema_editor, from_state, to_state) + except Exception: + pass + + def state_forwards(self, app_label, state) -> None: + try: + super().state_forwards(app_label, state) + except Exception: + pass + + class Migration(migrations.Migration): initial = True + atomic = False dependencies = [ ] operations = [ - migrations.CreateModel( + CreateModelOrSkip( name='Currency', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),