add an option --include-multibyte-charset

This commit is contained in:
ceshihao 2018-01-08 23:43:10 +08:00 committed by dyzheng
parent 35becabf3c
commit 346138772c
9 changed files with 24 additions and 7 deletions

View File

@ -91,6 +91,7 @@ type MigrationContext struct {
SkipRenamedColumns bool SkipRenamedColumns bool
IsTungsten bool IsTungsten bool
DiscardForeignKeys bool DiscardForeignKeys bool
IncludeMultibyteCharset bool
config ContextConfig config ContextConfig
configMutex *sync.Mutex configMutex *sync.Mutex

View File

@ -121,6 +121,7 @@ func main() {
version := flag.Bool("version", false, "Print version & exit") version := flag.Bool("version", false, "Print version & exit")
checkFlag := flag.Bool("check-flag", false, "Check if another flag exists/supported. This allows for cross-version scripting. Exits with 0 when all additional provided flags exist, nonzero otherwise. You must provide (dummy) values for flags that require a value. Example: gh-ost --check-flag --cut-over-lock-timeout-seconds --nice-ratio 0") checkFlag := flag.Bool("check-flag", false, "Check if another flag exists/supported. This allows for cross-version scripting. Exits with 0 when all additional provided flags exist, nonzero otherwise. You must provide (dummy) values for flags that require a value. Example: gh-ost --check-flag --cut-over-lock-timeout-seconds --nice-ratio 0")
flag.StringVar(&migrationContext.ForceTmpTableName, "force-table-names", "", "table name prefix to be used on the temporary tables") flag.StringVar(&migrationContext.ForceTmpTableName, "force-table-names", "", "table name prefix to be used on the temporary tables")
flag.BoolVar(&migrationContext.IncludeMultibyteCharset, "include-multibyte-charset", false, "charset includes multibyte encoding, e.g. gbk, gb2312, big5, cp932, sjis")
flag.CommandLine.SetOutput(os.Stdout) flag.CommandLine.SetOutput(os.Stdout)
flag.Parse() flag.Parse()

View File

@ -69,7 +69,7 @@ func NewApplier(migrationContext *base.MigrationContext) *Applier {
func (this *Applier) InitDBConnections() (err error) { func (this *Applier) InitDBConnections() (err error) {
applierUri := this.connectionConfig.GetDBUri(this.migrationContext.DatabaseName) applierUri := this.connectionConfig.GetDBUri(this.migrationContext.DatabaseName, this.migrationContext.IncludeMultibyteCharset)
if this.db, _, err = mysql.GetDB(this.migrationContext.Uuid, applierUri); err != nil { if this.db, _, err = mysql.GetDB(this.migrationContext.Uuid, applierUri); err != nil {
return err return err
} }

View File

@ -40,7 +40,7 @@ func NewInspector(migrationContext *base.MigrationContext) *Inspector {
} }
func (this *Inspector) InitDBConnections() (err error) { func (this *Inspector) InitDBConnections() (err error) {
inspectorUri := this.connectionConfig.GetDBUri(this.migrationContext.DatabaseName) inspectorUri := this.connectionConfig.GetDBUri(this.migrationContext.DatabaseName, this.migrationContext.IncludeMultibyteCharset)
if this.db, _, err = mysql.GetDB(this.migrationContext.Uuid, inspectorUri); err != nil { if this.db, _, err = mysql.GetDB(this.migrationContext.Uuid, inspectorUri); err != nil {
return err return err
} }

View File

@ -103,7 +103,7 @@ func (this *EventsStreamer) notifyListeners(binlogEvent *binlog.BinlogDMLEvent)
} }
func (this *EventsStreamer) InitDBConnections() (err error) { func (this *EventsStreamer) InitDBConnections() (err error) {
EventsStreamerUri := this.connectionConfig.GetDBUri(this.migrationContext.DatabaseName) EventsStreamerUri := this.connectionConfig.GetDBUri(this.migrationContext.DatabaseName, this.migrationContext.IncludeMultibyteCharset)
if this.db, _, err = mysql.GetDB(this.migrationContext.Uuid, EventsStreamerUri); err != nil { if this.db, _, err = mysql.GetDB(this.migrationContext.Uuid, EventsStreamerUri); err != nil {
return err return err
} }

View File

@ -183,7 +183,7 @@ func (this *Throttler) collectControlReplicasLag() {
) )
readReplicaLag := func(connectionConfig *mysql.ConnectionConfig) (lag time.Duration, err error) { readReplicaLag := func(connectionConfig *mysql.ConnectionConfig) (lag time.Duration, err error) {
dbUri := connectionConfig.GetDBUri("information_schema") dbUri := connectionConfig.GetDBUri("information_schema", false)
var heartbeatValue string var heartbeatValue string
if db, _, err := mysql.GetDB(this.migrationContext.Uuid, dbUri); err != nil { if db, _, err := mysql.GetDB(this.migrationContext.Uuid, dbUri); err != nil {

View File

@ -49,12 +49,16 @@ func (this *ConnectionConfig) Equals(other *ConnectionConfig) bool {
return this.Key.Equals(&other.Key) || this.ImpliedKey.Equals(other.ImpliedKey) return this.Key.Equals(&other.Key) || this.ImpliedKey.Equals(other.ImpliedKey)
} }
func (this *ConnectionConfig) GetDBUri(databaseName string) string { func (this *ConnectionConfig) GetDBUri(databaseName string, includeMultibyte bool) string {
hostname := this.Key.Hostname hostname := this.Key.Hostname
var ip = net.ParseIP(hostname) var ip = net.ParseIP(hostname)
if (ip != nil) && (ip.To4() == nil) { if (ip != nil) && (ip.To4() == nil) {
// Wrap IPv6 literals in square brackets // Wrap IPv6 literals in square brackets
hostname = fmt.Sprintf("[%s]", hostname) hostname = fmt.Sprintf("[%s]", hostname)
} }
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?interpolateParams=true&autocommit=true&charset=utf8mb4,utf8,latin1", this.User, this.Password, hostname, this.Key.Port, databaseName) var multibyteCharset string
if includeMultibyte {
multibyteCharset = ",gbk,gb2312,big5,cp932,sjis"
}
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?interpolateParams=%t&autocommit=true&charset=utf8mb4,utf8,latin1%s", this.User, this.Password, hostname, this.Key.Port, databaseName, !includeMultibyte, multibyteCharset)
} }

View File

@ -55,3 +55,14 @@ func TestDuplicate(t *testing.T) {
test.S(t).ExpectEquals(dup.User, "gromit") test.S(t).ExpectEquals(dup.User, "gromit")
test.S(t).ExpectEquals(dup.Password, "penguin") test.S(t).ExpectEquals(dup.Password, "penguin")
} }
func TestGetDBUri(t *testing.T) {
c := NewConnectionConfig()
c.Key = InstanceKey{Hostname: "myhost", Port: 3306}
c.User = "gromit"
c.Password = "penguin"
uri := c.GetDBUri("test", true)
test.S(t).ExpectEquals(uri, "gromit:penguin@tcp(myhost:3306)/test?interpolateParams=false&autocommit=true&charset=utf8mb4,utf8,latin1,gbk,gb2312,big5,cp932,sjis")
uri = c.GetDBUri("test", false)
test.S(t).ExpectEquals(uri, "gromit:penguin@tcp(myhost:3306)/test?interpolateParams=true&autocommit=true&charset=utf8mb4,utf8,latin1")
}

View File

@ -75,7 +75,7 @@ func GetReplicationLag(informationSchemaDb *gosql.DB, connectionConfig *Connecti
} }
func GetMasterKeyFromSlaveStatus(connectionConfig *ConnectionConfig) (masterKey *InstanceKey, err error) { func GetMasterKeyFromSlaveStatus(connectionConfig *ConnectionConfig) (masterKey *InstanceKey, err error) {
currentUri := connectionConfig.GetDBUri("information_schema") currentUri := connectionConfig.GetDBUri("information_schema", false)
// This function is only called once, okay to not have a cached connection pool // This function is only called once, okay to not have a cached connection pool
db, err := gosql.Open("mysql", currentUri) db, err := gosql.Open("mysql", currentUri)
if err != nil { if err != nil {