@@ -60,6 +60,7 @@ type Index struct {
6060 Columns []string
6161 Cardinality []uint64
6262 NoneUnique uint64
63+ Visible bool
6364}
6465
6566type Table struct {
@@ -209,7 +210,7 @@ func (ta *Table) AddIndex(name string) (index *Index) {
209210}
210211
211212func 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 }
213214}
214215
215216func (idx * Index ) AddColumn (name string , cardinality uint64 ) {
@@ -319,6 +320,23 @@ func (ta *Table) fetchColumnsViaSqlDB(conn *sql.DB) error {
319320 return r .Err ()
320321}
321322
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+
322340func (ta * Table ) fetchIndexes (conn mysql.Executer ) error {
323341 r , err := conn .Execute (fmt .Sprintf ("show index from `%s`.`%s`" , ta .Schema , ta .Name ))
324342 if err != nil {
@@ -327,6 +345,8 @@ func (ta *Table) fetchIndexes(conn mysql.Executer) error {
327345 var currentIndex * Index
328346 currentName := ""
329347
348+ hasInvisibleIndex := hasInvisibleIndexSupport (conn )
349+
330350 for i := 0 ; i < r .RowNumber (); i ++ {
331351 indexName , _ := r .GetString (i , 2 )
332352 if currentName != indexName {
@@ -337,6 +357,14 @@ func (ta *Table) fetchIndexes(conn mysql.Executer) error {
337357 colName , _ := r .GetString (i , 4 )
338358 currentIndex .AddColumn (colName , cardinality )
339359 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+ }
340368 }
341369
342370 return ta .fetchPrimaryKeyColumns ()
@@ -355,11 +383,14 @@ func (ta *Table) fetchIndexesViaSqlDB(conn *sql.DB) error {
355383
356384 var unusedVal interface {}
357385
386+ hasInvisibleIndex := hasInvisibleIndexSupportSqlDB (conn )
387+
358388 for r .Next () {
359389 var indexName string
360390 var colName sql.NullString
361391 var noneUnique uint64
362392 var cardinality interface {}
393+ var visible sql.NullString
363394 cols , err := r .Columns ()
364395 if err != nil {
365396 return errors .Trace (err )
@@ -375,6 +406,8 @@ func (ta *Table) fetchIndexesViaSqlDB(conn *sql.DB) error {
375406 values [i ] = & colName
376407 case 6 :
377408 values [i ] = & cardinality
409+ case 10 :
410+ values [i ] = & visible
378411 default :
379412 values [i ] = & unusedVal
380413 }
@@ -397,6 +430,11 @@ func (ta *Table) fetchIndexesViaSqlDB(conn *sql.DB) error {
397430 currentIndex .AddColumn ("" , c )
398431 }
399432 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+ }
400438 }
401439
402440 return ta .fetchPrimaryKeyColumns ()
0 commit comments