From bbe6124e15738440ce964f9c3540098dbc6f6836 Mon Sep 17 00:00:00 2001 From: Sky Singh <114267538+SkySingh04@users.noreply.github.com> Date: Tue, 15 Oct 2024 21:14:27 +0530 Subject: [PATCH] [feat:] Add `--wait` Flag to doctl databases migrate Command (#1591) * closes #1584 added --wait flag to migrate command Signed-off-by: Akash * Update commands/databases_test.go Co-authored-by: Anna Lushnikova --------- Signed-off-by: Akash Co-authored-by: Anna Lushnikova --- commands/databases.go | 28 +++++++++++++++++++++++++++- commands/databases_test.go | 13 +++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/commands/databases.go b/commands/databases.go index be8061a69..a5d63a1d8 100644 --- a/commands/databases.go +++ b/commands/databases.go @@ -143,6 +143,7 @@ For PostgreSQL and MySQL clusters, you can also provide a disk size in MiB to sc aliasOpt("m")) AddStringFlag(cmdDatabaseMigrate, doctl.ArgRegionSlug, "", "", "The region to which the database cluster should be migrated, such as `sfo2` or `nyc3`.", requiredOpt()) AddStringFlag(cmdDatabaseMigrate, doctl.ArgPrivateNetworkUUID, "", "", "The UUID of a VPC network to create the database cluster in. The command uses the region's default VPC network if not specified.") + AddBoolFlag(cmdDatabaseMigrate, doctl.ArgCommandWait, "", false, "A boolean value that specifies whether to wait for the database migration to complete before returning control to the terminal.") cmdDatabaseFork := CmdBuilder(cmd, RunDatabaseFork, "fork ", "Create a new database cluster by forking an existing database cluster.", `Creates a new database cluster from an existing cluster. The forked database contains all of the data from the original database at the time the fork is created.`, Writer, aliasOpt("f")) AddStringFlag(cmdDatabaseFork, doctl.ArgDatabaseRestoreFromClusterID, "", "", "The ID of an existing database cluster from which the new database will be forked from", requiredOpt()) @@ -586,7 +587,32 @@ func RunDatabaseMigrate(c *CmdConfig) error { return err } - return c.Databases().Migrate(id, r) + dbs := c.Databases() + err = dbs.Migrate(id, r) + if err != nil { + return err + } + + wait, err := c.Doit.GetBool(c.NS, doctl.ArgCommandWait) + if err != nil { + return err + } + + if wait { + notice("Database migration is in progress, waiting for database to be online") + + err := waitForDatabaseReady(dbs, id) + if err != nil { + return fmt.Errorf( + "database couldn't enter the `online` state after migration: %v", + err, + ) + } + + notice("Database migrated successfully") + } + + return nil } func buildDatabaseMigrateRequestFromArgs(c *CmdConfig) (*godo.DatabaseMigrateRequest, error) { diff --git a/commands/databases_test.go b/commands/databases_test.go index 854cdef04..19995671e 100644 --- a/commands/databases_test.go +++ b/commands/databases_test.go @@ -603,6 +603,19 @@ func TestDatabaseMigrate(t *testing.T) { assert.NoError(t, err) }) + // Success with wait flag + withTestClient(t, func(config *CmdConfig, tm *tcMocks) { + tm.databases.EXPECT().Migrate(testDBCluster.ID, r).Return(nil) + tm.databases.EXPECT().Get(testDBCluster.ID).Return(&testDBCluster, nil).AnyTimes() // Polling for status + config.Args = append(config.Args, testDBCluster.ID) + config.Doit.Set(config.NS, doctl.ArgRegionSlug, testDBCluster.RegionSlug) + config.Doit.Set(config.NS, doctl.ArgPrivateNetworkUUID, testDBCluster.PrivateNetworkUUID) + config.Doit.Set(config.NS, doctl.ArgCommandWait, true) + + err := RunDatabaseMigrate(config) + assert.NoError(t, err) + }) + // Error withTestClient(t, func(config *CmdConfig, tm *tcMocks) { tm.databases.EXPECT().Migrate(testDBCluster.ID, r).Return(errTest)