@@ -60,6 +60,7 @@ type Index struct {
60
60
Columns []string
61
61
Cardinality []uint64
62
62
NoneUnique uint64
63
+ Visible bool
63
64
}
64
65
65
66
type Table struct {
@@ -209,7 +210,7 @@ func (ta *Table) AddIndex(name string) (index *Index) {
209
210
}
210
211
211
212
func NewIndex (name string ) * Index {
212
- return & Index {name , make ([]string , 0 , 8 ), make ([]uint64 , 0 , 8 ), 0 }
213
+ return & Index {name , make ([]string , 0 , 8 ), make ([]uint64 , 0 , 8 ), 0 , true }
213
214
}
214
215
215
216
func (idx * Index ) AddColumn (name string , cardinality uint64 ) {
@@ -319,6 +320,23 @@ func (ta *Table) fetchColumnsViaSqlDB(conn *sql.DB) error {
319
320
return r .Err ()
320
321
}
321
322
323
+ func hasInvisibleIndexSupport (conn mysql.Executer ) bool {
324
+ if eq , err := conn .CompareServerVersion ("8.0.0" ); err == nil && eq >= 0 {
325
+ return true
326
+ }
327
+ return false
328
+ }
329
+
330
+ func hasInvisibleIndexSupportSqlDB (conn * sql.DB ) bool {
331
+ versionQuery := "SELECT VERSION()"
332
+ var version string
333
+ err := conn .QueryRow (versionQuery ).Scan (& version )
334
+ if err == nil && strings .HasPrefix (version , "8." ) {
335
+ return true
336
+ }
337
+ return false
338
+ }
339
+
322
340
func (ta * Table ) fetchIndexes (conn mysql.Executer ) error {
323
341
r , err := conn .Execute (fmt .Sprintf ("show index from `%s`.`%s`" , ta .Schema , ta .Name ))
324
342
if err != nil {
@@ -327,6 +345,8 @@ func (ta *Table) fetchIndexes(conn mysql.Executer) error {
327
345
var currentIndex * Index
328
346
currentName := ""
329
347
348
+ hasInvisibleIndex := hasInvisibleIndexSupport (conn )
349
+
330
350
for i := 0 ; i < r .RowNumber (); i ++ {
331
351
indexName , _ := r .GetString (i , 2 )
332
352
if currentName != indexName {
@@ -337,6 +357,14 @@ func (ta *Table) fetchIndexes(conn mysql.Executer) error {
337
357
colName , _ := r .GetString (i , 4 )
338
358
currentIndex .AddColumn (colName , cardinality )
339
359
currentIndex .NoneUnique , _ = r .GetUint (i , 1 )
360
+
361
+ // Only set to false if explicitly marked as invisible
362
+ if hasInvisibleIndex {
363
+ visible , _ := r .GetString (i , 10 )
364
+ if visible == "NO" {
365
+ currentIndex .Visible = false
366
+ }
367
+ }
340
368
}
341
369
342
370
return ta .fetchPrimaryKeyColumns ()
@@ -355,11 +383,14 @@ func (ta *Table) fetchIndexesViaSqlDB(conn *sql.DB) error {
355
383
356
384
var unusedVal interface {}
357
385
386
+ hasInvisibleIndex := hasInvisibleIndexSupportSqlDB (conn )
387
+
358
388
for r .Next () {
359
389
var indexName string
360
390
var colName sql.NullString
361
391
var noneUnique uint64
362
392
var cardinality interface {}
393
+ var visible sql.NullString
363
394
cols , err := r .Columns ()
364
395
if err != nil {
365
396
return errors .Trace (err )
@@ -375,6 +406,8 @@ func (ta *Table) fetchIndexesViaSqlDB(conn *sql.DB) error {
375
406
values [i ] = & colName
376
407
case 6 :
377
408
values [i ] = & cardinality
409
+ case 10 :
410
+ values [i ] = & visible
378
411
default :
379
412
values [i ] = & unusedVal
380
413
}
@@ -397,6 +430,11 @@ func (ta *Table) fetchIndexesViaSqlDB(conn *sql.DB) error {
397
430
currentIndex .AddColumn ("" , c )
398
431
}
399
432
currentIndex .NoneUnique = noneUnique
433
+
434
+ // Only set to false if explicitly marked as invisible
435
+ if hasInvisibleIndex && visible .Valid && visible .String == "NO" {
436
+ currentIndex .Visible = false
437
+ }
400
438
}
401
439
402
440
return ta .fetchPrimaryKeyColumns ()
0 commit comments