2016-04-04 10:27:51 +00:00
/ *
2022-02-23 22:28:03 +00:00
Copyright 2022 GitHub Inc .
2016-05-16 09:09:17 +00:00
See https : //github.com/github/gh-ost/blob/master/LICENSE
2016-04-04 10:27:51 +00:00
* /
package logic
import (
2022-07-06 22:23:23 +00:00
"context"
2016-04-04 10:27:51 +00:00
gosql "database/sql"
"fmt"
2016-10-20 09:29:30 +00:00
"reflect"
2016-04-04 10:27:51 +00:00
"strings"
2016-07-29 08:40:23 +00:00
"sync/atomic"
2016-11-02 11:48:35 +00:00
"time"
2016-04-04 10:27:51 +00:00
2016-05-16 09:09:17 +00:00
"github.com/github/gh-ost/go/base"
"github.com/github/gh-ost/go/mysql"
"github.com/github/gh-ost/go/sql"
2016-04-04 10:27:51 +00:00
2021-06-24 18:19:37 +00:00
"github.com/openark/golib/sqlutils"
2016-04-04 10:27:51 +00:00
)
2016-12-13 11:27:20 +00:00
const startSlavePostWaitMilliseconds = 500 * time . Millisecond
2016-04-04 10:27:51 +00:00
// Inspector reads data from the read-MySQL-server (typically a replica, but can be the master)
// It is used for gaining initial status and structure, and later also follow up on progress and changelog
type Inspector struct {
2017-09-22 23:06:06 +00:00
connectionConfig * mysql . ConnectionConfig
db * gosql . DB
informationSchemaDb * gosql . DB
migrationContext * base . MigrationContext
2021-02-08 12:36:59 +00:00
name string
2016-04-04 10:27:51 +00:00
}
2017-08-08 20:36:54 +00:00
func NewInspector ( migrationContext * base . MigrationContext ) * Inspector {
2016-04-04 10:27:51 +00:00
return & Inspector {
2017-08-08 20:36:54 +00:00
connectionConfig : migrationContext . InspectorConnectionConfig ,
migrationContext : migrationContext ,
2021-02-08 12:36:59 +00:00
name : "inspector" ,
2016-04-04 10:27:51 +00:00
}
}
func ( this * Inspector ) InitDBConnections ( ) ( err error ) {
2016-04-04 13:29:02 +00:00
inspectorUri := this . connectionConfig . GetDBUri ( this . migrationContext . DatabaseName )
2017-12-14 23:53:29 +00:00
if this . db , _ , err = mysql . GetDB ( this . migrationContext . Uuid , inspectorUri ) ; err != nil {
2016-04-04 10:27:51 +00:00
return err
}
2017-09-22 23:06:06 +00:00
informationSchemaUri := this . connectionConfig . GetDBUri ( "information_schema" )
2017-12-14 23:53:29 +00:00
if this . informationSchemaDb , _ , err = mysql . GetDB ( this . migrationContext . Uuid , informationSchemaUri ) ; err != nil {
2017-09-22 23:06:06 +00:00
return err
}
2016-04-04 10:27:51 +00:00
if err := this . validateConnection ( ) ; err != nil {
return err
}
2019-12-17 03:47:14 +00:00
if ! this . migrationContext . AliyunRDS && ! this . migrationContext . GoogleCloudPlatform && ! this . migrationContext . AzureMySQL {
2018-03-29 03:19:20 +00:00
if impliedKey , err := mysql . GetInstanceKey ( this . db ) ; err != nil {
return err
} else {
2018-01-23 05:51:41 +00:00
this . connectionConfig . ImpliedKey = impliedKey
}
2016-06-19 15:55:37 +00:00
}
2016-04-04 10:27:51 +00:00
if err := this . validateGrants ( ) ; err != nil {
return err
}
2016-04-18 17:57:18 +00:00
if err := this . validateBinlogs ( ) ; err != nil {
2016-04-11 15:27:16 +00:00
return err
}
2016-04-18 17:57:18 +00:00
if err := this . applyBinlogFormat ( ) ; err != nil {
2016-04-04 10:27:51 +00:00
return err
}
2019-10-07 15:10:36 +00:00
this . migrationContext . Log . Infof ( "Inspector initiated on %+v, version %+v" , this . connectionConfig . ImpliedKey , this . migrationContext . InspectorMySQLVersion )
2016-04-07 13:57:12 +00:00
return nil
}
func ( this * Inspector ) ValidateOriginalTable ( ) ( err error ) {
2016-04-04 10:27:51 +00:00
if err := this . validateTable ( ) ; err != nil {
return err
}
2016-10-10 10:28:56 +00:00
if err := this . validateTableForeignKeys ( this . migrationContext . DiscardForeignKeys ) ; err != nil {
2016-04-04 16:19:46 +00:00
return err
}
2016-08-11 12:10:35 +00:00
if err := this . validateTableTriggers ( ) ; err != nil {
return err
}
2016-06-06 10:33:05 +00:00
if err := this . estimateTableRowsViaExplain ( ) ; err != nil {
return err
2016-04-04 10:27:51 +00:00
}
return nil
}
2018-05-22 09:36:52 +00:00
func ( this * Inspector ) InspectTableColumnsAndUniqueKeys ( tableName string ) ( columns * sql . ColumnList , virtualColumns * sql . ColumnList , uniqueKeys [ ] ( * sql . UniqueKey ) , err error ) {
2016-04-08 12:35:06 +00:00
uniqueKeys , err = this . getCandidateUniqueKeys ( tableName )
2016-04-04 10:27:51 +00:00
if err != nil {
2018-05-22 09:36:52 +00:00
return columns , virtualColumns , uniqueKeys , err
2016-04-04 10:27:51 +00:00
}
if len ( uniqueKeys ) == 0 {
2018-05-22 09:36:52 +00:00
return columns , virtualColumns , uniqueKeys , fmt . Errorf ( "No PRIMARY nor UNIQUE key found in table! Bailing out" )
2016-04-08 12:35:06 +00:00
}
2018-05-22 09:36:52 +00:00
columns , virtualColumns , err = mysql . GetTableColumns ( this . db , this . migrationContext . DatabaseName , tableName )
2016-04-08 12:35:06 +00:00
if err != nil {
2018-05-22 09:36:52 +00:00
return columns , virtualColumns , uniqueKeys , err
2016-04-08 12:35:06 +00:00
}
2018-05-22 09:36:52 +00:00
return columns , virtualColumns , uniqueKeys , nil
2016-04-08 12:35:06 +00:00
}
func ( this * Inspector ) InspectOriginalTable ( ) ( err error ) {
2018-05-22 09:36:52 +00:00
this . migrationContext . OriginalTableColumns , this . migrationContext . OriginalTableVirtualColumns , this . migrationContext . OriginalTableUniqueKeys , err = this . InspectTableColumnsAndUniqueKeys ( this . migrationContext . OriginalTableName )
2016-12-11 01:19:19 +00:00
if err != nil {
2016-04-08 12:35:06 +00:00
return err
}
2021-05-14 13:32:56 +00:00
this . migrationContext . OriginalTableAutoIncrement , err = this . getAutoIncrementValue ( this . migrationContext . OriginalTableName )
if err != nil {
return err
}
2016-04-08 12:35:06 +00:00
return nil
}
2016-10-20 09:29:30 +00:00
// inspectOriginalAndGhostTables compares original and ghost tables to see whether the migration
2016-06-06 10:33:05 +00:00
// makes sense and is valid. It extracts the list of shared columns and the chosen migration unique key
2016-10-20 09:29:30 +00:00
func ( this * Inspector ) inspectOriginalAndGhostTables ( ) ( err error ) {
originalNamesOnApplier := this . migrationContext . OriginalTableColumnsOnApplier . Names ( )
originalNames := this . migrationContext . OriginalTableColumns . Names ( )
if ! reflect . DeepEqual ( originalNames , originalNamesOnApplier ) {
return fmt . Errorf ( "It seems like table structure is not identical between master and replica. This scenario is not supported." )
}
2018-05-22 09:36:52 +00:00
this . migrationContext . GhostTableColumns , this . migrationContext . GhostTableVirtualColumns , this . migrationContext . GhostTableUniqueKeys , err = this . InspectTableColumnsAndUniqueKeys ( this . migrationContext . GetGhostTableName ( ) )
2016-04-08 12:35:06 +00:00
if err != nil {
return err
}
2022-09-06 13:23:18 +00:00
sharedUniqueKeys := this . getSharedUniqueKeys ( this . migrationContext . OriginalTableUniqueKeys , this . migrationContext . GhostTableUniqueKeys )
2017-09-03 06:57:26 +00:00
for i , sharedUniqueKey := range sharedUniqueKeys {
this . applyColumnTypes ( this . migrationContext . DatabaseName , this . migrationContext . OriginalTableName , & sharedUniqueKey . Columns )
uniqueKeyIsValid := true
for _ , column := range sharedUniqueKey . Columns . Columns ( ) {
switch column . Type {
case sql . FloatColumnType :
{
2019-10-07 15:10:36 +00:00
this . migrationContext . Log . Warning ( "Will not use %+v as shared key due to FLOAT data type" , sharedUniqueKey . Name )
2017-09-03 06:57:26 +00:00
uniqueKeyIsValid = false
}
case sql . JSONColumnType :
{
// Noteworthy that at this time MySQL does not allow JSON indexing anyhow, but this code
// will remain in place to potentially handle the future case where JSON is supported in indexes.
2019-10-07 15:10:36 +00:00
this . migrationContext . Log . Warning ( "Will not use %+v as shared key due to JSON data type" , sharedUniqueKey . Name )
2017-09-03 06:57:26 +00:00
uniqueKeyIsValid = false
}
}
}
if uniqueKeyIsValid {
this . migrationContext . UniqueKey = sharedUniqueKeys [ i ]
break
}
}
if this . migrationContext . UniqueKey == nil {
2016-04-08 12:35:06 +00:00
return fmt . Errorf ( "No shared unique key can be found after ALTER! Bailing out" )
2016-04-04 10:27:51 +00:00
}
2019-10-07 15:10:36 +00:00
this . migrationContext . Log . Infof ( "Chosen shared unique key is %s" , this . migrationContext . UniqueKey . Name )
2016-05-20 10:52:14 +00:00
if this . migrationContext . UniqueKey . HasNullable {
if this . migrationContext . NullableUniqueKeyAllowed {
2019-10-07 15:10:36 +00:00
this . migrationContext . Log . Warningf ( "Chosen key (%s) has nullable columns. You have supplied with --allow-nullable-unique-key and so this migration proceeds. As long as there aren't NULL values in this key's column, migration should be fine. NULL values will corrupt migration's data" , this . migrationContext . UniqueKey )
2016-05-20 10:52:14 +00:00
} else {
return fmt . Errorf ( "Chosen key (%s) has nullable columns. Bailing out. To force this operation to continue, supply --allow-nullable-unique-key flag. Only do so if you are certain there are no actual NULL values in this key. As long as there aren't, migration should be fine. NULL values in columns of this key will corrupt migration's data" , this . migrationContext . UniqueKey )
}
}
2016-04-08 12:35:06 +00:00
2018-05-22 09:36:52 +00:00
this . migrationContext . SharedColumns , this . migrationContext . MappedSharedColumns = this . getSharedColumns ( this . migrationContext . OriginalTableColumns , this . migrationContext . GhostTableColumns , this . migrationContext . OriginalTableVirtualColumns , this . migrationContext . GhostTableVirtualColumns , this . migrationContext . ColumnRenameMap )
2019-10-07 15:10:36 +00:00
this . migrationContext . Log . Infof ( "Shared columns are %s" , this . migrationContext . SharedColumns )
2016-04-08 12:35:06 +00:00
// By fact that a non-empty unique key exists we also know the shared columns are non-empty
2016-08-17 04:51:58 +00:00
// This additional step looks at which columns are unsigned. We could have merged this within
// the `getTableColumns()` function, but it's a later patch and introduces some complexity; I feel
// comfortable in doing this as a separate step.
2018-11-01 13:43:57 +00:00
this . applyColumnTypes ( this . migrationContext . DatabaseName , this . migrationContext . OriginalTableName , this . migrationContext . OriginalTableColumns , this . migrationContext . SharedColumns , & this . migrationContext . UniqueKey . Columns )
2016-09-07 12:24:11 +00:00
this . applyColumnTypes ( this . migrationContext . DatabaseName , this . migrationContext . GetGhostTableName ( ) , this . migrationContext . GhostTableColumns , this . migrationContext . MappedSharedColumns )
2016-08-17 04:51:58 +00:00
2016-10-13 11:09:58 +00:00
for i := range this . migrationContext . SharedColumns . Columns ( ) {
column := this . migrationContext . SharedColumns . Columns ( ) [ i ]
mappedColumn := this . migrationContext . MappedSharedColumns . Columns ( ) [ i ]
if column . Name == mappedColumn . Name && column . Type == sql . DateTimeColumnType && mappedColumn . Type == sql . TimestampColumnType {
this . migrationContext . MappedSharedColumns . SetConvertDatetimeToTimestamp ( column . Name , this . migrationContext . ApplierTimeZone )
}
2021-06-10 15:17:49 +00:00
if column . Name == mappedColumn . Name && column . Type == sql . EnumColumnType && mappedColumn . Charset != "" {
this . migrationContext . MappedSharedColumns . SetEnumToTextConversion ( column . Name )
this . migrationContext . MappedSharedColumns . SetEnumValues ( column . Name , column . EnumValues )
}
2022-08-27 01:55:36 +00:00
if column . Name == mappedColumn . Name && column . Charset != mappedColumn . Charset {
this . migrationContext . SharedColumns . SetCharsetConversion ( column . Name , column . Charset , mappedColumn . Charset )
}
2016-10-13 11:09:58 +00:00
}
2016-10-14 07:33:53 +00:00
for _ , column := range this . migrationContext . UniqueKey . Columns . Columns ( ) {
2021-05-24 18:16:49 +00:00
if this . migrationContext . GhostTableVirtualColumns . GetColumn ( column . Name ) != nil {
// this is a virtual column
continue
}
2016-10-14 07:33:53 +00:00
if this . migrationContext . MappedSharedColumns . HasTimezoneConversion ( column . Name ) {
return fmt . Errorf ( "No support at this time for converting a column from DATETIME to TIMESTAMP that is also part of the chosen unique key. Column: %s, key: %s" , column . Name , this . migrationContext . UniqueKey . Name )
}
}
2016-04-08 12:35:06 +00:00
return nil
2016-04-04 10:27:51 +00:00
}
// validateConnection issues a simple can-connect to MySQL
func ( this * Inspector ) validateConnection ( ) error {
2017-09-03 07:27:04 +00:00
if len ( this . connectionConfig . Password ) > mysql . MaxReplicationPasswordLength {
return fmt . Errorf ( "MySQL replication length limited to 32 characters. See https://dev.mysql.com/doc/refman/5.7/en/assigning-passwords.html" )
}
2017-09-06 18:25:35 +00:00
2021-02-08 12:35:30 +00:00
version , err := base . ValidateConnection ( this . db , this . connectionConfig , this . migrationContext , this . name )
2017-09-06 18:25:35 +00:00
this . migrationContext . InspectorMySQLVersion = version
return err
2016-04-04 10:27:51 +00:00
}
// validateGrants verifies the user by which we're executing has necessary grants
2017-11-08 01:02:32 +00:00
// to do its thing.
2016-04-04 10:27:51 +00:00
func ( this * Inspector ) validateGrants ( ) error {
2016-05-16 09:09:17 +00:00
query := ` show /* gh-ost */ grants for current_user() `
2016-04-04 10:27:51 +00:00
foundAll := false
foundSuper := false
2016-08-12 12:26:58 +00:00
foundReplicationClient := false
2016-04-04 10:27:51 +00:00
foundReplicationSlave := false
foundDBAll := false
err := sqlutils . QueryRowsMap ( this . db , query , func ( rowMap sqlutils . RowMap ) error {
for _ , grantData := range rowMap {
grant := grantData . String
if strings . Contains ( grant , ` GRANT ALL PRIVILEGES ON *.* ` ) {
foundAll = true
}
if strings . Contains ( grant , ` SUPER ` ) && strings . Contains ( grant , ` ON *.* ` ) {
foundSuper = true
}
2016-08-12 12:26:58 +00:00
if strings . Contains ( grant , ` REPLICATION CLIENT ` ) && strings . Contains ( grant , ` ON *.* ` ) {
foundReplicationClient = true
}
2016-04-04 10:27:51 +00:00
if strings . Contains ( grant , ` REPLICATION SLAVE ` ) && strings . Contains ( grant , ` ON *.* ` ) {
foundReplicationSlave = true
}
if strings . Contains ( grant , fmt . Sprintf ( "GRANT ALL PRIVILEGES ON `%s`.*" , this . migrationContext . DatabaseName ) ) {
foundDBAll = true
}
2018-08-30 16:40:09 +00:00
if strings . Contains ( grant , fmt . Sprintf ( "GRANT ALL PRIVILEGES ON `%s`.*" , strings . Replace ( this . migrationContext . DatabaseName , "_" , "\\_" , - 1 ) ) ) {
foundDBAll = true
}
2016-06-17 06:03:18 +00:00
if base . StringContainsAll ( grant , ` ALTER ` , ` CREATE ` , ` DELETE ` , ` DROP ` , ` INDEX ` , ` INSERT ` , ` LOCK TABLES ` , ` SELECT ` , ` TRIGGER ` , ` UPDATE ` , ` ON *.* ` ) {
2016-06-16 14:06:26 +00:00
foundDBAll = true
}
2016-06-17 06:03:18 +00:00
if base . StringContainsAll ( grant , ` ALTER ` , ` CREATE ` , ` DELETE ` , ` DROP ` , ` INDEX ` , ` INSERT ` , ` LOCK TABLES ` , ` SELECT ` , ` TRIGGER ` , ` UPDATE ` , fmt . Sprintf ( " ON `%s`.*" , this . migrationContext . DatabaseName ) ) {
2016-06-16 14:06:26 +00:00
foundDBAll = true
}
2016-04-04 10:27:51 +00:00
}
return nil
} )
if err != nil {
2016-04-04 13:29:02 +00:00
return err
2016-04-04 10:27:51 +00:00
}
2016-08-12 12:26:58 +00:00
this . migrationContext . HasSuperPrivilege = foundSuper
2016-04-04 10:27:51 +00:00
if foundAll {
2019-10-07 15:10:36 +00:00
this . migrationContext . Log . Infof ( "User has ALL privileges" )
2016-04-04 10:27:51 +00:00
return nil
}
if foundSuper && foundReplicationSlave && foundDBAll {
2019-10-07 15:10:36 +00:00
this . migrationContext . Log . Infof ( "User has SUPER, REPLICATION SLAVE privileges, and has ALL privileges on %s.*" , sql . EscapeName ( this . migrationContext . DatabaseName ) )
2016-04-04 10:27:51 +00:00
return nil
}
2016-08-12 12:26:58 +00:00
if foundReplicationClient && foundReplicationSlave && foundDBAll {
2019-10-07 15:10:36 +00:00
this . migrationContext . Log . Infof ( "User has REPLICATION CLIENT, REPLICATION SLAVE privileges, and has ALL privileges on %s.*" , sql . EscapeName ( this . migrationContext . DatabaseName ) )
2016-08-12 12:26:58 +00:00
return nil
}
2019-10-07 15:10:36 +00:00
this . migrationContext . Log . Debugf ( "Privileges: Super: %t, REPLICATION CLIENT: %t, REPLICATION SLAVE: %t, ALL on *.*: %t, ALL on %s.*: %t" , foundSuper , foundReplicationClient , foundReplicationSlave , foundAll , sql . EscapeName ( this . migrationContext . DatabaseName ) , foundDBAll )
return this . migrationContext . Log . Errorf ( "User has insufficient privileges for migration. Needed: SUPER|REPLICATION CLIENT, REPLICATION SLAVE and ALL on %s.*" , sql . EscapeName ( this . migrationContext . DatabaseName ) )
2016-04-04 10:27:51 +00:00
}
2016-04-11 15:27:16 +00:00
// restartReplication is required so that we are _certain_ the binlog format and
// row image settings have actually been applied to the replication thread.
2017-11-08 00:47:36 +00:00
// It is entirely possible, for example, that the replication is using 'STATEMENT'
2016-04-11 15:27:16 +00:00
// binlog format even as the variable says 'ROW'
func ( this * Inspector ) restartReplication ( ) error {
2022-02-23 22:28:03 +00:00
this . migrationContext . Log . Infof ( "Restarting replication on %s to make sure binlog settings apply to replication thread" , this . connectionConfig . Key . String ( ) )
2016-04-11 15:27:16 +00:00
2016-05-01 18:36:36 +00:00
masterKey , _ := mysql . GetMasterKeyFromSlaveStatus ( this . connectionConfig )
2016-04-14 11:37:56 +00:00
if masterKey == nil {
// This is not a replica
return nil
}
2016-04-11 15:27:16 +00:00
var stopError , startError error
_ , stopError = sqlutils . ExecNoPrepare ( this . db , ` stop slave ` )
_ , startError = sqlutils . ExecNoPrepare ( this . db , ` start slave ` )
if stopError != nil {
return stopError
}
if startError != nil {
return startError
}
2016-12-13 11:27:20 +00:00
time . Sleep ( startSlavePostWaitMilliseconds )
2019-10-07 15:10:36 +00:00
this . migrationContext . Log . Debugf ( "Replication restarted" )
2016-04-11 15:27:16 +00:00
return nil
}
2016-04-18 17:57:18 +00:00
// applyBinlogFormat sets ROW binlog format and restarts replication to make
// the replication thread apply it.
func ( this * Inspector ) applyBinlogFormat ( ) error {
if this . migrationContext . RequiresBinlogFormatChange ( ) {
2016-08-12 12:26:58 +00:00
if ! this . migrationContext . SwitchToRowBinlogFormat {
return fmt . Errorf ( "Existing binlog_format is %s. Am not switching it to ROW unless you specify --switch-to-rbr" , this . migrationContext . OriginalBinlogFormat )
}
2016-04-18 17:57:18 +00:00
if _ , err := sqlutils . ExecNoPrepare ( this . db , ` set global binlog_format='ROW' ` ) ; err != nil {
return err
}
if _ , err := sqlutils . ExecNoPrepare ( this . db , ` set session binlog_format='ROW' ` ) ; err != nil {
return err
}
2016-08-12 12:26:58 +00:00
if err := this . restartReplication ( ) ; err != nil {
return err
}
2019-10-07 15:10:36 +00:00
this . migrationContext . Log . Debugf ( "'ROW' binlog format applied" )
2016-08-12 12:26:58 +00:00
return nil
2016-04-18 17:57:18 +00:00
}
2016-08-12 12:26:58 +00:00
// We already have RBR, no explicit switch
2016-08-15 09:05:51 +00:00
if ! this . migrationContext . AssumeRBR {
2016-08-12 12:26:58 +00:00
if err := this . restartReplication ( ) ; err != nil {
return err
}
2016-04-18 17:57:18 +00:00
}
return nil
}
2016-04-06 11:05:58 +00:00
// validateBinlogs checks that binary log configuration is good to go
2016-04-04 10:27:51 +00:00
func ( this * Inspector ) validateBinlogs ( ) error {
2016-08-11 12:49:14 +00:00
query := ` select @@global.log_bin, @@global.binlog_format `
var hasBinaryLogs bool
if err := this . db . QueryRow ( query ) . Scan ( & hasBinaryLogs , & this . migrationContext . OriginalBinlogFormat ) ; err != nil {
2016-04-04 10:27:51 +00:00
return err
}
if ! hasBinaryLogs {
2022-02-23 22:28:03 +00:00
return fmt . Errorf ( "%s must have binary logs enabled" , this . connectionConfig . Key . String ( ) )
2016-04-04 10:27:51 +00:00
}
if this . migrationContext . RequiresBinlogFormatChange ( ) {
2016-04-18 17:57:18 +00:00
if ! this . migrationContext . SwitchToRowBinlogFormat {
2022-02-23 22:28:03 +00:00
return fmt . Errorf ( "You must be using ROW binlog format. I can switch it for you, provided --switch-to-rbr and that %s doesn't have replicas" , this . connectionConfig . Key . String ( ) )
2016-04-18 17:57:18 +00:00
}
2022-02-24 00:30:29 +00:00
query := ` show /* gh-ost */ slave hosts `
2016-04-04 10:27:51 +00:00
countReplicas := 0
err := sqlutils . QueryRowsMap ( this . db , query , func ( rowMap sqlutils . RowMap ) error {
countReplicas ++
return nil
} )
if err != nil {
2016-04-04 13:29:02 +00:00
return err
2016-04-04 10:27:51 +00:00
}
if countReplicas > 0 {
2022-02-23 22:28:03 +00:00
return fmt . Errorf ( "%s has %s binlog_format, but I'm too scared to change it to ROW because it has replicas. Bailing out" , this . connectionConfig . Key . String ( ) , this . migrationContext . OriginalBinlogFormat )
2016-04-04 10:27:51 +00:00
}
2022-02-23 22:28:03 +00:00
this . migrationContext . Log . Infof ( "%s has %s binlog_format. I will change it to ROW, and will NOT change it back, even in the event of failure." , this . connectionConfig . Key . String ( ) , this . migrationContext . OriginalBinlogFormat )
2016-04-04 10:27:51 +00:00
}
query = ` select @@global.binlog_row_image `
if err := this . db . QueryRow ( query ) . Scan ( & this . migrationContext . OriginalBinlogRowImage ) ; err != nil {
2022-02-24 23:02:27 +00:00
return err
2016-04-04 10:27:51 +00:00
}
2016-05-20 10:52:14 +00:00
this . migrationContext . OriginalBinlogRowImage = strings . ToUpper ( this . migrationContext . OriginalBinlogRowImage )
2018-04-24 12:56:03 +00:00
if this . migrationContext . OriginalBinlogRowImage != "FULL" {
2022-02-23 22:28:03 +00:00
return fmt . Errorf ( "%s has '%s' binlog_row_image, and only 'FULL' is supported. This operation cannot proceed. You may `set global binlog_row_image='full'` and try again" , this . connectionConfig . Key . String ( ) , this . migrationContext . OriginalBinlogRowImage )
2018-04-24 12:56:03 +00:00
}
2016-04-04 10:27:51 +00:00
2022-02-23 22:28:03 +00:00
this . migrationContext . Log . Infof ( "binary logs validated on %s" , this . connectionConfig . Key . String ( ) )
2016-04-04 10:27:51 +00:00
return nil
}
2016-08-11 15:37:50 +00:00
// validateLogSlaveUpdates checks that binary log log_slave_updates is set. This test is not required when migrating on replica or when migrating directly on master
func ( this * Inspector ) validateLogSlaveUpdates ( ) error {
2016-08-11 12:49:14 +00:00
query := ` select @@global.log_slave_updates `
var logSlaveUpdates bool
if err := this . db . QueryRow ( query ) . Scan ( & logSlaveUpdates ) ; err != nil {
return err
}
2016-10-31 09:25:34 +00:00
if logSlaveUpdates {
2022-02-23 22:28:03 +00:00
this . migrationContext . Log . Infof ( "log_slave_updates validated on %s" , this . connectionConfig . Key . String ( ) )
2016-10-31 09:25:34 +00:00
return nil
2016-08-11 12:49:14 +00:00
}
2016-10-31 09:25:34 +00:00
if this . migrationContext . IsTungsten {
2022-02-23 22:28:03 +00:00
this . migrationContext . Log . Warningf ( "log_slave_updates not found on %s, but --tungsten provided, so I'm proceeding" , this . connectionConfig . Key . String ( ) )
2016-10-31 09:25:34 +00:00
return nil
}
if this . migrationContext . TestOnReplica || this . migrationContext . MigrateOnReplica {
2022-02-23 22:28:03 +00:00
return fmt . Errorf ( "%s must have log_slave_updates enabled for testing/migrating on replica" , this . connectionConfig . Key . String ( ) )
2016-10-31 09:25:34 +00:00
}
if this . migrationContext . InspectorIsAlsoApplier ( ) {
2022-02-23 22:28:03 +00:00
this . migrationContext . Log . Warningf ( "log_slave_updates not found on %s, but executing directly on master, so I'm proceeding" , this . connectionConfig . Key . String ( ) )
2016-10-31 09:25:34 +00:00
return nil
}
2022-02-23 22:28:03 +00:00
return fmt . Errorf ( "%s must have log_slave_updates enabled for executing migration" , this . connectionConfig . Key . String ( ) )
2016-04-04 10:27:51 +00:00
}
// validateTable makes sure the table we need to operate on actually exists
func ( this * Inspector ) validateTable ( ) error {
2016-05-16 09:09:17 +00:00
query := fmt . Sprintf ( ` show /* gh-ost */ table status from %s like '%s' ` , sql . EscapeName ( this . migrationContext . DatabaseName ) , this . migrationContext . OriginalTableName )
2016-04-04 10:27:51 +00:00
tableFound := false
err := sqlutils . QueryRowsMap ( this . db , query , func ( rowMap sqlutils . RowMap ) error {
this . migrationContext . TableEngine = rowMap . GetString ( "Engine" )
this . migrationContext . RowsEstimate = rowMap . GetInt64 ( "Rows" )
this . migrationContext . UsedRowsEstimateMethod = base . TableStatusRowsEstimate
if rowMap . GetString ( "Comment" ) == "VIEW" {
return fmt . Errorf ( "%s.%s is a VIEW, not a real table. Bailing out" , sql . EscapeName ( this . migrationContext . DatabaseName ) , sql . EscapeName ( this . migrationContext . OriginalTableName ) )
}
tableFound = true
return nil
} )
if err != nil {
2016-04-04 13:29:02 +00:00
return err
2016-04-04 10:27:51 +00:00
}
if ! tableFound {
2019-10-07 15:10:36 +00:00
return this . migrationContext . Log . Errorf ( "Cannot find table %s.%s!" , sql . EscapeName ( this . migrationContext . DatabaseName ) , sql . EscapeName ( this . migrationContext . OriginalTableName ) )
2016-04-04 10:27:51 +00:00
}
2019-10-07 15:10:36 +00:00
this . migrationContext . Log . Infof ( "Table found. Engine=%s" , this . migrationContext . TableEngine )
this . migrationContext . Log . Debugf ( "Estimated number of rows via STATUS: %d" , this . migrationContext . RowsEstimate )
2016-04-04 10:27:51 +00:00
return nil
}
2016-06-19 15:55:37 +00:00
// validateTableForeignKeys makes sure no foreign keys exist on the migrated table
2016-10-10 10:28:56 +00:00
func ( this * Inspector ) validateTableForeignKeys ( allowChildForeignKeys bool ) error {
2016-11-21 08:18:40 +00:00
if this . migrationContext . SkipForeignKeyChecks {
2019-10-07 15:10:36 +00:00
this . migrationContext . Log . Warning ( "--skip-foreign-key-checks provided: will not check for foreign keys" )
2016-11-21 08:18:40 +00:00
return nil
}
2016-04-04 16:19:46 +00:00
query := `
2016-10-10 10:28:56 +00:00
SELECT
SUM ( REFERENCED_TABLE_NAME IS NOT NULL AND TABLE_SCHEMA = ? AND TABLE_NAME = ? ) as num_child_side_fk ,
SUM ( REFERENCED_TABLE_NAME IS NOT NULL AND REFERENCED_TABLE_SCHEMA = ? AND REFERENCED_TABLE_NAME = ? ) as num_parent_side_fk
2016-04-04 16:19:46 +00:00
FROM INFORMATION_SCHEMA . KEY_COLUMN_USAGE
WHERE
REFERENCED_TABLE_NAME IS NOT NULL
AND ( ( TABLE_SCHEMA = ? AND TABLE_NAME = ? )
OR ( REFERENCED_TABLE_SCHEMA = ? AND REFERENCED_TABLE_NAME = ? )
)
`
2016-10-10 10:28:56 +00:00
numParentForeignKeys := 0
numChildForeignKeys := 0
err := sqlutils . QueryRowsMap ( this . db , query , func ( m sqlutils . RowMap ) error {
numChildForeignKeys = m . GetInt ( "num_child_side_fk" )
numParentForeignKeys = m . GetInt ( "num_parent_side_fk" )
2016-04-04 16:19:46 +00:00
return nil
} ,
this . migrationContext . DatabaseName ,
this . migrationContext . OriginalTableName ,
this . migrationContext . DatabaseName ,
this . migrationContext . OriginalTableName ,
2016-10-10 10:28:56 +00:00
this . migrationContext . DatabaseName ,
this . migrationContext . OriginalTableName ,
this . migrationContext . DatabaseName ,
this . migrationContext . OriginalTableName ,
2016-04-04 16:19:46 +00:00
)
if err != nil {
return err
}
2016-10-10 10:28:56 +00:00
if numParentForeignKeys > 0 {
2019-10-07 15:10:36 +00:00
return this . migrationContext . Log . Errorf ( "Found %d parent-side foreign keys on %s.%s. Parent-side foreign keys are not supported. Bailing out" , numParentForeignKeys , sql . EscapeName ( this . migrationContext . DatabaseName ) , sql . EscapeName ( this . migrationContext . OriginalTableName ) )
2016-04-04 16:19:46 +00:00
}
2016-10-10 10:28:56 +00:00
if numChildForeignKeys > 0 {
if allowChildForeignKeys {
2019-10-07 15:10:36 +00:00
this . migrationContext . Log . Debugf ( "Foreign keys found and will be dropped, as per given --discard-foreign-keys flag" )
2016-10-10 10:28:56 +00:00
return nil
}
2019-10-07 15:10:36 +00:00
return this . migrationContext . Log . Errorf ( "Found %d child-side foreign keys on %s.%s. Child-side foreign keys are not supported. Bailing out" , numChildForeignKeys , sql . EscapeName ( this . migrationContext . DatabaseName ) , sql . EscapeName ( this . migrationContext . OriginalTableName ) )
2016-04-04 16:19:46 +00:00
}
2019-10-07 15:10:36 +00:00
this . migrationContext . Log . Debugf ( "Validated no foreign keys exist on table" )
2016-04-04 16:19:46 +00:00
return nil
}
2016-08-11 12:10:35 +00:00
// validateTableTriggers makes sure no triggers exist on the migrated table
func ( this * Inspector ) validateTableTriggers ( ) error {
query := `
SELECT COUNT ( * ) AS num_triggers
FROM INFORMATION_SCHEMA . TRIGGERS
WHERE
TRIGGER_SCHEMA = ?
AND EVENT_OBJECT_TABLE = ?
`
numTriggers := 0
err := sqlutils . QueryRowsMap ( this . db , query , func ( rowMap sqlutils . RowMap ) error {
numTriggers = rowMap . GetInt ( "num_triggers" )
return nil
} ,
this . migrationContext . DatabaseName ,
this . migrationContext . OriginalTableName ,
)
if err != nil {
return err
}
if numTriggers > 0 {
2019-10-07 15:10:36 +00:00
return this . migrationContext . Log . Errorf ( "Found triggers on %s.%s. Triggers are not supported at this time. Bailing out" , sql . EscapeName ( this . migrationContext . DatabaseName ) , sql . EscapeName ( this . migrationContext . OriginalTableName ) )
2016-08-11 12:10:35 +00:00
}
2019-10-07 15:10:36 +00:00
this . migrationContext . Log . Debugf ( "Validated no triggers exist on table" )
2016-08-11 12:10:35 +00:00
return nil
}
2016-06-19 15:55:37 +00:00
// estimateTableRowsViaExplain estimates number of rows on original table
2016-04-04 10:27:51 +00:00
func ( this * Inspector ) estimateTableRowsViaExplain ( ) error {
2016-05-16 09:09:17 +00:00
query := fmt . Sprintf ( ` explain select /* gh-ost */ * from %s.%s where 1=1 ` , sql . EscapeName ( this . migrationContext . DatabaseName ) , sql . EscapeName ( this . migrationContext . OriginalTableName ) )
2016-04-04 10:27:51 +00:00
outputFound := false
err := sqlutils . QueryRowsMap ( this . db , query , func ( rowMap sqlutils . RowMap ) error {
this . migrationContext . RowsEstimate = rowMap . GetInt64 ( "rows" )
this . migrationContext . UsedRowsEstimateMethod = base . ExplainRowsEstimate
outputFound = true
return nil
} )
if err != nil {
2016-04-04 13:29:02 +00:00
return err
2016-04-04 10:27:51 +00:00
}
if ! outputFound {
2019-10-07 15:10:36 +00:00
return this . migrationContext . Log . Errorf ( "Cannot run EXPLAIN on %s.%s!" , sql . EscapeName ( this . migrationContext . DatabaseName ) , sql . EscapeName ( this . migrationContext . OriginalTableName ) )
2016-04-04 10:27:51 +00:00
}
2019-10-07 15:10:36 +00:00
this . migrationContext . Log . Infof ( "Estimated number of rows via EXPLAIN: %d" , this . migrationContext . RowsEstimate )
2016-04-04 10:27:51 +00:00
return nil
}
2016-06-19 15:55:37 +00:00
// CountTableRows counts exact number of rows on the original table
2022-07-06 22:23:23 +00:00
func ( this * Inspector ) CountTableRows ( ctx context . Context ) error {
2016-07-29 08:40:23 +00:00
atomic . StoreInt64 ( & this . migrationContext . CountingRowsFlag , 1 )
defer atomic . StoreInt64 ( & this . migrationContext . CountingRowsFlag , 0 )
2019-10-07 15:10:36 +00:00
this . migrationContext . Log . Infof ( "As instructed, I'm issuing a SELECT COUNT(*) on the table. This may take a while" )
2016-07-29 08:40:23 +00:00
2022-07-06 22:23:23 +00:00
conn , err := this . db . Conn ( ctx )
if err != nil {
return err
}
defer conn . Close ( )
var connectionID string
if err := conn . QueryRowContext ( ctx , ` SELECT /* gh-ost */ CONNECTION_ID() ` ) . Scan ( & connectionID ) ; err != nil {
return err
}
2021-04-29 06:33:10 +00:00
query := fmt . Sprintf ( ` select /* gh-ost */ count(*) as count_rows from %s.%s ` , sql . EscapeName ( this . migrationContext . DatabaseName ) , sql . EscapeName ( this . migrationContext . OriginalTableName ) )
2016-08-24 09:39:44 +00:00
var rowsEstimate int64
2022-07-06 22:23:23 +00:00
if err := conn . QueryRowContext ( ctx , query ) . Scan ( & rowsEstimate ) ; err != nil {
switch err {
case context . Canceled , context . DeadlineExceeded :
this . migrationContext . Log . Infof ( "exact row count cancelled (%s), likely because I'm about to cut over. I'm going to kill that query." , ctx . Err ( ) )
2022-07-11 09:02:04 +00:00
return mysql . Kill ( this . db , connectionID )
2022-07-06 22:23:23 +00:00
default :
return err
}
2016-04-04 10:27:51 +00:00
}
2022-07-06 22:23:23 +00:00
// row count query finished. nil out the cancel func, so the main migration thread
// doesn't bother calling it after row copy is done.
this . migrationContext . SetCountTableRowsCancelFunc ( nil )
2016-08-24 09:39:44 +00:00
atomic . StoreInt64 ( & this . migrationContext . RowsEstimate , rowsEstimate )
2016-04-04 10:27:51 +00:00
this . migrationContext . UsedRowsEstimateMethod = base . CountRowsEstimate
2016-07-29 08:40:23 +00:00
2019-10-07 15:10:36 +00:00
this . migrationContext . Log . Infof ( "Exact number of rows via COUNT: %d" , rowsEstimate )
2016-07-29 08:40:23 +00:00
2016-04-04 10:27:51 +00:00
return nil
}
2016-09-07 12:24:11 +00:00
// applyColumnTypes
func ( this * Inspector ) applyColumnTypes ( databaseName , tableName string , columnsLists ... * sql . ColumnList ) error {
query := `
select
*
from
information_schema . columns
where
table_schema = ?
and table_name = ?
`
err := sqlutils . QueryRowsMap ( this . db , query , func ( m sqlutils . RowMap ) error {
columnName := m . GetString ( "COLUMN_NAME" )
2016-10-13 11:09:58 +00:00
columnType := m . GetString ( "COLUMN_TYPE" )
2021-01-13 09:21:21 +00:00
columnOctetLength := m . GetUint ( "CHARACTER_OCTET_LENGTH" )
2018-11-01 13:33:11 +00:00
for _ , columnsList := range columnsLists {
column := columnsList . GetColumn ( columnName )
if column == nil {
continue
2016-08-17 04:51:58 +00:00
}
2018-11-01 13:33:11 +00:00
if strings . Contains ( columnType , "unsigned" ) {
column . IsUnsigned = true
2017-01-10 07:57:42 +00:00
}
2018-11-01 13:33:11 +00:00
if strings . Contains ( columnType , "mediumint" ) {
column . Type = sql . MediumIntColumnType
2016-10-13 11:09:58 +00:00
}
2018-11-01 13:33:11 +00:00
if strings . Contains ( columnType , "timestamp" ) {
column . Type = sql . TimestampColumnType
2016-10-13 11:09:58 +00:00
}
2018-11-01 13:33:11 +00:00
if strings . Contains ( columnType , "datetime" ) {
column . Type = sql . DateTimeColumnType
2017-08-22 08:51:20 +00:00
}
2018-11-01 13:33:11 +00:00
if strings . Contains ( columnType , "json" ) {
column . Type = sql . JSONColumnType
2017-09-03 06:57:26 +00:00
}
2018-11-01 13:33:11 +00:00
if strings . Contains ( columnType , "float" ) {
column . Type = sql . FloatColumnType
2016-10-19 13:22:29 +00:00
}
2018-11-01 13:33:11 +00:00
if strings . HasPrefix ( columnType , "enum" ) {
column . Type = sql . EnumColumnType
2021-06-10 15:17:49 +00:00
column . EnumValues = sql . ParseEnumValues ( m . GetString ( "COLUMN_TYPE" ) )
2018-11-01 13:33:11 +00:00
}
2021-01-13 09:21:21 +00:00
if strings . HasPrefix ( columnType , "binary" ) {
column . Type = sql . BinaryColumnType
column . BinaryOctetLength = columnOctetLength
}
2018-11-01 13:33:11 +00:00
if charset := m . GetString ( "CHARACTER_SET_NAME" ) ; charset != "" {
column . Charset = charset
2016-09-07 12:24:11 +00:00
}
}
2016-08-17 04:51:58 +00:00
return nil
2016-09-07 12:24:11 +00:00
} , databaseName , tableName )
2016-08-17 04:51:58 +00:00
return err
}
2021-05-14 13:32:56 +00:00
// getAutoIncrementValue get's the original table's AUTO_INCREMENT value, if exists (0 value if not exists)
func ( this * Inspector ) getAutoIncrementValue ( tableName string ) ( autoIncrement uint64 , err error ) {
query := `
SELECT
AUTO_INCREMENT
FROM INFORMATION_SCHEMA . TABLES
WHERE
TABLES . TABLE_SCHEMA = ?
AND TABLES . TABLE_NAME = ?
AND AUTO_INCREMENT IS NOT NULL
`
err = sqlutils . QueryRowsMap ( this . db , query , func ( m sqlutils . RowMap ) error {
autoIncrement = m . GetUint64 ( "AUTO_INCREMENT" )
return nil
} , this . migrationContext . DatabaseName , tableName )
return autoIncrement , err
}
2016-04-04 10:27:51 +00:00
// getCandidateUniqueKeys investigates a table and returns the list of unique keys
// candidate for chunking
func ( this * Inspector ) getCandidateUniqueKeys ( tableName string ) ( uniqueKeys [ ] ( * sql . UniqueKey ) , err error ) {
query := `
SELECT
COLUMNS . TABLE_SCHEMA ,
COLUMNS . TABLE_NAME ,
COLUMNS . COLUMN_NAME ,
UNIQUES . INDEX_NAME ,
UNIQUES . COLUMN_NAMES ,
UNIQUES . COUNT_COLUMN_IN_INDEX ,
COLUMNS . DATA_TYPE ,
COLUMNS . CHARACTER_SET_NAME ,
2016-04-18 17:57:18 +00:00
LOCATE ( ' auto_increment ' , EXTRA ) > 0 as is_auto_increment ,
2016-04-04 10:27:51 +00:00
has_nullable
FROM INFORMATION_SCHEMA . COLUMNS INNER JOIN (
SELECT
TABLE_SCHEMA ,
TABLE_NAME ,
INDEX_NAME ,
COUNT ( * ) AS COUNT_COLUMN_IN_INDEX ,
GROUP_CONCAT ( COLUMN_NAME ORDER BY SEQ_IN_INDEX ASC ) AS COLUMN_NAMES ,
SUBSTRING_INDEX ( GROUP_CONCAT ( COLUMN_NAME ORDER BY SEQ_IN_INDEX ASC ) , ',' , 1 ) AS FIRST_COLUMN_NAME ,
SUM ( NULLABLE = ' YES ' ) > 0 AS has_nullable
FROM INFORMATION_SCHEMA . STATISTICS
2016-05-04 06:50:00 +00:00
WHERE
NON_UNIQUE = 0
AND TABLE_SCHEMA = ?
AND TABLE_NAME = ?
2016-04-04 10:27:51 +00:00
GROUP BY TABLE_SCHEMA , TABLE_NAME , INDEX_NAME
) AS UNIQUES
ON (
COLUMNS . COLUMN_NAME = UNIQUES . FIRST_COLUMN_NAME
)
WHERE
COLUMNS . TABLE_SCHEMA = ?
AND COLUMNS . TABLE_NAME = ?
ORDER BY
COLUMNS . TABLE_SCHEMA , COLUMNS . TABLE_NAME ,
CASE UNIQUES . INDEX_NAME
WHEN ' PRIMARY ' THEN 0
ELSE 1
END ,
CASE has_nullable
WHEN 0 THEN 0
ELSE 1
END ,
CASE IFNULL ( CHARACTER_SET_NAME , ' ' )
WHEN ' ' THEN 0
ELSE 1
END ,
CASE DATA_TYPE
WHEN ' tinyint ' THEN 0
WHEN ' smallint ' THEN 1
WHEN ' int ' THEN 2
WHEN ' bigint ' THEN 3
ELSE 100
END ,
COUNT_COLUMN_IN_INDEX
`
2016-04-18 17:57:18 +00:00
err = sqlutils . QueryRowsMap ( this . db , query , func ( m sqlutils . RowMap ) error {
2016-04-04 10:27:51 +00:00
uniqueKey := & sql . UniqueKey {
2016-04-18 17:57:18 +00:00
Name : m . GetString ( "INDEX_NAME" ) ,
Columns : * sql . ParseColumnList ( m . GetString ( "COLUMN_NAMES" ) ) ,
HasNullable : m . GetBool ( "has_nullable" ) ,
IsAutoIncrement : m . GetBool ( "is_auto_increment" ) ,
2016-04-04 10:27:51 +00:00
}
uniqueKeys = append ( uniqueKeys , uniqueKey )
return nil
2016-05-04 06:50:00 +00:00
} , this . migrationContext . DatabaseName , tableName , this . migrationContext . DatabaseName , tableName )
2016-04-04 10:27:51 +00:00
if err != nil {
return uniqueKeys , err
}
2019-10-07 15:10:36 +00:00
this . migrationContext . Log . Debugf ( "Potential unique keys in %+v: %+v" , tableName , uniqueKeys )
2016-04-04 10:27:51 +00:00
return uniqueKeys , nil
}
2016-04-08 12:35:06 +00:00
// getSharedUniqueKeys returns the intersection of two given unique keys,
// testing by list of columns
2022-09-06 13:23:18 +00:00
func ( this * Inspector ) getSharedUniqueKeys ( originalUniqueKeys , ghostUniqueKeys [ ] * sql . UniqueKey ) ( uniqueKeys [ ] * sql . UniqueKey ) {
2016-04-04 10:27:51 +00:00
// We actually do NOT rely on key name, just on the set of columns. This is because maybe
// the ALTER is on the name itself...
for _ , originalUniqueKey := range originalUniqueKeys {
for _ , ghostUniqueKey := range ghostUniqueKeys {
2016-09-07 12:24:11 +00:00
if originalUniqueKey . Columns . EqualsByNames ( & ghostUniqueKey . Columns ) {
2016-04-04 10:27:51 +00:00
uniqueKeys = append ( uniqueKeys , originalUniqueKey )
}
}
}
2022-09-06 13:23:18 +00:00
return uniqueKeys
2016-04-04 10:27:51 +00:00
}
2016-04-04 13:29:02 +00:00
2016-04-08 12:35:06 +00:00
// getSharedColumns returns the intersection of two lists of columns in same order as the first list
2018-05-22 09:36:52 +00:00
func ( this * Inspector ) getSharedColumns ( originalColumns , ghostColumns * sql . ColumnList , originalVirtualColumns , ghostVirtualColumns * sql . ColumnList , columnRenameMap map [ string ] string ) ( * sql . ColumnList , * sql . ColumnList ) {
2016-04-11 15:27:16 +00:00
sharedColumnNames := [ ] string { }
2016-09-07 12:24:11 +00:00
for _ , originalColumn := range originalColumns . Names ( ) {
2017-04-23 05:38:35 +00:00
isSharedColumn := false
2017-07-12 09:33:32 +00:00
for _ , ghostColumn := range ghostColumns . Names ( ) {
if strings . EqualFold ( originalColumn , ghostColumn ) {
isSharedColumn = true
2018-04-27 06:58:07 +00:00
break
2017-07-12 09:33:32 +00:00
}
if strings . EqualFold ( columnRenameMap [ originalColumn ] , ghostColumn ) {
isSharedColumn = true
2018-04-27 06:58:07 +00:00
break
2017-07-12 09:33:32 +00:00
}
2017-04-23 05:38:35 +00:00
}
2017-07-12 09:33:32 +00:00
for droppedColumn := range this . migrationContext . DroppedColumnsMap {
if strings . EqualFold ( originalColumn , droppedColumn ) {
isSharedColumn = false
2018-04-27 06:58:07 +00:00
break
2017-07-12 09:33:32 +00:00
}
2017-04-23 05:38:35 +00:00
}
2018-05-22 09:36:52 +00:00
for _ , virtualColumn := range originalVirtualColumns . Names ( ) {
if strings . EqualFold ( originalColumn , virtualColumn ) {
isSharedColumn = false
}
}
for _ , virtualColumn := range ghostVirtualColumns . Names ( ) {
if strings . EqualFold ( originalColumn , virtualColumn ) {
isSharedColumn = false
}
}
2017-04-23 05:38:35 +00:00
if isSharedColumn {
2016-04-11 15:27:16 +00:00
sharedColumnNames = append ( sharedColumnNames , originalColumn )
2016-04-08 12:35:06 +00:00
}
}
2016-06-17 06:03:18 +00:00
mappedSharedColumnNames := [ ] string { }
for _ , columnName := range sharedColumnNames {
if mapped , ok := columnRenameMap [ columnName ] ; ok {
mappedSharedColumnNames = append ( mappedSharedColumnNames , mapped )
} else {
mappedSharedColumnNames = append ( mappedSharedColumnNames , columnName )
}
}
return sql . NewColumnList ( sharedColumnNames ) , sql . NewColumnList ( mappedSharedColumnNames )
2016-04-08 12:35:06 +00:00
}
2016-06-22 10:39:13 +00:00
// showCreateTable returns the `show create table` statement for given table
func ( this * Inspector ) showCreateTable ( tableName string ) ( createTableStatement string , err error ) {
var dummy string
query := fmt . Sprintf ( ` show /* gh-ost */ create table %s.%s ` , sql . EscapeName ( this . migrationContext . DatabaseName ) , sql . EscapeName ( tableName ) )
err = this . db . QueryRow ( query ) . Scan ( & dummy , & createTableStatement )
return createTableStatement , err
}
2016-06-19 15:55:37 +00:00
// readChangelogState reads changelog hints
2016-12-26 19:31:35 +00:00
func ( this * Inspector ) readChangelogState ( hint string ) ( string , error ) {
2016-04-14 11:37:56 +00:00
query := fmt . Sprintf ( `
2016-12-26 19:31:35 +00:00
select hint , value from % s . % s where hint = ? and id <= 255
2016-04-14 11:37:56 +00:00
` ,
sql . EscapeName ( this . migrationContext . DatabaseName ) ,
sql . EscapeName ( this . migrationContext . GetChangelogTableName ( ) ) ,
)
2016-12-26 19:31:35 +00:00
result := ""
2016-04-14 11:37:56 +00:00
err := sqlutils . QueryRowsMap ( this . db , query , func ( m sqlutils . RowMap ) error {
2016-12-26 19:31:35 +00:00
result = m . GetString ( "value" )
2016-04-14 11:37:56 +00:00
return nil
2016-12-26 19:31:35 +00:00
} , hint )
2016-04-14 11:37:56 +00:00
return result , err
2016-04-04 13:29:02 +00:00
}
2016-04-14 11:37:56 +00:00
func ( this * Inspector ) getMasterConnectionConfig ( ) ( applierConfig * mysql . ConnectionConfig , err error ) {
2019-10-07 15:10:36 +00:00
this . migrationContext . Log . Infof ( "Recursively searching for replication master" )
2016-04-14 11:37:56 +00:00
visitedKeys := mysql . NewInstanceKeyMap ( )
2016-06-22 08:38:13 +00:00
return mysql . GetMasterConnectionConfigSafe ( this . connectionConfig , visitedKeys , this . migrationContext . AllowedMasterMaster )
2016-04-04 13:29:02 +00:00
}
2016-11-02 11:48:35 +00:00
func ( this * Inspector ) getReplicationLag ( ) ( replicationLag time . Duration , err error ) {
2018-04-28 04:20:28 +00:00
replicationLag , err = mysql . GetReplicationLagFromSlaveStatus (
2017-09-22 23:06:06 +00:00
this . informationSchemaDb ,
2016-11-02 11:48:35 +00:00
)
2016-11-03 11:14:53 +00:00
return replicationLag , err
2016-11-02 11:48:35 +00:00
}
2017-08-28 22:53:47 +00:00
func ( this * Inspector ) Teardown ( ) {
this . db . Close ( )
2017-09-22 23:06:06 +00:00
this . informationSchemaDb . Close ( )
2017-08-28 22:53:47 +00:00
}