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

Polish command import mysql #106

Merged
merged 2 commits into from
Jun 7, 2023
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
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