-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·445 lines (390 loc) · 12.2 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package main
import (
"database/sql"
"flag"
"fmt"
"log"
"os"
"strings"
"time"
"unicode/utf8"
"github.com/fatih/color"
_ "github.com/go-sql-driver/mysql"
)
// Declare the database connection globally so it can be used by all functions
//var db *sql.DB
// Define flags
var (
source = flag.String("s", "", "Source Host")
database = flag.String("d", "", "Database Name")
table = flag.String("t", "", "Select table")
show = flag.Bool("show", false, "Show Databases")
scanTable = flag.Bool("scan", false, "Scan specific table")
)
// read the ~/.my.cnf file to get the database credentials
func readMyCnf() {
file, err := os.ReadFile(os.Getenv("HOME") + "/.my.cnf")
if err != nil {
log.Fatal(err)
}
lines := strings.Split(string(file), "\n")
for _, line := range lines {
if strings.HasPrefix(line, "user") {
os.Setenv("MYSQL_USER", strings.TrimSpace(line[5:]))
}
if strings.HasPrefix(line, "password") {
os.Setenv("MYSQL_PASSWORD", strings.TrimSpace(line[9:]))
}
}
}
func connectToDatabase(source string) (*sql.DB, error) {
db, err := sql.Open("mysql", os.Getenv("MYSQL_USER")+":"+os.Getenv("MYSQL_PASSWORD")+"@tcp("+source+":3306)/"+*database)
if err != nil {
return nil, err
}
err = db.Ping()
if err != nil {
return nil, err
}
// Get the hostname of the connected MySQL server
var hostname string
err = db.QueryRow("SELECT @@hostname").Scan(&hostname)
if err != nil {
return nil, err
}
// Print the result (simplified)
fmt.Printf("Connected to %s (%s): %s\n", source, hostname, color.GreenString("✔"))
fmt.Println()
return db, nil
}
// Create a function that will get the Character Set and Collation for the table given with the -t table flag and the database with the -d database flag
func getTableCollationCharacterSet(db *sql.DB, database string, table string) (string, string, error) {
var characterSet string
var collation string
err := db.QueryRow(fmt.Sprintf("SELECT CCSA.character_set_name, T.table_collation FROM information_schema.`TABLES` T LEFT JOIN information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA ON (T.table_collation = CCSA.collation_name) WHERE T.table_schema = '%s' AND T.table_name = '%s'", database, table)).Scan(&characterSet, &collation)
if err != nil {
return "", "", err
}
return characterSet, collation, nil
}
func isUnusualLatin1(value string) bool {
for _, r := range value {
if r > 255 || (r >= 128 && r <= 159) {
return true
}
}
return false
}
func isUnusualCP1252(value string) bool {
problematicChars := map[rune]bool{129: true, 141: true, 143: true, 144: true, 157: true}
for _, r := range value {
if r > 255 || problematicChars[r] {
return true
}
}
return false
}
func getPrimaryKeys(db *sql.DB, database, table string) ([]string, error) {
query := `SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND CONSTRAINT_NAME = 'PRIMARY'`
rows, err := db.Query(query, database, table)
if err != nil {
return nil, err
}
defer rows.Close()
var keys []string
for rows.Next() {
var key string
if err := rows.Scan(&key); err != nil {
return nil, err
}
keys = append(keys, key)
}
return keys, nil
}
func scanTableForIssues(db *sql.DB, database, table string) error {
startTime := time.Now()
batchSize := 80000
maxRetries := 5
offendingIDs := make(map[string][]int) // Map to store column name -> slice of IDs
// Get primary keys
primaryKeys, err := getPrimaryKeys(db, database, table)
if err != nil {
return err
}
if len(primaryKeys) == 0 {
return fmt.Errorf("table %s has no primary key", table)
}
// Get the min and max IDs
primaryKeyStr := "`" + strings.Join(primaryKeys, "`, `") + "`"
var minID, maxID int
query := fmt.Sprintf("SELECT MIN(%s), MAX(%s) FROM %s.%s", primaryKeyStr, primaryKeyStr, database, table)
err = db.QueryRow(query).Scan(&minID, &maxID)
if err != nil {
return err
}
// Get text columns
columnsQuery := `SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?
AND DATA_TYPE IN ('char', 'varchar', 'text', 'tinytext', 'mediumtext', 'longtext')`
columns, err := db.Query(columnsQuery, database, table)
if err != nil {
return err
}
defer columns.Close()
var columnNames []string
for columns.Next() {
var colName string
if err := columns.Scan(&colName); err != nil {
return err
}
columnNames = append(columnNames, colName)
}
// Process in batches
for start := minID; start <= maxID; start += batchSize {
end := start + batchSize - 1
if end > maxID {
end = maxID
}
for _, column := range columnNames {
for retry := 0; retry < maxRetries; retry++ {
query := fmt.Sprintf(`SELECT %s, %s FROM %s.%s WHERE %s BETWEEN ? AND ?`,
primaryKeyStr, column, database, table, primaryKeyStr)
rows, err := db.Query(query, start, end)
if err != nil {
if retry == maxRetries-1 {
return err
}
time.Sleep(time.Second * time.Duration(retry+1))
continue
}
defer rows.Close()
for rows.Next() {
var id int
var value sql.NullString
if err := rows.Scan(&id, &value); err != nil {
return err
}
if value.Valid {
if isUnusualLatin1(value.String) || isUnusualCP1252(value.String) {
offendingIDs[column] = append(offendingIDs[column], id)
}
}
}
break
}
}
}
// Print results for each column with issues
for column, ids := range offendingIDs {
if len(ids) > 0 {
fmt.Printf("\nCurrent table: %s\n", color.BlueString(table))
fmt.Printf("Column: %s\n", color.BlueString(column))
fmt.Printf("Count of records that need to be fixed: %s\n\n", color.RedString(fmt.Sprintf("%d", len(ids))))
fmt.Println("Offending IDs:")
fmt.Printf("%v\n\n", ids)
}
}
elapsed := time.Since(startTime)
fmt.Printf("Time taken: %v seconds\n", elapsed.Seconds())
return nil
}
func main() {
// Parse the command line flags
flag.Parse()
// Check if the source flag is set
if *source == "" {
fmt.Println("Usage: go-utf8 -s <source host> [-d <database name>] [-show] [-t <table>] [-scan]")
fmt.Println("Please specify a source host")
os.Exit(1)
}
// read the ~/.my.cnf file to get the database credentials
readMyCnf()
// Connect to MySQL database using the credentials from the ~/.my.cnf file and the function above connectToDatabase
db, err := connectToDatabase(*source)
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Show databases if the -show flag is set
if *show {
rows, err := db.Query("SHOW DATABASES")
if err != nil {
panic(err.Error())
}
defer rows.Close()
for rows.Next() {
var dbName string
err := rows.Scan(&dbName)
if err != nil {
panic(err.Error())
}
fmt.Println(dbName)
}
return
}
// Check if the database flag is set
if *database == "" {
fmt.Println("Usage: go-utf8 -s <source host> -d <database name> [-t <table>] [-scan]")
fmt.Println("Please specify a database name")
os.Exit(1)
}
// Handle table info request (existing behavior)
if *database != "" && *table != "" && !*scanTable {
characterSet, collation, err := getTableCollationCharacterSet(db, *database, *table)
if err != nil {
panic(err.Error())
}
fmt.Println("Default character set:", color.RedString(characterSet))
fmt.Println("Default collation:", color.RedString(collation))
fmt.Println()
return
}
// Handle specific table scanning
if *database != "" && *table != "" && *scanTable {
if err := scanTableForIssues(db, *database, *table); err != nil {
log.Fatal(err)
}
return
}
// Get a list of tables to loop through
rows, err := db.Query(fmt.Sprintf("SHOW TABLES FROM %s", *database))
if err != nil {
log.Fatal(err)
}
defer rows.Close()
// Loop through the tables
for rows.Next() {
var tableName string
err := rows.Scan(&tableName)
if err != nil {
log.Fatal(err)
}
// Get a list of columns to loop through
columns, err := db.Query(fmt.Sprintf("SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '%s' AND table_schema = '%s' ORDER BY ordinal_position", tableName, *database))
if err != nil {
log.Fatal(err)
}
defer columns.Close()
// Loop through the columns
for columns.Next() {
var columnName string
err := columns.Scan(&columnName)
if err != nil {
log.Fatal(err)
}
// Query the column for records that need to be fixed
query := fmt.Sprintf("SELECT COUNT(*) FROM `%s`.`%s` WHERE LENGTH(`%s`) != CHAR_LENGTH(`%s`) AND `%s` IS NOT NULL", *database, tableName, columnName, columnName, columnName)
var count int
err = db.QueryRow(query).Scan(&count)
if err != nil {
log.Fatal(err)
}
// Print the result
if count > 0 {
fmt.Println()
fmt.Printf("Current table: %s\n", color.BlueString(tableName))
fmt.Printf("Column: %s\n", color.BlueString(columnName))
fmt.Printf("Count of records that need to be fixed: %s\n", color.RedString(fmt.Sprintf("%d", count)))
fmt.Println()
}
}
// Get the total number of rows in the table
var totalRows int
err = db.QueryRow(fmt.Sprintf("SELECT COUNT(*) FROM `%s`.`%s`", *database, tableName)).Scan(&totalRows)
if err != nil {
log.Fatal(err)
}
/*
// Create a progress bar
bar := progressbar.NewOptions(int(totalRows),
progressbar.OptionSetRenderBlankState(true),
progressbar.OptionSetWidth(15),
progressbar.OptionSetDescription(*database),
progressbar.OptionSetTheme(progressbar.Theme{
Saucer: "=",
SaucerPadding: " ",
BarStart: "[",
BarEnd: "]",
}),
)
*/
// Loop through the columns
for columns.Next() {
var columnName string
err := columns.Scan(&columnName)
if err != nil {
log.Fatal(err)
}
// Loop through the columns and check if they contain utf8 encoded data
rows, err := db.Query(fmt.Sprintf("SELECT `%s` FROM `%s`.`%s`", columnName, *database, tableName))
if err != nil {
panic(err.Error())
}
defer rows.Close()
for rows.Next() {
var value sql.NullString
err := rows.Scan(&value)
if err != nil {
panic(err.Error())
}
// Check if the value contains non-utf8 characters
if value.Valid {
if !utf8.ValidString(value.String) {
fmt.Println()
fmt.Printf("Non-UTF8 character found in table: %s, column: %s, value: %s\n", color.BlueString(tableName), color.BlueString(columnName), color.RedString(value.String))
fmt.Println()
}
}
// Increment the progress bar
//bar.Add(1)
}
// Increment the progress bar
//bar.Add(1)
//fmt.Println()
}
/*
// Query the information schema to get the tables and their character set and collation
tables, err := db.Query(fmt.Sprintf("SELECT table_name, TABLE_COLLATION FROM information_schema.tables WHERE table_schema = '%s'", *database))
if err != nil {
panic(err.Error())
}
defer tables.Close()
*/
/*
// Print the default character set and collation for each table
for tables.Next() {
var tableName string
var tableCollation string
err := tables.Scan(&tableName, &tableCollation)
if err != nil {
panic(err.Error())
}
fmt.Println()
fmt.Printf("Table: %s, Collation: %s\n", color.BlueString(tableName), color.RedString(tableCollation))
}
*/
/*
// Query the information schema to get the default character set for the table
var characterSet string
err = db.QueryRow(fmt.Sprintf("SELECT CCSA.character_set_name FROM information_schema.`TABLES` T LEFT JOIN information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA ON (T.table_collation = CCSA.collation_name) WHERE T.table_schema = '%s' AND T.table_name = '%s'", *database, tableName)).Scan(&characterSet)
if err != nil {
panic(err.Error())
}
fmt.Println("Default character set:", color.RedString(characterSet))
fmt.Println()
// end
*/
if *database != "" && *table != "" {
// call the function getTableCollationCharacterSet to get the character set and collation for the table
characterSet, collation, err := getTableCollationCharacterSet(db, *database, *table)
if err != nil {
panic(err.Error())
}
fmt.Println("Default character set:", color.RedString(characterSet))
fmt.Println("Default collation:", color.RedString(collation))
fmt.Println()
os.Exit(0) // Exit the program after printing the character set and collation
}
}
}