2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-11-13 11:26:42 +00:00

improve docker dx with empty migration set (#10774)

* if we are in docker and empty - init db

* use structlog

* remove logging change

* reduce diff

* ignore in test mode

* add changes from review
This commit is contained in:
Matthias Mair
2025-11-06 00:42:34 +01:00
committed by GitHub
parent 2dfe6b5f41
commit 2fc7c7eb54
2 changed files with 20 additions and 2 deletions

View File

@@ -321,6 +321,18 @@ class InvenTreeConfig(AppConfig):
return return
if not InvenTree.tasks.check_for_migrations(): if not InvenTree.tasks.check_for_migrations():
logger.error('INVE-W8: Database Migrations required') # Detect if this an empty database - if so, start with a fresh migration
sys.exit(1) if (
settings.DOCKER
and not InvenTree.ready.isInTestMode()
and not InvenTree.ready.isRunningMigrations()
and InvenTree.tasks.get_migration_count() == 0
):
logger.warning(
'INVE-W8: Empty database detected - trying to run migrations'
)
InvenTree.tasks.check_for_migrations(force_run=True)
else:
logger.error('INVE-W8: Database Migrations required')
sys.exit(1)
MIGRATIONS_CHECK_DONE = True MIGRATIONS_CHECK_DONE = True

View File

@@ -661,6 +661,12 @@ def get_migration_plan():
return plan return plan
def get_migration_count():
"""Returns the number of all detected migrations."""
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
return executor.loader.applied_migrations
@tracer.start_as_current_span('check_for_migrations') @tracer.start_as_current_span('check_for_migrations')
@scheduled_task(ScheduledTask.DAILY) @scheduled_task(ScheduledTask.DAILY)
def check_for_migrations(force: bool = False, reload_registry: bool = True) -> bool: def check_for_migrations(force: bool = False, reload_registry: bool = True) -> bool: