From e7af8039063c2473caac4f73bc0cf62650915514 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mustafa=20Sencer=20=C3=96zcan?= <32759850+mustafasencer@users.noreply.github.com> Date: Wed, 25 Jun 2025 08:50:53 +0200 Subject: [PATCH] fix: migration cli exit on grpc errors when on non-interactive (#107163) --- .../datamigrations/to_unified_storage.go | 37 ++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/pkg/cmd/grafana-cli/commands/datamigrations/to_unified_storage.go b/pkg/cmd/grafana-cli/commands/datamigrations/to_unified_storage.go index 37110c5df7c..b499b2a8ec9 100644 --- a/pkg/cmd/grafana-cli/commands/datamigrations/to_unified_storage.go +++ b/pkg/cmd/grafana-cli/commands/datamigrations/to_unified_storage.go @@ -77,19 +77,19 @@ func ToUnifiedStorage(c utils.CommandLine, cfg *setting.Cfg, sqlStore db.DB) err sort.ProvideService(), ) - if c.Bool("non-interactive") { - client, err := newUnifiedClient(cfg, sqlStore) - if err != nil { - return err - } + client, err := newUnifiedClient(cfg, sqlStore) + if err != nil { + return err + } + if c.Bool("non-interactive") { opts.Store = client opts.BlobStore = client rsp, err := migrator.Migrate(ctx, opts) - if err != nil { - msg := fmt.Sprintf("Failed to migrate legacy resources: %+v", err) - return cli.Exit(msg, 1) + if exitErr := handleMigrationError(err, rsp); exitErr != nil { + return exitErr } + logger.Info("Migrated legacy resources successfully in", time.Since(start)) if rsp != nil { jj, _ := json.MarshalIndent(rsp, "", " ") @@ -154,11 +154,6 @@ func ToUnifiedStorage(c utils.CommandLine, cfg *setting.Cfg, sqlStore db.DB) err return err } if yes { - client, err := newUnifiedClient(cfg, sqlStore) - if err != nil { - return err - } - // Check the stats (eventually compare) req := &resourcepb.ResourceStatsRequest{ Namespace: opts.Namespace, @@ -243,3 +238,19 @@ func newParquetClient(file *os.File) (resourcepb.BulkStoreClient, error) { client := parquet.NewBulkResourceWriterClient(writer) return client, nil } + +func handleMigrationError(err error, rsp *resourcepb.BulkResponse) error { + if err != nil { + return cli.Exit(fmt.Sprintf("Failed to migrate legacy resources: %+v", err), 1) + } + + if rsp != nil && rsp.Error != nil { + msg := fmt.Sprintf("Failed to migrate legacy resources: %s", rsp.Error.Message) + if rsp.Error.Reason != "" { + msg += fmt.Sprintf(" (%s)", rsp.Error.Reason) + } + return cli.Exit(msg, 1) + } + + return nil +}