Skip to content

Commit

Permalink
Polish command import mysql (#106)
Browse files Browse the repository at this point in the history
## What is the purpose of the change

Polish command `import mysql`

## Brief change log

- Rename user to targetUser.
- Rename password to targetPassword
- Rename database to targetDatabase.
  • Loading branch information
xuanyu66 authored Jun 7, 2023
1 parent 4a27eea commit 814fab6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
44 changes: 22 additions & 22 deletions internal/cli/dataimport/start/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ func (c MySQLOpts) NonInteractiveFlags() []string {
return []string{
flag.ClusterID,
flag.ProjectID,
flag.Database,
flag.TargetDatabase,
flag.SourceHost,
flag.SourcePort,
flag.SourceDatabase,
flag.SourceTable,
flag.SourceUser,
flag.SourcePassword,
flag.Password,
flag.TargetPassword,
}
}

Expand All @@ -94,14 +94,14 @@ It depends on 'mysql' command-line tool, please make sure you have installed it
Example: fmt.Sprintf(` Start an import task in interactive mode:
$ %[1]s import start mysql
Start an import task in non-interactive mode:
$ %[1]s import start mysql --project-id <project-id> --cluster-id <cluster-id> --source-host <source-host> --source-port <source-port> --source-user <source-user> --source-password <source-password> --source-database <source-database> --source-table <source-table> --database <database> --password <password>
Start an import task in non-interactive mode (using the TiDB Serverless cluster default user '<username-prefix>.root'):
$ %[1]s import start mysql --project-id <project-id> --cluster-id <cluster-id> --source-host <source-host> --source-port <source-port> --source-user <source-user> --source-password <source-password> --source-database <source-database> --source-table <source-table> --target-database <target-database> --target-password <target-password>
Start an import task with a specific user:
$ %[1]s import start mysql --project-id <project-id> --cluster-id <cluster-id> --source-host <source-host> --source-port <source-port> --source-user <source-user> --source-password <source-password> --source-database <source-database> --source-table <source-table> --database <database> --password <password> --user <user>
Start an import task in non-interactive mode (using a specific user):
$ %[1]s import start mysql --project-id <project-id> --cluster-id <cluster-id> --source-host <source-host> --source-port <source-port> --source-user <source-user> --source-password <source-password> --source-database <source-database> --source-table <source-table> --target-database <target-database> --target-password <target-password> --target-user <target-user>
Start an import task skipping create table:
$ %[1]s import start mysql --project-id <project-id> --cluster-id <cluster-id> --source-host <source-host> --source-port <source-port> --source-user <source-user> --source-password <source-password> --source-database <source-database> --source-table <source-table> --database <database> --password <password> --skip-create-table
Start an import task that skips creating the target table if it already exists in the target database:
$ %[1]s import start mysql --project-id <project-id> --cluster-id <cluster-id> --source-host <source-host> --source-port <source-port> --source-user <source-user> --source-password <source-password> --source-database <source-database> --source-table <source-table> --target-database <target-database> --target-password <target-password> --skip-create-table
`,
config.CliName),
PreRunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -289,21 +289,21 @@ It depends on 'mysql' command-line tool, please make sure you have installed it
if err != nil {
return errors.Trace(err)
}
password, err = cmd.Flags().GetString(flag.Password)
password, err = cmd.Flags().GetString(flag.TargetPassword)
if err != nil {
return errors.Trace(err)
}
skipCreateTable, err = cmd.Flags().GetBool(flag.SkipCreateTable)
if err != nil {
return errors.Trace(err)
}
databaseName, err = cmd.Flags().GetString(flag.Database)
databaseName, err = cmd.Flags().GetString(flag.TargetDatabase)
if err != nil {
return errors.Trace(err)
}

if cmd.Flags().Changed(flag.User) {
userName, err = cmd.Flags().GetString(flag.User)
if cmd.Flags().Changed(flag.TargetUser) {
userName, err = cmd.Flags().GetString(flag.TargetUser)
if err != nil {
return errors.Trace(err)
}
Expand Down Expand Up @@ -408,16 +408,16 @@ It depends on 'mysql' command-line tool, please make sure you have installed it

mysqlCmd.Flags().StringP(flag.ProjectID, flag.ProjectIDShort, "", "Project ID")
mysqlCmd.Flags().StringP(flag.ClusterID, flag.ClusterIDShort, "", "Cluster ID")
mysqlCmd.Flags().String(flag.SourceHost, "", "The host of the source MySQL")
mysqlCmd.Flags().String(flag.SourcePort, "", "The port of the source MySQL")
mysqlCmd.Flags().String(flag.SourceUser, "", "The user to login source MySQL")
mysqlCmd.Flags().String(flag.SourcePassword, "", "The password to login source MySQL")
mysqlCmd.Flags().String(flag.SourceDatabase, "", "The database of the source MySQL")
mysqlCmd.Flags().String(flag.SourceTable, "", "The table to dump")
mysqlCmd.Flags().String(flag.Database, "", "The target database")
mysqlCmd.Flags().String(flag.User, "", "The user to login serverless cluster, default is '<token>.root'")
mysqlCmd.Flags().Bool(flag.SkipCreateTable, false, "Skip create table step, default create table")
mysqlCmd.Flags().String(flag.Password, "", "The password to login serverless cluster")
mysqlCmd.Flags().String(flag.SourceHost, "", "The host of the source MySQL instance")
mysqlCmd.Flags().String(flag.SourcePort, "", "The port of the source MySQL instance")
mysqlCmd.Flags().String(flag.SourceUser, "", "The user to log in to the source MySQL instance")
mysqlCmd.Flags().String(flag.SourcePassword, "", "The password of the source MySQL instance")
mysqlCmd.Flags().String(flag.SourceDatabase, "", "The name of the source MySQL database")
mysqlCmd.Flags().String(flag.SourceTable, "", "The source table name in the source MySQL database")
mysqlCmd.Flags().String(flag.TargetDatabase, "", "The target database name in TiDB Serverless cluster")
mysqlCmd.Flags().String(flag.TargetUser, "", "The user to log in to the target TiDB Serverless cluster, default is '<token>.root'")
mysqlCmd.Flags().Bool(flag.SkipCreateTable, false, "Skip creating the target table if it already exists in the target database")
mysqlCmd.Flags().String(flag.TargetPassword, "", "The password of the target TiDB Serverless cluster")

return mysqlCmd
}
Expand Down
8 changes: 4 additions & 4 deletions internal/cli/dataimport/start/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,11 @@ func (suite *MySQLImportSuite) TestMySQLImportArgs() {
}{
{
name: "start import success",
args: []string{"--project-id", projectID, "--cluster-id", clusterID, "--source-host", sourceHost, "--source-port", sourcePort, "--source-database", sourceDatabase, "--source-table", sourceTable, "--source-user", sourceUser, "--source-password", sourcePassword, "--password", password, "--database", database},
args: []string{"--project-id", projectID, "--cluster-id", clusterID, "--source-host", sourceHost, "--source-port", sourcePort, "--source-database", sourceDatabase, "--source-table", sourceTable, "--source-user", sourceUser, "--source-password", sourcePassword, "--target-password", password, "--target-database", database},
},
{
name: "start import without required project-id flag",
args: []string{"--cluster-id", clusterID, "--source-host", sourceHost, "--source-port", sourcePort, "--source-database", sourceDatabase, "--source-table", sourceTable, "--source-user", sourceUser, "--source-password", sourcePassword, "--password", password, "--database", database},
args: []string{"--cluster-id", clusterID, "--source-host", sourceHost, "--source-port", sourcePort, "--source-database", sourceDatabase, "--source-table", sourceTable, "--source-user", sourceUser, "--source-password", sourcePassword, "--target-password", password, "--target-database", database},
err: fmt.Errorf("required flag(s) \"project-id\" not set"),
},
}
Expand Down Expand Up @@ -344,7 +344,7 @@ func (suite *MySQLImportSuite) TestMySQLImportWithoutCreateTable() {
}{
{
name: "start import success",
args: []string{"--project-id", projectID, "--cluster-id", clusterID, "--source-host", sourceHost, "--source-port", sourcePort, "--source-database", sourceDatabase, "--source-table", sourceTable, "--source-user", sourceUser, "--source-password", sourcePassword, "--password", password, "--database", database, "--user", targetUser},
args: []string{"--project-id", projectID, "--cluster-id", clusterID, "--source-host", sourceHost, "--source-port", sourcePort, "--source-database", sourceDatabase, "--source-table", sourceTable, "--source-user", sourceUser, "--source-password", sourcePassword, "--target-password", password, "--target-database", database, "--target-user", targetUser},
},
}

Expand Down Expand Up @@ -456,7 +456,7 @@ func (suite *MySQLImportSuite) TestMySQLImportWithSpecificUser() {
}{
{
name: "start import success",
args: []string{"--project-id", projectID, "--cluster-id", clusterID, "--source-host", sourceHost, "--source-port", sourcePort, "--source-database", sourceDatabase, "--source-table", sourceTable, "--source-user", sourceUser, "--source-password", sourcePassword, "--password", password, "--database", database, "--skip-create-table"},
args: []string{"--project-id", projectID, "--cluster-id", clusterID, "--source-host", sourceHost, "--source-port", sourcePort, "--source-database", sourceDatabase, "--source-table", sourceTable, "--source-user", sourceUser, "--source-password", sourcePassword, "--target-password", password, "--target-database", database, "--skip-create-table"},
},
}

Expand Down
3 changes: 2 additions & 1 deletion internal/flag/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const (
ClusterIDShort string = "c"
ClusterName string = "cluster-name"
ClusterType string = "cluster-type"
Database string = "database"
DataFormat string = "data-format"
Debug string = "debug"
DebugShort string = "D"
Expand Down Expand Up @@ -55,7 +54,9 @@ const (
SourceUser string = "source-user"
SourceUrl string = "source-url"
TargetDatabase string = "target-database"
TargetPassword string = "target-password"
TargetTable string = "target-table"
TargetUser string = "target-user"
TrimLastSeparator string = "trim-last-separator"
User string = "user"
UserShort string = "u"
Expand Down

0 comments on commit 814fab6

Please sign in to comment.