Pass entire sql.UniqueKey

This commit is contained in:
Tim Vaillancourt 2022-12-28 23:53:20 +01:00
parent 846b20edd1
commit 1bc9c5104b
3 changed files with 16 additions and 18 deletions

View File

@ -472,8 +472,7 @@ func (this *Applier) ExecuteThrottleQuery() (int64, error) {
// readMigrationMinValues returns the minimum values to be iterated on rowcopy // readMigrationMinValues returns the minimum values to be iterated on rowcopy
func (this *Applier) readMigrationMinValues(tx *gosql.Tx, uniqueKey *sql.UniqueKey) error { func (this *Applier) readMigrationMinValues(tx *gosql.Tx, uniqueKey *sql.UniqueKey) error {
this.migrationContext.Log.Debugf("Reading migration range according to key: %s", uniqueKey.Name) this.migrationContext.Log.Debugf("Reading migration range according to key: %s", uniqueKey.Name)
query, err := sql.BuildUniqueKeyMinValuesPreparedQuery(this.migrationContext.DatabaseName, this.migrationContext.OriginalTableName, query, err := sql.BuildUniqueKeyMinValuesPreparedQuery(this.migrationContext.DatabaseName, this.migrationContext.OriginalTableName, uniqueKey)
this.migrationContext.UniqueKey.Name, &uniqueKey.Columns)
if err != nil { if err != nil {
return err return err
} }
@ -498,8 +497,7 @@ func (this *Applier) readMigrationMinValues(tx *gosql.Tx, uniqueKey *sql.UniqueK
// readMigrationMaxValues returns the maximum values to be iterated on rowcopy // readMigrationMaxValues returns the maximum values to be iterated on rowcopy
func (this *Applier) readMigrationMaxValues(tx *gosql.Tx, uniqueKey *sql.UniqueKey) error { func (this *Applier) readMigrationMaxValues(tx *gosql.Tx, uniqueKey *sql.UniqueKey) error {
this.migrationContext.Log.Debugf("Reading migration range according to key: %s", uniqueKey.Name) this.migrationContext.Log.Debugf("Reading migration range according to key: %s", uniqueKey.Name)
query, err := sql.BuildUniqueKeyMaxValuesPreparedQuery(this.migrationContext.DatabaseName, this.migrationContext.OriginalTableName, query, err := sql.BuildUniqueKeyMaxValuesPreparedQuery(this.migrationContext.DatabaseName, this.migrationContext.OriginalTableName, uniqueKey)
this.migrationContext.UniqueKey.Name, &uniqueKey.Columns)
if err != nil { if err != nil {
return err return err
} }

View File

@ -352,24 +352,24 @@ func BuildUniqueKeyRangeEndPreparedQueryViaTemptable(databaseName, tableName str
return result, explodedArgs, nil return result, explodedArgs, nil
} }
func BuildUniqueKeyMinValuesPreparedQuery(databaseName, tableName, indexName string, uniqueKeyColumns *ColumnList) (string, error) { func BuildUniqueKeyMinValuesPreparedQuery(databaseName, tableName string, uniqueKey *UniqueKey) (string, error) {
return buildUniqueKeyMinMaxValuesPreparedQuery(databaseName, tableName, indexName, uniqueKeyColumns, "asc") return buildUniqueKeyMinMaxValuesPreparedQuery(databaseName, tableName, uniqueKey, "asc")
} }
func BuildUniqueKeyMaxValuesPreparedQuery(databaseName, tableName, indexName string, uniqueKeyColumns *ColumnList) (string, error) { func BuildUniqueKeyMaxValuesPreparedQuery(databaseName, tableName string, uniqueKey *UniqueKey) (string, error) {
return buildUniqueKeyMinMaxValuesPreparedQuery(databaseName, tableName, indexName, uniqueKeyColumns, "desc") return buildUniqueKeyMinMaxValuesPreparedQuery(databaseName, tableName, uniqueKey, "desc")
} }
func buildUniqueKeyMinMaxValuesPreparedQuery(databaseName, tableName, indexName string, uniqueKeyColumns *ColumnList, order string) (string, error) { func buildUniqueKeyMinMaxValuesPreparedQuery(databaseName, tableName string, uniqueKey *UniqueKey, order string) (string, error) {
if uniqueKeyColumns.Len() == 0 { if uniqueKey.Columns.Len() == 0 {
return "", fmt.Errorf("Got 0 columns in BuildUniqueKeyMinMaxValuesPreparedQuery") return "", fmt.Errorf("Got 0 columns in BuildUniqueKeyMinMaxValuesPreparedQuery")
} }
databaseName = EscapeName(databaseName) databaseName = EscapeName(databaseName)
tableName = EscapeName(tableName) tableName = EscapeName(tableName)
uniqueKeyColumnNames := duplicateNames(uniqueKeyColumns.Names()) uniqueKeyColumnNames := duplicateNames(uniqueKey.Columns.Names())
uniqueKeyColumnOrder := make([]string, len(uniqueKeyColumnNames)) uniqueKeyColumnOrder := make([]string, len(uniqueKeyColumnNames))
for i, column := range uniqueKeyColumns.Columns() { for i, column := range uniqueKey.Columns.Columns() {
uniqueKeyColumnNames[i] = EscapeName(uniqueKeyColumnNames[i]) uniqueKeyColumnNames[i] = EscapeName(uniqueKeyColumnNames[i])
if column.Type == EnumColumnType { if column.Type == EnumColumnType {
uniqueKeyColumnOrder[i] = fmt.Sprintf("concat(%s) %s", uniqueKeyColumnNames[i], order) uniqueKeyColumnOrder[i] = fmt.Sprintf("concat(%s) %s", uniqueKeyColumnNames[i], order)
@ -385,7 +385,7 @@ func buildUniqueKeyMinMaxValuesPreparedQuery(databaseName, tableName, indexName
order by %s limit 1 order by %s limit 1
`, `,
databaseName, tableName, strings.Join(uniqueKeyColumnNames, ", "), databaseName, tableName, strings.Join(uniqueKeyColumnNames, ", "),
databaseName, tableName, indexName, databaseName, tableName, uniqueKey.Name,
strings.Join(uniqueKeyColumnOrder, ", "), strings.Join(uniqueKeyColumnOrder, ", "),
) )
return query, nil return query, nil

View File

@ -1,5 +1,5 @@
/* /*
Copyright 2016 GitHub Inc. Copyright 2022 GitHub Inc.
See https://github.com/github/gh-ost/blob/master/LICENSE See https://github.com/github/gh-ost/blob/master/LICENSE
*/ */
@ -309,10 +309,10 @@ func TestBuildUniqueKeyRangeEndPreparedQuery(t *testing.T) {
func TestBuildUniqueKeyMinValuesPreparedQuery(t *testing.T) { func TestBuildUniqueKeyMinValuesPreparedQuery(t *testing.T) {
databaseName := "mydb" databaseName := "mydb"
originalTableName := "tbl" originalTableName := "tbl"
indexName := "PRIMARY" columnList := NewColumnList([]string{"name", "position"})
uniqueKeyColumns := NewColumnList([]string{"name", "position"}) uniqueKey := &UniqueKey{Name: "PRIMARY", Columns: *columnList}
{ {
query, err := BuildUniqueKeyMinValuesPreparedQuery(databaseName, originalTableName, indexName, uniqueKeyColumns) query, err := BuildUniqueKeyMinValuesPreparedQuery(databaseName, originalTableName, uniqueKey)
test.S(t).ExpectNil(err) test.S(t).ExpectNil(err)
expected := ` expected := `
select /* gh-ost mydb.tbl */ name, position select /* gh-ost mydb.tbl */ name, position
@ -326,7 +326,7 @@ func TestBuildUniqueKeyMinValuesPreparedQuery(t *testing.T) {
test.S(t).ExpectEquals(normalizeQuery(query), normalizeQuery(expected)) test.S(t).ExpectEquals(normalizeQuery(query), normalizeQuery(expected))
} }
{ {
query, err := BuildUniqueKeyMaxValuesPreparedQuery(databaseName, originalTableName, indexName, uniqueKeyColumns) query, err := BuildUniqueKeyMaxValuesPreparedQuery(databaseName, originalTableName, uniqueKey)
test.S(t).ExpectNil(err) test.S(t).ExpectNil(err)
expected := ` expected := `
select /* gh-ost mydb.tbl */ name, position select /* gh-ost mydb.tbl */ name, position