Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat:] Add --wait Flag to doctl databases migrate Command #1591

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading