Skip to content

Commit 5ebb953

Browse files
Cleanup whitespace and comments in SQL query text (#1246)
* Cleanup whitespace in SQL query text * cleanup * Add indent * Update unit tests * Update unit tests, pt 2 * Fix tweaks * Fix merge conflict resolution Signed-off-by: Tim Vaillancourt <[email protected]> --------- Signed-off-by: Tim Vaillancourt <[email protected]>
1 parent 347d8e0 commit 5ebb953

File tree

5 files changed

+287
-222
lines changed

5 files changed

+287
-222
lines changed

go/logic/applier.go

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (this *Applier) InitDBConnections() (err error) {
108108

109109
// validateAndReadTimeZone potentially reads server time-zone
110110
func (this *Applier) validateAndReadTimeZone() error {
111-
query := `select @@global.time_zone`
111+
query := `select /* gh-ost */ @@global.time_zone`
112112
if err := this.db.QueryRow(query).Scan(&this.migrationContext.ApplierTimeZone); err != nil {
113113
return err
114114
}
@@ -392,14 +392,15 @@ func (this *Applier) WriteChangelog(hint, value string) (string, error) {
392392
explicitId = 3
393393
}
394394
query := fmt.Sprintf(`
395-
insert /* gh-ost */ into %s.%s
396-
(id, hint, value)
397-
values
398-
(NULLIF(?, 0), ?, ?)
399-
on duplicate key update
400-
last_update=NOW(),
401-
value=VALUES(value)
402-
`,
395+
insert /* gh-ost */
396+
into
397+
%s.%s
398+
(id, hint, value)
399+
values
400+
(NULLIF(?, 0), ?, ?)
401+
on duplicate key update
402+
last_update=NOW(),
403+
value=VALUES(value)`,
403404
sql.EscapeName(this.migrationContext.DatabaseName),
404405
sql.EscapeName(this.migrationContext.GetChangelogTableName()),
405406
)
@@ -854,7 +855,7 @@ func (this *Applier) GetSessionLockName(sessionId int64) string {
854855
// ExpectUsedLock expects the special hint voluntary lock to exist on given session
855856
func (this *Applier) ExpectUsedLock(sessionId int64) error {
856857
var result int64
857-
query := `select is_used_lock(?)`
858+
query := `select /* gh-ost */ is_used_lock(?)`
858859
lockName := this.GetSessionLockName(sessionId)
859860
this.migrationContext.Log.Infof("Checking session lock: %s", lockName)
860861
if err := this.db.QueryRow(query, lockName).Scan(&result); err != nil || result != sessionId {
@@ -867,14 +868,14 @@ func (this *Applier) ExpectUsedLock(sessionId int64) error {
867868
func (this *Applier) ExpectProcess(sessionId int64, stateHint, infoHint string) error {
868869
found := false
869870
query := `
870-
select id
871-
from information_schema.processlist
872-
where
873-
id != connection_id()
874-
and ? in (0, id)
875-
and state like concat('%', ?, '%')
876-
and info like concat('%', ?, '%')
877-
`
871+
select /* gh-ost */ id
872+
from
873+
information_schema.processlist
874+
where
875+
id != connection_id()
876+
and ? in (0, id)
877+
and state like concat('%', ?, '%')
878+
and info like concat('%', ?, '%')`
878879
err := sqlutils.QueryRowsMap(this.db, query, func(m sqlutils.RowMap) error {
879880
found = true
880881
return nil
@@ -912,10 +913,10 @@ func (this *Applier) CreateAtomicCutOverSentryTable() error {
912913
}
913914
tableName := this.migrationContext.GetOldTableName()
914915

915-
query := fmt.Sprintf(`create /* gh-ost */ table %s.%s (
916+
query := fmt.Sprintf(`
917+
create /* gh-ost */ table %s.%s (
916918
id int auto_increment primary key
917-
) engine=%s comment='%s'
918-
`,
919+
) engine=%s comment='%s'`,
919920
sql.EscapeName(this.migrationContext.DatabaseName),
920921
sql.EscapeName(tableName),
921922
this.migrationContext.TableEngine,
@@ -949,14 +950,14 @@ func (this *Applier) AtomicCutOverMagicLock(sessionIdChan chan int64, tableLocke
949950
}()
950951

951952
var sessionId int64
952-
if err := tx.QueryRow(`select connection_id()`).Scan(&sessionId); err != nil {
953+
if err := tx.QueryRow(`select /* gh-ost */ connection_id()`).Scan(&sessionId); err != nil {
953954
tableLocked <- err
954955
return err
955956
}
956957
sessionIdChan <- sessionId
957958

958959
lockResult := 0
959-
query := `select get_lock(?, 0)`
960+
query := `select /* gh-ost */ get_lock(?, 0)`
960961
lockName := this.GetSessionLockName(sessionId)
961962
this.migrationContext.Log.Infof("Grabbing voluntary lock: %s", lockName)
962963
if err := tx.QueryRow(query, lockName).Scan(&lockResult); err != nil || lockResult != 1 {
@@ -967,7 +968,7 @@ func (this *Applier) AtomicCutOverMagicLock(sessionIdChan chan int64, tableLocke
967968

968969
tableLockTimeoutSeconds := this.migrationContext.CutOverLockTimeoutSeconds * 2
969970
this.migrationContext.Log.Infof("Setting LOCK timeout as %d seconds", tableLockTimeoutSeconds)
970-
query = fmt.Sprintf(`set session lock_wait_timeout:=%d`, tableLockTimeoutSeconds)
971+
query = fmt.Sprintf(`set /* gh-ost */ session lock_wait_timeout:=%d`, tableLockTimeoutSeconds)
971972
if _, err := tx.Exec(query); err != nil {
972973
tableLocked <- err
973974
return err
@@ -1026,7 +1027,7 @@ func (this *Applier) AtomicCutOverMagicLock(sessionIdChan chan int64, tableLocke
10261027
sql.EscapeName(this.migrationContext.DatabaseName),
10271028
sql.EscapeName(this.migrationContext.GetOldTableName()),
10281029
)
1029-
query = `unlock tables`
1030+
query = `unlock /* gh-ost */ tables`
10301031
if _, err := tx.Exec(query); err != nil {
10311032
tableUnlocked <- err
10321033
return this.migrationContext.Log.Errore(err)
@@ -1048,13 +1049,13 @@ func (this *Applier) AtomicCutoverRename(sessionIdChan chan int64, tablesRenamed
10481049
tablesRenamed <- fmt.Errorf("Unexpected error in AtomicCutoverRename(), injected to release blocking channel reads")
10491050
}()
10501051
var sessionId int64
1051-
if err := tx.QueryRow(`select connection_id()`).Scan(&sessionId); err != nil {
1052+
if err := tx.QueryRow(`select /* gh-ost */ connection_id()`).Scan(&sessionId); err != nil {
10521053
return err
10531054
}
10541055
sessionIdChan <- sessionId
10551056

10561057
this.migrationContext.Log.Infof("Setting RENAME timeout as %d seconds", this.migrationContext.CutOverLockTimeoutSeconds)
1057-
query := fmt.Sprintf(`set session lock_wait_timeout:=%d`, this.migrationContext.CutOverLockTimeoutSeconds)
1058+
query := fmt.Sprintf(`set /* gh-ost */ session lock_wait_timeout:=%d`, this.migrationContext.CutOverLockTimeoutSeconds)
10581059
if _, err := tx.Exec(query); err != nil {
10591060
return err
10601061
}
@@ -1080,7 +1081,7 @@ func (this *Applier) AtomicCutoverRename(sessionIdChan chan int64, tablesRenamed
10801081
}
10811082

10821083
func (this *Applier) ShowStatusVariable(variableName string) (result int64, err error) {
1083-
query := fmt.Sprintf(`show global status like '%s'`, variableName)
1084+
query := fmt.Sprintf(`show /* gh-ost */ global status like '%s'`, variableName)
10841085
if err := this.db.QueryRow(query).Scan(&variableName, &result); err != nil {
10851086
return 0, err
10861087
}
@@ -1150,7 +1151,7 @@ func (this *Applier) ApplyDMLEventQueries(dmlEvents [](*binlog.BinlogDMLEvent))
11501151
return err
11511152
}
11521153

1153-
sessionQuery := "SET SESSION time_zone = '+00:00'"
1154+
sessionQuery := "SET /* gh-ost */ SESSION time_zone = '+00:00'"
11541155
sessionQuery = fmt.Sprintf("%s, %s", sessionQuery, this.generateSqlModeQuery())
11551156

11561157
if _, err := tx.Exec(sessionQuery); err != nil {

go/logic/applier_test.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,12 @@ func TestApplierBuildDMLEventQuery(t *testing.T) {
114114
res := applier.buildDMLEventQuery(binlogEvent)
115115
test.S(t).ExpectEquals(len(res), 1)
116116
test.S(t).ExpectNil(res[0].err)
117-
test.S(t).ExpectEquals(strings.TrimSpace(res[0].query),
118-
`delete /* gh-ost `+"`test`.`_test_gho`"+` */
119-
from
120-
`+"`test`.`_test_gho`"+`
121-
where
122-
((`+"`id`"+` = ?) and (`+"`item_id`"+` = ?))`)
123-
117+
test.S(t).ExpectEquals(strings.TrimSpace(res[0].query), `delete /* gh-ost `+"`test`.`_test_gho`"+` */
118+
from
119+
`+"`test`.`_test_gho`"+`
120+
where
121+
((`+"`id`"+` = ?) and (`+"`item_id`"+` = ?))`,
122+
)
124123
test.S(t).ExpectEquals(len(res[0].args), 2)
125124
test.S(t).ExpectEquals(res[0].args[0], 123456)
126125
test.S(t).ExpectEquals(res[0].args[1], 42)
@@ -136,11 +135,12 @@ func TestApplierBuildDMLEventQuery(t *testing.T) {
136135
test.S(t).ExpectEquals(len(res), 1)
137136
test.S(t).ExpectNil(res[0].err)
138137
test.S(t).ExpectEquals(strings.TrimSpace(res[0].query),
139-
`replace /* gh-ost `+"`test`.`_test_gho`"+` */ into
140-
`+"`test`.`_test_gho`"+`
141-
`+"(`id`, `item_id`)"+`
142-
values
143-
(?, ?)`)
138+
`replace /* gh-ost `+"`test`.`_test_gho`"+` */
139+
into
140+
`+"`test`.`_test_gho`"+`
141+
`+"(`id`, `item_id`)"+`
142+
values
143+
(?, ?)`)
144144
test.S(t).ExpectEquals(len(res[0].args), 2)
145145
test.S(t).ExpectEquals(res[0].args[0], 123456)
146146
test.S(t).ExpectEquals(res[0].args[1], 42)
@@ -158,11 +158,11 @@ func TestApplierBuildDMLEventQuery(t *testing.T) {
158158
test.S(t).ExpectNil(res[0].err)
159159
test.S(t).ExpectEquals(strings.TrimSpace(res[0].query),
160160
`update /* gh-ost `+"`test`.`_test_gho`"+` */
161-
`+"`test`.`_test_gho`"+`
162-
set
163-
`+"`id`"+`=?, `+"`item_id`"+`=?
164-
where
165-
((`+"`id`"+` = ?) and (`+"`item_id`"+` = ?))`)
161+
`+"`test`.`_test_gho`"+`
162+
set
163+
`+"`id`"+`=?, `+"`item_id`"+`=?
164+
where
165+
((`+"`id`"+` = ?) and (`+"`item_id`"+` = ?))`)
166166
test.S(t).ExpectEquals(len(res[0].args), 4)
167167
test.S(t).ExpectEquals(res[0].args[0], 123456)
168168
test.S(t).ExpectEquals(res[0].args[1], 42)

0 commit comments

Comments
 (0)