Skip to content

Commit

Permalink
controllers: support migrate down for cloud-dir
Browse files Browse the repository at this point in the history
  • Loading branch information
giautm committed Apr 11, 2024
1 parent c5e2534 commit 62744a8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
28 changes: 24 additions & 4 deletions controllers/atlasmigration_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,15 @@ func (r *AtlasMigrationReconciler) Reconcile(ctx context.Context, req ctrl.Reque
// TODO(giautm): Create DevDB and run linter for new migration
// files before applying it to the target database.

if data.DevURL == "" {
// The user has not specified an URL for dev-db,
// spin up a dev-db and get the connection string.
data.DevURL, err = r.devDB.devURL(ctx, res, *data.URL)
if err != nil {
res.SetNotReady("GettingDevDB", err.Error())
return result(err)
}
}
// Create a working directory for the Atlas CLI
// The working directory contains the atlas.hcl config
// and the migrations directory (if any)
Expand All @@ -178,7 +187,7 @@ func (r *AtlasMigrationReconciler) Reconcile(ctx context.Context, req ctrl.Reque
}
defer wd.Close()
// Reconcile given resource
status, err := r.reconcile(ctx, wd.Path(), data.EnvName)
status, err := r.reconcile(ctx, wd.Path(), data.Cloud, data.EnvName)
if err != nil {
res.SetNotReady("Migrating", strings.TrimSpace(err.Error()))
r.recordErrEvent(res, err)
Expand Down Expand Up @@ -235,11 +244,13 @@ const (
)

// Reconcile the given AtlasMigration resource.
func (r *AtlasMigrationReconciler) reconcile(ctx context.Context, wd, envName string) (_ *dbv1alpha1.AtlasMigrationStatus, _ error) {
func (r *AtlasMigrationReconciler) reconcile(ctx context.Context, wd string, cloud *cloud, envName string) (_ *dbv1alpha1.AtlasMigrationStatus, _ error) {
log := ctrl.Log.WithName("atlas_migration.reconcile")
c, err := atlas.NewClient(wd, r.execPath)
if err != nil {
return nil, err
}
log.Info("reconciling migration", "env", envName)
// Check if there are any pending migration files
status, err := c.MigrateStatus(ctx, &atlas.MigrateStatusParams{Env: envName})
if err != nil {
Expand All @@ -253,17 +264,24 @@ func (r *AtlasMigrationReconciler) reconcile(ctx context.Context, wd, envName st
switch {
case len(status.Pending) == 0 && len(status.Applied) > 0 && len(status.Available) < len(status.Applied):
// Migration is downgraded
log.Info("downgrading migration detected", "allowed", allowedDowngraded)
if allowedDowngraded {
// The downgrade is allowed, apply the last migration version
last := status.Available[len(status.Available)-1]
run, err := c.MigrateDown(ctx, &atlas.MigrateDownParams{
log.Info("downgrading to the last available version", "version", last.Version)
params := &atlas.MigrateDownParams{
Env: envName,
ToVersion: last.Version,
Context: &atlas.DeployRunContext{
TriggerType: atlas.TriggerTypeKubernetes,
TriggerVersion: dbv1alpha1.VersionFromContext(ctx),
},
})
}
if cloud != nil && cloud.RemoteDir != nil {
// Use the latest tag of the remote directory
params.DirURL = fmt.Sprintf("atlas://%s", cloud.RemoteDir.Name)
}
run, err := c.MigrateDown(ctx, params)
if err != nil {
return nil, transient(err)
}
Expand All @@ -287,6 +305,7 @@ func (r *AtlasMigrationReconciler) reconcile(ctx context.Context, wd, envName st
// The downgrade is not allowed, fall through to default behavior: No action
fallthrough
case len(status.Pending) == 0:
log.Info("no pending migrations")
// No pending migrations
var lastApplied int64
if len(status.Applied) > 0 {
Expand All @@ -297,6 +316,7 @@ func (r *AtlasMigrationReconciler) reconcile(ctx context.Context, wd, envName st
LastAppliedVersion: status.Current,
}, nil
default:
log.Info("applying pending migrations", "count", len(status.Pending))
// There are pending migrations
// Execute Atlas CLI migrate command
report, err := c.MigrateApply(ctx, &atlas.MigrateApplyParams{
Expand Down
6 changes: 3 additions & 3 deletions controllers/atlasmigration_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ func TestReconcile_reconcile(t *testing.T) {
defer func() {
require.NoError(t, wd.Close())
}()
status, err := tt.r.reconcile(context.Background(), wd.Path(), "test")
status, err := tt.r.reconcile(context.Background(), wd.Path(), nil, "test")
require.NoError(t, err)
require.EqualValues(t, "20230412003626", status.LastAppliedVersion)
}
Expand Down Expand Up @@ -465,7 +465,7 @@ func TestReconcile_reconcile_upToDate(t *testing.T) {
defer func() {
require.NoError(t, wd.Close())
}()
status, err := tt.r.reconcile(context.Background(), wd.Path(), "test")
status, err := tt.r.reconcile(context.Background(), wd.Path(), nil, "test")
require.NoError(t, err)
require.EqualValues(t, "20230412003626", status.LastAppliedVersion)
}
Expand Down Expand Up @@ -495,7 +495,7 @@ func TestReconcile_reconcile_baseline(t *testing.T) {
defer func() {
require.NoError(t, wd.Close())
}()
status, err := tt.r.reconcile(context.Background(), wd.Path(), "test")
status, err := tt.r.reconcile(context.Background(), wd.Path(), nil, "test")
require.NoError(t, err)
require.EqualValues(t, "20230412003628", status.LastAppliedVersion)
cli, err := atlasexec.NewClient(wd.Path(), tt.r.execPath)
Expand Down

0 comments on commit 62744a8

Please sign in to comment.