88
99 "github.com/go-mysql-org/go-mysql/client"
1010 _ "github.com/go-mysql-org/go-mysql/driver"
11- "github.com/go-mysql-org/go-mysql/mysql"
1211 "github.com/go-mysql-org/go-mysql/test_util"
1312 "github.com/stretchr/testify/require"
1413 "github.com/stretchr/testify/suite"
@@ -180,19 +179,7 @@ func (s *schemaTestSuite) TestSchemaWithInvisibleIndex() {
180179 _ , err := s .conn .Execute (`DROP TABLE IF EXISTS invisible_idx_test` )
181180 require .NoError (s .T (), err )
182181
183- // Check MySQL version
184- hasInvisibleIndex := false
185- versionQuery := "SELECT VERSION()"
186- r , err := s .conn .Execute (versionQuery )
187- require .NoError (s .T (), err )
188-
189- if r .RowNumber () > 0 {
190- version , _ := r .GetString (0 , 0 )
191- if eq , err := mysql .CompareServerVersions (version , "8.0.0" ); err == nil && eq >= 0 {
192- hasInvisibleIndex = true
193- }
194- }
195-
182+ // Create table first to check invisible index support via column presence
196183 str := `
197184 CREATE TABLE IF NOT EXISTS invisible_idx_test (
198185 id INT,
@@ -207,7 +194,12 @@ func (s *schemaTestSuite) TestSchemaWithInvisibleIndex() {
207194 _ , err = s .conn .Execute (str )
208195 require .NoError (s .T (), err )
209196
210- // Add INVISIBLE keyword only for MySQL 8.0+
197+ // Check if invisible index support exists by checking SHOW INDEX columns
198+ r , err := s .conn .Execute (fmt .Sprintf ("SHOW INDEX FROM `%s`.`%s`" , * schema , "invisible_idx_test" ))
199+ require .NoError (s .T (), err )
200+ hasInvisibleIndex := hasInvisibleIndexSupportFromResult (r )
201+
202+ // Add INVISIBLE keyword only if database supports it
211203 if hasInvisibleIndex {
212204 _ , err = s .conn .Execute (`ALTER TABLE invisible_idx_test ALTER INDEX name_idx INVISIBLE` )
213205 require .NoError (s .T (), err )
@@ -225,9 +217,10 @@ func (s *schemaTestSuite) TestSchemaWithInvisibleIndex() {
225217 // Find name_idx and email_idx (order may vary)
226218 var nameIdx , emailIdx * Index
227219 for _ , idx := range ta .Indexes {
228- if idx .Name == "name_idx" {
220+ switch idx .Name {
221+ case "name_idx" :
229222 nameIdx = idx
230- } else if idx . Name == "email_idx" {
223+ case "email_idx" :
231224 emailIdx = idx
232225 }
233226 }
@@ -238,11 +231,11 @@ func (s *schemaTestSuite) TestSchemaWithInvisibleIndex() {
238231 // email_idx should always be visible (default)
239232 require .True (s .T (), emailIdx .Visible )
240233
241- // name_idx visibility depends on MySQL version
234+ // name_idx visibility depends on database support for invisible indexes
242235 if hasInvisibleIndex {
243- require .False (s .T (), nameIdx .Visible , "name_idx should be invisible in MySQL 8.0+ " )
236+ require .False (s .T (), nameIdx .Visible , "name_idx should be invisible when database supports invisible indexes " )
244237 } else {
245- require .True (s .T (), nameIdx .Visible , "name_idx should be visible in MySQL <8.0 " )
238+ require .True (s .T (), nameIdx .Visible , "name_idx should be visible when database doesn't support invisible indexes " )
246239 }
247240
248241 taSqlDb , err := NewTableFromSqlDB (s .sqlDB , * schema , "invisible_idx_test" )
@@ -251,6 +244,65 @@ func (s *schemaTestSuite) TestSchemaWithInvisibleIndex() {
251244 require .Equal (s .T (), ta , taSqlDb )
252245}
253246
247+ func (s * schemaTestSuite ) TestInvisibleIndexColumnDetection () {
248+ _ , err := s .conn .Execute (`DROP TABLE IF EXISTS column_detection_test` )
249+ require .NoError (s .T (), err )
250+
251+ str := `
252+ CREATE TABLE IF NOT EXISTS column_detection_test (
253+ id INT PRIMARY KEY,
254+ name VARCHAR(256),
255+ INDEX name_idx (name)
256+ ) ENGINE = INNODB;
257+ `
258+
259+ _ , err = s .conn .Execute (str )
260+ require .NoError (s .T (), err )
261+
262+ // Test both detection functions work consistently
263+ r , err := s .conn .Execute (fmt .Sprintf ("SHOW INDEX FROM `%s`.`%s`" , * schema , "column_detection_test" ))
264+ require .NoError (s .T (), err )
265+
266+ hasInvisibleFromResult := hasInvisibleIndexSupportFromResult (r )
267+
268+ // Get columns and test the other detection function
269+ cols , err := s .sqlDB .Query (fmt .Sprintf ("SHOW INDEX FROM `%s`.`%s`" , * schema , "column_detection_test" ))
270+ require .NoError (s .T (), err )
271+ defer cols .Close ()
272+
273+ columnNames , err := cols .Columns ()
274+ require .NoError (s .T (), err )
275+ hasInvisibleFromColumns := hasInvisibleIndexSupportFromColumns (columnNames )
276+
277+ // Both detection methods should agree
278+ require .Equal (s .T (), hasInvisibleFromResult , hasInvisibleFromColumns , "Detection methods should be consistent" )
279+
280+ // Test that both connection types work identically
281+ ta1 , err := NewTable (s .conn , * schema , "column_detection_test" )
282+ require .NoError (s .T (), err )
283+
284+ ta2 , err := NewTableFromSqlDB (s .sqlDB , * schema , "column_detection_test" )
285+ require .NoError (s .T (), err )
286+
287+ require .Equal (s .T (), ta1 , ta2 , "Both connection types should produce identical results" )
288+ }
289+
290+ func TestInvisibleIndexLogic (t * testing.T ) {
291+ // Test MySQL-style visibility logic
292+ require .True (t , isIndexInvisible ("NO" ), "Visible=NO should be invisible" )
293+ require .False (t , isIndexInvisible ("YES" ), "Visible=YES should be visible" )
294+
295+ // Test case insensitivity
296+ require .True (t , isIndexInvisible ("no" ), "Should be case insensitive" )
297+ require .True (t , isIndexInvisible ("No" ), "Should be case insensitive" )
298+ require .False (t , isIndexInvisible ("yes" ), "Should be case insensitive" )
299+ require .False (t , isIndexInvisible ("YES" ), "Should be case insensitive" )
300+
301+ // Test other values default to visible
302+ require .False (t , isIndexInvisible ("" ), "Empty string should default to visible" )
303+ require .False (t , isIndexInvisible ("UNKNOWN" ), "Unknown value should default to visible" )
304+ }
305+
254306func TestIndexVisibilityDefault (t * testing.T ) {
255307 // Test that NewIndex creates visible indexes by default
256308 idx := NewIndex ("test_index" )
0 commit comments