diff --git a/controllers/atlasmigration_controller.go b/controllers/atlasmigration_controller.go index 1af381f6..feed578f 100644 --- a/controllers/atlasmigration_controller.go +++ b/controllers/atlasmigration_controller.go @@ -83,6 +83,7 @@ type ( URL *url.URL DevURL string Dir migrate.Dir + DirLatest migrate.Dir Cloud *cloud RevisionsSchema string Baseline string @@ -219,7 +220,7 @@ func (r *AtlasMigrationReconciler) readDirState(ctx context.Context, obj client. }, } if err := r.Get(ctx, client.ObjectKeyFromObject(secret), secret); err != nil { - return nil, err + return nil, client.IgnoreNotFound(err) } return extractDirFromSecret(secret) } @@ -330,9 +331,21 @@ func (r *AtlasMigrationReconciler) reconcile(ctx context.Context, wd *atlas.Work } // Atlas needs all versions to be present in the directory // to downgrade to a specific version. - if c := data.Cloud; c != nil && c.RemoteDir != nil { + switch { + case data.Cloud != nil && data.Cloud.RemoteDir != nil: // Use the `latest` tag of the remote directory to fetch all versions. - params.DirURL = fmt.Sprintf("atlas://%s", c.RemoteDir.Name) + params.DirURL = fmt.Sprintf("atlas://%s", data.Cloud.RemoteDir.Name) + case data.DirLatest != nil: + // Copy the dir-state from latest deployment to the different location + // (to avoid the conflict with the current migration directory) + // then use it to downgrade. + current := fmt.Sprintf("migrations-%s", status.Current) + if err = wd.CopyFS(current, data.DirLatest); err != nil { + return nil, err + } + params.DirURL = fmt.Sprintf("file://%s", current) + default: + return nil, fmt.Errorf("unable to downgrade, no dir-state found") } run, err := c.MigrateDown(ctx, params) if err != nil { @@ -439,11 +452,19 @@ func (r *AtlasMigrationReconciler) extractData(ctx context.Context, res *dbv1alp if err != nil { return nil, err } + data.DirLatest, err = r.readDirState(ctx, res) + if err != nil { + return nil, err + } case d.Local != nil: data.Dir, err = memDir(d.Local) if err != nil { return nil, err } + data.DirLatest, err = r.readDirState(ctx, res) + if err != nil { + return nil, err + } default: return nil, errors.New("no directory specified") }