From 814fab6f363bc5dba4456c2d06f3fe6c3626ff94 Mon Sep 17 00:00:00 2001 From: Leon Yang Date: Wed, 7 Jun 2023 14:31:15 +0800 Subject: [PATCH] Polish command `import mysql` (#106) ## 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. --- internal/cli/dataimport/start/mysql.go | 44 ++++++++++----------- internal/cli/dataimport/start/mysql_test.go | 8 ++-- internal/flag/flag.go | 3 +- 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/internal/cli/dataimport/start/mysql.go b/internal/cli/dataimport/start/mysql.go index 51aee120..671f94a3 100644 --- a/internal/cli/dataimport/start/mysql.go +++ b/internal/cli/dataimport/start/mysql.go @@ -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, } } @@ -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 --cluster-id --source-host --source-port --source-user --source-password --source-database --source-table --database --password + Start an import task in non-interactive mode (using the TiDB Serverless cluster default user '.root'): + $ %[1]s import start mysql --project-id --cluster-id --source-host --source-port --source-user --source-password --source-database --source-table --target-database --target-password - Start an import task with a specific user: - $ %[1]s import start mysql --project-id --cluster-id --source-host --source-port --source-user --source-password --source-database --source-table --database --password --user + Start an import task in non-interactive mode (using a specific user): + $ %[1]s import start mysql --project-id --cluster-id --source-host --source-port --source-user --source-password --source-database --source-table --target-database --target-password --target-user - Start an import task skipping create table: - $ %[1]s import start mysql --project-id --cluster-id --source-host --source-port --source-user --source-password --source-database --source-table --database --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 --cluster-id --source-host --source-port --source-user --source-password --source-database --source-table --target-database --target-password --skip-create-table `, config.CliName), PreRunE: func(cmd *cobra.Command, args []string) error { @@ -289,7 +289,7 @@ 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) } @@ -297,13 +297,13 @@ It depends on 'mysql' command-line tool, please make sure you have installed it 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) } @@ -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 '.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 '.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 } diff --git a/internal/cli/dataimport/start/mysql_test.go b/internal/cli/dataimport/start/mysql_test.go index 1c75d22d..d7c20205 100644 --- a/internal/cli/dataimport/start/mysql_test.go +++ b/internal/cli/dataimport/start/mysql_test.go @@ -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"), }, } @@ -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}, }, } @@ -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"}, }, } diff --git a/internal/flag/flag.go b/internal/flag/flag.go index 76f44f05..4db9fe19 100644 --- a/internal/flag/flag.go +++ b/internal/flag/flag.go @@ -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" @@ -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"