2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-07-02 03:30:54 +00:00

Plugin auto migrate (#5668)

* Improved handling of race condition when saving setting value

* Improvements for managing pending migrations

- Inform user if there are outstanding migrations
- reload plugin registry (if necessary)

* Increase django-q polling time

According to this thread, should reduce multiple workers taking the same task:

https://github.com/Koed00/django-q/issues/183#issuecomment-239676084

* Revert default behavior

* Better logging

* Remove comment

* Update unit test

* Revert maintenance mode behaviour

* raise ValidationError in settings
This commit is contained in:
Oliver
2023-10-06 11:38:01 +11:00
committed by GitHub
parent c7eb90347a
commit 7ab5ddcd7d
9 changed files with 93 additions and 36 deletions

View File

@ -595,6 +595,8 @@ class BaseInvenTreeSetting(models.Model):
if change_user is not None and not change_user.is_staff:
return
attempts = int(kwargs.get('attempts', 3))
filters = {
'key__iexact': key,
@ -615,8 +617,22 @@ class BaseInvenTreeSetting(models.Model):
if setting.is_bool():
value = InvenTree.helpers.str2bool(value)
setting.value = str(value)
setting.save()
try:
setting.value = str(value)
setting.save()
except ValidationError as exc:
# We need to know about validation errors
raise exc
except IntegrityError:
# Likely a race condition has caused a duplicate entry to be created
if attempts > 0:
# Try again
logger.info("Duplicate setting key '%s' for %s - trying again", key, str(cls))
cls.set_setting(key, value, change_user, create=create, attempts=attempts - 1, **kwargs)
except Exception as exc:
# Some other error
logger.exception("Error setting setting '%s' for %s: %s", key, str(cls), str(type(exc)))
pass
key = models.CharField(max_length=50, blank=False, unique=False, help_text=_('Settings key (must be unique - case insensitive)'))
@ -1050,6 +1066,13 @@ class InvenTreeSetting(BaseInvenTreeSetting):
'hidden': True,
},
'_PENDING_MIGRATIONS': {
'name': _('Pending migrations'),
'description': _('Number of pending database migrations'),
'default': 0,
'validator': int,
},
'INVENTREE_INSTANCE': {
'name': _('Server Instance Name'),
'default': 'InvenTree',

View File

@ -335,8 +335,10 @@ class GlobalSettingsApiTest(InvenTreeAPITestCase):
response = self.get(url, expected_code=200)
n_public_settings = len([k for k in InvenTreeSetting.SETTINGS.keys() if not k.startswith('_')])
# Number of results should match the number of settings
self.assertEqual(len(response.data), len(InvenTreeSetting.SETTINGS.keys()))
self.assertEqual(len(response.data), n_public_settings)
def test_company_name(self):
"""Test a settings object lifecycle e2e."""