Skip to content

Commit

Permalink
atlasmigration: migrate down for local dir
Browse files Browse the repository at this point in the history
controllers: support local directory migrate down
  • Loading branch information
giautm committed Apr 25, 2024
1 parent 7dea498 commit 2d1f95b
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions controllers/atlasmigration_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ type (
URL *url.URL
DevURL string
Dir migrate.Dir
DirLatest migrate.Dir
Cloud *cloud
RevisionsSchema string
Baseline string
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
}
Expand Down

0 comments on commit 2d1f95b

Please sign in to comment.