@@ -175,7 +175,12 @@ async def migrate_cmd(
175175 context : CliContext = ctx .obj
176176 dbdriver = await context .init_db (retry = 1 )
177177
178- await migrate_database (db = dbdriver , initialize = True , check = check , migration_number = migration_number )
178+ migrations = await detect_migration_to_run (db = dbdriver , migration_number = migration_number )
179+
180+ if check or not migrations :
181+ return
182+
183+ await migrate_database (db = dbdriver , migrations = migrations , initialize = True )
179184
180185 await dbdriver .close ()
181186
@@ -343,7 +348,7 @@ async def detect_migration_to_run(
343348) -> Sequence [GraphMigration | InternalSchemaMigration | ArbitraryMigration | MigrationWithRebase ]:
344349 """Return a sequence of migrations to apply to upgrade the database."""
345350 rprint ("Checking current state of the database" )
346- migrations : Sequence [GraphMigration | InternalSchemaMigration | ArbitraryMigration | MigrationWithRebase ] = []
351+ migrations : list [GraphMigration | InternalSchemaMigration | ArbitraryMigration | MigrationWithRebase ] = []
347352
348353 root_node = await get_root_node (db = db )
349354 if migration_number :
@@ -413,7 +418,7 @@ async def migrate_database(
413418async def rebase_and_migrate_branches (
414419 db : InfrahubDatabase ,
415420 migrations : Sequence [GraphMigration | InternalSchemaMigration | ArbitraryMigration | MigrationWithRebase ],
416- ) -> None :
421+ ) -> bool :
417422 """Only applies migrations that aim at rebasing branches."""
418423 branches = [b for b in await Branch .get_list (db = db ) if b .name not in [registry .default_branch , GLOBAL_BRANCH_NAME ]]
419424 rprint (f"Planning rebase and migrations for { len (branches )} branches: { ', ' .join ([b .name for b in branches ])} " )
@@ -428,6 +433,17 @@ async def rebase_and_migrate_branches(
428433 if validation_result .success :
429434 rprint (f"Migration: { migration .name } { SUCCESS_BADGE } " )
430435
436+ if not execution_result .success or (validation_result and not validation_result .success ):
437+ rprint (f"Migration: { migration .name } { FAILED_BADGE } " )
438+ for error in execution_result .errors :
439+ rprint (f" { error } " )
440+ if validation_result and not validation_result .success :
441+ for error in validation_result .errors :
442+ rprint (f" { error } " )
443+ return False
444+
445+ return True
446+
431447
432448async def initialize_internal_schema () -> None :
433449 registry .schema = SchemaManager ()
0 commit comments