mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-16 20:15:44 +00:00
feat(backend): clearer behaviour on missing migrations (#9527)
* feat(backend): better warning on missing migrations * add debug info to some tasks * ensure db would even be accessed before raising concerns * add more markers * Add decorator to log flow * reduce calls * reduce fnc down
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
"""AppConfig for InvenTree app."""
|
||||
|
||||
import sys
|
||||
from importlib import import_module
|
||||
from pathlib import Path
|
||||
|
||||
@ -20,6 +21,7 @@ from common.settings import get_global_setting, set_global_setting
|
||||
from InvenTree.config import get_setting
|
||||
|
||||
logger = structlog.get_logger('inventree')
|
||||
MIGRATIONS_CHECK_DONE = False
|
||||
|
||||
|
||||
class InvenTreeConfig(AppConfig):
|
||||
@ -56,6 +58,9 @@ class InvenTreeConfig(AppConfig):
|
||||
return
|
||||
|
||||
if InvenTree.ready.canAppAccessDatabase() or settings.TESTING_ENV:
|
||||
# Ensure there are no open migrations
|
||||
self.ensure_migrations_done()
|
||||
|
||||
self.remove_obsolete_tasks()
|
||||
self.collect_tasks()
|
||||
self.start_background_tasks()
|
||||
@ -382,3 +387,14 @@ class InvenTreeConfig(AppConfig):
|
||||
from generic.states import storage
|
||||
|
||||
storage.collect()
|
||||
|
||||
def ensure_migrations_done(self=None):
|
||||
"""Ensures there are no open migrations, stop if inconsistent state."""
|
||||
global MIGRATIONS_CHECK_DONE
|
||||
if MIGRATIONS_CHECK_DONE:
|
||||
return
|
||||
|
||||
if not InvenTree.tasks.check_for_migrations():
|
||||
logger.error('INVE-W8: Database Migrations required')
|
||||
sys.exit(1)
|
||||
MIGRATIONS_CHECK_DONE = True
|
||||
|
@ -629,10 +629,12 @@ def get_migration_plan():
|
||||
|
||||
|
||||
@scheduled_task(ScheduledTask.DAILY)
|
||||
def check_for_migrations(force: bool = False, reload_registry: bool = True):
|
||||
def check_for_migrations(force: bool = False, reload_registry: bool = True) -> bool:
|
||||
"""Checks if migrations are needed.
|
||||
|
||||
If the setting auto_update is enabled we will start updating.
|
||||
|
||||
Returns bool indicating if migrations are up to date
|
||||
"""
|
||||
from plugin import registry
|
||||
|
||||
@ -654,14 +656,14 @@ def check_for_migrations(force: bool = False, reload_registry: bool = True):
|
||||
# Check if there are any open migrations
|
||||
if not plan:
|
||||
set_pending_migrations(0)
|
||||
return
|
||||
return True
|
||||
|
||||
set_pending_migrations(n)
|
||||
|
||||
# Test if auto-updates are enabled
|
||||
if not force and not get_setting('INVENTREE_AUTO_UPDATE', 'auto_update'):
|
||||
logger.info('Auto-update is disabled - skipping migrations')
|
||||
return
|
||||
return False
|
||||
|
||||
# Log open migrations
|
||||
for migration in plan:
|
||||
@ -696,6 +698,8 @@ def check_for_migrations(force: bool = False, reload_registry: bool = True):
|
||||
# are loaded fully in their new state.
|
||||
registry.reload_plugins(full_reload=True, force_reload=True, collect=True)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def email_user(user_id: int, subject: str, message: str) -> None:
|
||||
"""Send a message to a user."""
|
||||
|
@ -6,6 +6,7 @@ from django.db.utils import OperationalError, ProgrammingError
|
||||
import structlog
|
||||
|
||||
import InvenTree.ready
|
||||
from InvenTree.apps import InvenTreeConfig
|
||||
|
||||
logger = structlog.get_logger('inventree')
|
||||
|
||||
@ -28,6 +29,9 @@ class PartConfig(AppConfig):
|
||||
return
|
||||
|
||||
if InvenTree.ready.canAppAccessDatabase():
|
||||
# Ensure there are no open migrations
|
||||
InvenTreeConfig.ensure_migrations_done()
|
||||
|
||||
self.update_trackable_status()
|
||||
self.reset_part_pricing_flags()
|
||||
|
||||
|
Reference in New Issue
Block a user