8
8
9
9
"github.com/go-mysql-org/go-mysql/client"
10
10
_ "github.com/go-mysql-org/go-mysql/driver"
11
- "github.com/go-mysql-org/go-mysql/mysql"
12
11
"github.com/go-mysql-org/go-mysql/test_util"
13
12
"github.com/stretchr/testify/require"
14
13
"github.com/stretchr/testify/suite"
@@ -180,19 +179,7 @@ func (s *schemaTestSuite) TestSchemaWithInvisibleIndex() {
180
179
_ , err := s .conn .Execute (`DROP TABLE IF EXISTS invisible_idx_test` )
181
180
require .NoError (s .T (), err )
182
181
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
196
183
str := `
197
184
CREATE TABLE IF NOT EXISTS invisible_idx_test (
198
185
id INT,
@@ -207,7 +194,12 @@ func (s *schemaTestSuite) TestSchemaWithInvisibleIndex() {
207
194
_ , err = s .conn .Execute (str )
208
195
require .NoError (s .T (), err )
209
196
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
211
203
if hasInvisibleIndex {
212
204
_ , err = s .conn .Execute (`ALTER TABLE invisible_idx_test ALTER INDEX name_idx INVISIBLE` )
213
205
require .NoError (s .T (), err )
@@ -225,9 +217,10 @@ func (s *schemaTestSuite) TestSchemaWithInvisibleIndex() {
225
217
// Find name_idx and email_idx (order may vary)
226
218
var nameIdx , emailIdx * Index
227
219
for _ , idx := range ta .Indexes {
228
- if idx .Name == "name_idx" {
220
+ switch idx .Name {
221
+ case "name_idx" :
229
222
nameIdx = idx
230
- } else if idx . Name == "email_idx" {
223
+ case "email_idx" :
231
224
emailIdx = idx
232
225
}
233
226
}
@@ -238,11 +231,11 @@ func (s *schemaTestSuite) TestSchemaWithInvisibleIndex() {
238
231
// email_idx should always be visible (default)
239
232
require .True (s .T (), emailIdx .Visible )
240
233
241
- // name_idx visibility depends on MySQL version
234
+ // name_idx visibility depends on database support for invisible indexes
242
235
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 " )
244
237
} 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 " )
246
239
}
247
240
248
241
taSqlDb , err := NewTableFromSqlDB (s .sqlDB , * schema , "invisible_idx_test" )
@@ -251,6 +244,65 @@ func (s *schemaTestSuite) TestSchemaWithInvisibleIndex() {
251
244
require .Equal (s .T (), ta , taSqlDb )
252
245
}
253
246
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
+
254
306
func TestIndexVisibilityDefault (t * testing.T ) {
255
307
// Test that NewIndex creates visible indexes by default
256
308
idx := NewIndex ("test_index" )
0 commit comments