Skip to content

Commit 5855869

Browse files
committed
add visible to index
1 parent 639eccd commit 5855869

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

schema/schema.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ type Index struct {
6060
Columns []string
6161
Cardinality []uint64
6262
NoneUnique uint64
63+
Visible bool
6364
}
6465

6566
type Table struct {
@@ -209,7 +210,7 @@ func (ta *Table) AddIndex(name string) (index *Index) {
209210
}
210211

211212
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}
213214
}
214215

215216
func (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+
322340
func (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()

schema/schema_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"database/sql"
55
"flag"
66
"fmt"
7+
"strings"
78
"testing"
89

910
"github.com/go-mysql-org/go-mysql/client"
@@ -174,3 +175,50 @@ func (s *schemaTestSuite) TestSchemaWithMultiValueIndex() {
174175

175176
require.Equal(s.T(), ta, taSqlDb)
176177
}
178+
179+
// func (s *schemaTestSuite) TestSchemaWithInvisibleIndex() {
180+
// _, err := s.conn.Execute(`DROP TABLE IF EXISTS invisible_idx_test`)
181+
// require.NoError(s.T(), err)
182+
183+
// // Check MySQL version
184+
// hasInvisibleIndex := false
185+
// if eq, err := s.conn.CompareServerVersion("8.0.0"); err == nil && eq >= 0 {
186+
// hasInvisibleIndex = true
187+
// }
188+
189+
// str := `
190+
// CREATE TABLE IF NOT EXISTS invisible_idx_test (
191+
// id INT,
192+
// name VARCHAR(256),
193+
// PRIMARY KEY(id),
194+
// INDEX name_idx (name)
195+
// ) ENGINE = INNODB;
196+
// `
197+
198+
// // Add INVISIBLE keyword only for MySQL 8.0+
199+
// if hasInvisibleIndex {
200+
// str = strings.Replace(str, "INDEX name_idx (name)", "INDEX name_idx (name) INVISIBLE", 1)
201+
// }
202+
203+
// _, err = s.conn.Execute(str)
204+
// require.NoError(s.T(), err)
205+
206+
// ta, err := NewTable(s.conn, *schema, "invisible_idx_test")
207+
// require.NoError(s.T(), err)
208+
209+
// require.Len(s.T(), ta.Indexes, 2)
210+
// require.Equal(s.T(), "PRIMARY", ta.Indexes[0].Name)
211+
// require.True(s.T(), ta.Indexes[0].Visible)
212+
// require.Equal(s.T(), "name_idx", ta.Indexes[1].Name)
213+
214+
// if hasInvisibleIndex {
215+
// require.False(s.T(), ta.Indexes[1].Visible)
216+
// } else {
217+
// require.True(s.T(), ta.Indexes[1].Visible)
218+
// }
219+
220+
// taSqlDb, err := NewTableFromSqlDB(s.sqlDB, *schema, "invisible_idx_test")
221+
// require.NoError(s.T(), err)
222+
223+
// require.Equal(s.T(), ta, taSqlDb)
224+
// }

0 commit comments

Comments
 (0)