mirror of
https://github.com/inventree/InvenTree.git
synced 2025-11-30 09:20:03 +00:00
[bug] Migration test fix (#10899)
* Pop instead of get * Better error handling for unit registry * Prevent caching during database migrations * Remove debug msg * Revert changes
This commit is contained in:
@@ -24,6 +24,22 @@ logger = structlog.get_logger('inventree')
|
|||||||
logging.getLogger('pint').setLevel(logging.ERROR)
|
logging.getLogger('pint').setLevel(logging.ERROR)
|
||||||
|
|
||||||
|
|
||||||
|
def can_cache_registry() -> bool:
|
||||||
|
"""Return True if it is appropriate to cache the unit registry.
|
||||||
|
|
||||||
|
Prevent caching under certain conditions (such as database migration)
|
||||||
|
to prevent database access.
|
||||||
|
"""
|
||||||
|
import InvenTree.ready
|
||||||
|
|
||||||
|
return not any([
|
||||||
|
InvenTree.ready.isImportingData(),
|
||||||
|
InvenTree.ready.isRunningBackup(),
|
||||||
|
InvenTree.ready.isRunningMigrations(),
|
||||||
|
InvenTree.ready.isInTestMode(),
|
||||||
|
])
|
||||||
|
|
||||||
|
|
||||||
def get_unit_registry_hash():
|
def get_unit_registry_hash():
|
||||||
"""Return a hash representing the current state of the unit registry.
|
"""Return a hash representing the current state of the unit registry.
|
||||||
|
|
||||||
@@ -53,6 +69,9 @@ def set_unit_registry_hash(registry_hash: str):
|
|||||||
global _unit_registry_hash
|
global _unit_registry_hash
|
||||||
_unit_registry_hash = registry_hash
|
_unit_registry_hash = registry_hash
|
||||||
|
|
||||||
|
if not can_cache_registry():
|
||||||
|
return
|
||||||
|
|
||||||
# Save to both the global settings and the session cache
|
# Save to both the global settings and the session cache
|
||||||
set_global_setting('_UNIT_REGISTRY_HASH', registry_hash)
|
set_global_setting('_UNIT_REGISTRY_HASH', registry_hash)
|
||||||
set_session_cache(_UNIT_REG_CACHE_KEY, registry_hash)
|
set_session_cache(_UNIT_REG_CACHE_KEY, registry_hash)
|
||||||
@@ -68,7 +87,7 @@ def get_unit_registry():
|
|||||||
return reload_unit_registry()
|
return reload_unit_registry()
|
||||||
|
|
||||||
# Check if the unit registry has changed
|
# Check if the unit registry has changed
|
||||||
if _unit_registry_hash != get_unit_registry_hash():
|
if can_cache_registry() and _unit_registry_hash != get_unit_registry_hash():
|
||||||
logger.info('Unit registry hash has changed, reloading unit registry')
|
logger.info('Unit registry hash has changed, reloading unit registry')
|
||||||
return reload_unit_registry()
|
return reload_unit_registry()
|
||||||
|
|
||||||
@@ -103,13 +122,18 @@ def reload_unit_registry():
|
|||||||
reg.define('thousand = 1000')
|
reg.define('thousand = 1000')
|
||||||
|
|
||||||
# Allow for custom units to be defined in the database
|
# Allow for custom units to be defined in the database
|
||||||
try:
|
|
||||||
from common.models import CustomUnit
|
|
||||||
|
|
||||||
# Calculate a hash of all custom units
|
# Calculate a hash of all custom units
|
||||||
hash_md5 = md5()
|
hash_md5 = md5()
|
||||||
|
|
||||||
for cu in CustomUnit.objects.all():
|
try:
|
||||||
|
from common.models import CustomUnit
|
||||||
|
|
||||||
|
custom_units = list(CustomUnit.objects.all())
|
||||||
|
except Exception:
|
||||||
|
# Database is likely not ready
|
||||||
|
custom_units = []
|
||||||
|
|
||||||
|
for cu in custom_units:
|
||||||
try:
|
try:
|
||||||
fmt = cu.fmt_string()
|
fmt = cu.fmt_string()
|
||||||
reg.define(fmt)
|
reg.define(fmt)
|
||||||
@@ -117,9 +141,7 @@ def reload_unit_registry():
|
|||||||
hash_md5.update(fmt.encode('utf-8'))
|
hash_md5.update(fmt.encode('utf-8'))
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.exception(
|
logger.exception('Failed to load custom unit: %s - %s', cu.fmt_string(), e)
|
||||||
'Failed to load custom unit: %s - %s', cu.fmt_string(), e
|
|
||||||
)
|
|
||||||
|
|
||||||
# Once custom units are loaded, save registry
|
# Once custom units are loaded, save registry
|
||||||
_unit_registry = reg
|
_unit_registry = reg
|
||||||
@@ -127,10 +149,6 @@ def reload_unit_registry():
|
|||||||
# Update the unit registry hash
|
# Update the unit registry hash
|
||||||
set_unit_registry_hash(hash_md5.hexdigest())
|
set_unit_registry_hash(hash_md5.hexdigest())
|
||||||
|
|
||||||
except Exception:
|
|
||||||
# Database is not ready, or CustomUnit model is not available
|
|
||||||
pass
|
|
||||||
|
|
||||||
dt = time.time() - t_start
|
dt = time.time() - t_start
|
||||||
logger.debug('Loaded unit registry in %.3f s', dt)
|
logger.debug('Loaded unit registry in %.3f s', dt)
|
||||||
|
|
||||||
|
|||||||
@@ -704,7 +704,7 @@ class BaseInvenTreeSetting(models.Model):
|
|||||||
): # pragma: no cover
|
): # pragma: no cover
|
||||||
return
|
return
|
||||||
|
|
||||||
attempts = int(kwargs.get('attempts', 3))
|
attempts = int(kwargs.pop('attempts', 3))
|
||||||
|
|
||||||
filters = {
|
filters = {
|
||||||
'key__iexact': key,
|
'key__iexact': key,
|
||||||
|
|||||||
Reference in New Issue
Block a user