Skip to content

Commit 48802fd

Browse files
authored
cmd/atlas/internal/cmdapi: support reporting plan error (#3293)
1 parent f5ef1da commit 48802fd

File tree

1 file changed

+47
-12
lines changed

1 file changed

+47
-12
lines changed

cmd/atlas/internal/cmdapi/migrate.go

+47-12
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ func migrateApplyRun(cmd *cobra.Command, args []string, flags migrateApplyFlags,
238238
}
239239
pending, err := ex.Pending(ctx)
240240
if err != nil && !errors.Is(err, migrate.ErrNoPendingFiles) {
241+
mr.RecordPlanError(cmd, flags, err.Error())
241242
return err
242243
}
243244
noPending := errors.Is(err, migrate.ErrNoPendingFiles)
@@ -464,6 +465,35 @@ func (r *MigrateReport) RecordTargetID(ctx context.Context) error {
464465
return nil
465466
}
466467

468+
// RecordPlanError records any errors that occurred during the planning phase. i.e., when calling to ex.Pending.
469+
func (r *MigrateReport) RecordPlanError(cmd *cobra.Command, flags migrateApplyFlags, planerr string) {
470+
if !r.CloudEnabled(cmd.Context()) {
471+
return
472+
}
473+
var ver string
474+
if rev, err := r.rrw.CurrentRevision(cmd.Context()); err == nil {
475+
ver = rev.Version
476+
}
477+
r.done(&cloudapi.ReportMigrationInput{
478+
ProjectName: r.env.config.cloud.Project,
479+
EnvName: r.env.Name,
480+
DirName: r.DirName(flags),
481+
AtlasVersion: operatorVersion(),
482+
Target: cloudapi.DeployedTargetInput{
483+
ID: r.id,
484+
Schema: r.client.URL.Schema,
485+
URL: r.client.URL.Redacted(),
486+
},
487+
StartTime: r.log.Start,
488+
EndTime: r.log.End,
489+
FromVersion: r.log.Current,
490+
ToVersion: r.log.Target,
491+
CurrentVersion: ver,
492+
Error: &planerr,
493+
Log: planerr,
494+
})
495+
}
496+
467497
// Done closes and flushes this report.
468498
func (r *MigrateReport) Done(cmd *cobra.Command, flags migrateApplyFlags) error {
469499
if !r.CloudEnabled(cmd.Context()) {
@@ -481,21 +511,10 @@ func (r *MigrateReport) Done(cmd *cobra.Command, flags migrateApplyFlags) error
481511
default:
482512
ver = rev.Version
483513
}
484-
dirName := flags.dirURL
485-
switch u, err := url.Parse(flags.dirURL); {
486-
case err != nil:
487-
// Local directories are reported as (dangling)
488-
// deployments without a directory.
489-
case u.Scheme == cmdmigrate.DirTypeFile:
490-
dirName = cloudapi.DefaultDirName
491-
// Directory slug.
492-
default:
493-
dirName = path.Join(u.Host, u.Path)
494-
}
495514
r.done(&cloudapi.ReportMigrationInput{
496515
ProjectName: r.env.config.cloud.Project,
497516
EnvName: r.env.Name,
498-
DirName: dirName,
517+
DirName: r.DirName(flags),
499518
AtlasVersion: operatorVersion(),
500519
Target: cloudapi.DeployedTargetInput{
501520
ID: r.id,
@@ -551,6 +570,22 @@ func (r *MigrateReport) Done(cmd *cobra.Command, flags migrateApplyFlags) error
551570
return err
552571
}
553572

573+
// DirName returns the directory name for the report.
574+
func (r *MigrateReport) DirName(flags migrateApplyFlags) string {
575+
dirName := flags.dirURL
576+
switch u, err := url.Parse(flags.dirURL); {
577+
case err != nil:
578+
// Local directories are reported as (dangling)
579+
// deployments without a directory.
580+
case u.Scheme == cmdmigrate.DirTypeFile:
581+
dirName = cloudapi.DefaultDirName
582+
// Directory slug.
583+
default:
584+
dirName = path.Join(u.Host, u.Path)
585+
}
586+
return dirName
587+
}
588+
554589
// CloudEnabled reports if cloud reporting is enabled.
555590
func (r *MigrateReport) CloudEnabled(ctx context.Context) bool {
556591
if r.env == nil || r.env.cloud == nil {

0 commit comments

Comments
 (0)