Skip to content

Commit

Permalink
[feat:] Add --wait Flag to doctl databases migrate Command (#1591)
Browse files Browse the repository at this point in the history
* closes #1584 added --wait flag to migrate command

Signed-off-by: Akash <[email protected]>

* Update commands/databases_test.go

Co-authored-by: Anna Lushnikova <[email protected]>

---------

Signed-off-by: Akash <[email protected]>
Co-authored-by: Anna Lushnikova <[email protected]>
  • Loading branch information
SkySingh04 and loosla authored Oct 15, 2024
1 parent 0558237 commit bbe6124
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
28 changes: 27 additions & 1 deletion commands/databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <name>", "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())
Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 13 additions & 0 deletions commands/databases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit bbe6124

Please sign in to comment.