Skip to content

Commit 4f2c3fe

Browse files
authored
fix: GetIndexes method returns zero values (#149)
due to the mismatch between the `Index` struct field order and the `indexSQL` field order
1 parent cc3a5ff commit 4f2c3fe

File tree

2 files changed

+60
-7
lines changed

2 files changed

+60
-7
lines changed

migrator.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ import (
1515

1616
const indexSQL = `
1717
SELECT
18+
col.name AS column_name,
1819
i.name AS index_name,
1920
i.is_unique,
20-
i.is_primary_key,
21-
col.name AS column_name
21+
i.is_primary_key
2222
FROM
2323
sys.indexes i
2424
LEFT JOIN sys.index_columns ic ON ic.object_id = i.object_id AND ic.index_id = i.index_id
@@ -496,11 +496,10 @@ func (m Migrator) RenameIndex(value interface{}, oldName, newName string) error
496496
}
497497

498498
type Index struct {
499-
TableName string
500-
ColumnName string
501-
IndexName string
502-
IsUnique sql.NullBool
503-
IsPrimaryKey sql.NullBool
499+
ColumnName string `gorm:"column:column_name"`
500+
IndexName string `gorm:"column:index_name"`
501+
IsUnique sql.NullBool `gorm:"column:is_unique"`
502+
IsPrimaryKey sql.NullBool `gorm:"column:is_primary_key"`
504503
}
505504

506505
func (m Migrator) GetIndexes(value interface{}) ([]gorm.Index, error) {

migrator_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sqlserver_test
22

33
import (
4+
"encoding/json"
45
"os"
56
"reflect"
67
"testing"
@@ -249,3 +250,56 @@ func TestMigrator_MigrateColumnComment(t *testing.T) {
249250
t.Logf("got comments: %#v", gotComments)
250251
}
251252
}
253+
254+
func TestMigrator_GetIndexes(t *testing.T) {
255+
db, err := gorm.Open(sqlserver.Open(sqlserverDSN))
256+
if err != nil {
257+
t.Fatal(err)
258+
}
259+
dm := db.Debug().Migrator()
260+
261+
type testTableIndex struct {
262+
Test uint64 `gorm:"index"`
263+
}
264+
type testTableUnique struct {
265+
ID string `gorm:"index:unique_id,class:UNIQUE,where:id IS NOT NULL"`
266+
}
267+
type testTablePrimaryKey struct {
268+
ID string `gorm:"primaryKey"`
269+
}
270+
271+
type args struct {
272+
value interface{}
273+
}
274+
tests := []struct {
275+
name string
276+
args args
277+
wantErr bool
278+
}{
279+
{name: "index", args: args{value: new(testTableIndex)}},
280+
{name: "unique", args: args{value: new(testTableUnique)}},
281+
{name: "primaryKey", args: args{value: new(testTablePrimaryKey)}},
282+
}
283+
for _, tt := range tests {
284+
t.Run(tt.name, func(t *testing.T) {
285+
if err = dm.AutoMigrate(tt.args.value); err != nil {
286+
t.Error(err)
287+
}
288+
got, gotErr := dm.GetIndexes(tt.args.value)
289+
if (gotErr != nil) != tt.wantErr {
290+
t.Errorf("GetIndexes() error = %v, wantErr %v", gotErr, tt.wantErr)
291+
return
292+
}
293+
for _, index := range got {
294+
_, validUnique := index.Unique()
295+
_, validPK := index.PrimaryKey()
296+
indexBytes, _ := json.Marshal(index)
297+
if index.Name() == "" && !validUnique && !validPK {
298+
t.Errorf("GetIndexes() got = %s empty", indexBytes)
299+
} else {
300+
t.Logf("GetIndexes() got = %s", indexBytes)
301+
}
302+
}
303+
})
304+
}
305+
}

0 commit comments

Comments
 (0)