Skip to content

Commit ef8f762

Browse files
authored
refactor: distinguish between Unique and UniqueIndex (#106)
* refactor: distinguish between Unique and UniqueIndex * update gorm to master latest for go-gorm/gorm#6386
1 parent 34e811a commit ef8f762

File tree

3 files changed

+59
-7
lines changed

3 files changed

+59
-7
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ go 1.14
44

55
require (
66
github.com/microsoft/go-mssqldb v1.6.0
7-
gorm.io/gorm v1.25.2-0.20230610234218-206613868439
7+
gorm.io/gorm v1.25.7-0.20240204074919-46816ad31dde
88
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,5 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
136136
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
137137
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
138138
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
139-
gorm.io/gorm v1.25.2-0.20230610234218-206613868439 h1:LerZWOlV0e/6u9dEoVhe7Ehg+o7QvQKIxrI4Ccb2aV8=
140-
gorm.io/gorm v1.25.2-0.20230610234218-206613868439/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
139+
gorm.io/gorm v1.25.7-0.20240204074919-46816ad31dde h1:9DShaph9qhkIYw7QF91I/ynrr4cOO2PZra2PFD7Mfeg=
140+
gorm.io/gorm v1.25.7-0.20240204074919-46816ad31dde/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=

migrator.go

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,22 @@ import (
1212
"gorm.io/gorm/schema"
1313
)
1414

15+
const indexSQL = `
16+
SELECT
17+
i.name AS index_name,
18+
i.is_unique,
19+
i.is_primary_key,
20+
col.name AS column_name
21+
FROM
22+
sys.indexes i
23+
LEFT JOIN sys.index_columns ic ON ic.object_id = i.object_id AND ic.index_id = i.index_id
24+
LEFT JOIN sys.all_columns col ON col.column_id = ic.column_id AND col.object_id = ic.object_id
25+
WHERE
26+
i.name IS NOT NULL
27+
AND i.is_unique_constraint = 0
28+
AND i.object_id = OBJECT_ID(?)
29+
`
30+
1531
type Migrator struct {
1632
migrator.Migrator
1733
}
@@ -348,14 +364,50 @@ func (m Migrator) RenameIndex(value interface{}, oldName, newName string) error
348364
})
349365
}
350366

367+
type Index struct {
368+
TableName string
369+
ColumnName string
370+
IndexName string
371+
IsUnique sql.NullBool
372+
IsPrimaryKey sql.NullBool
373+
}
374+
375+
func (m Migrator) GetIndexes(value interface{}) ([]gorm.Index, error) {
376+
indexes := make([]gorm.Index, 0)
377+
err := m.RunWithValue(value, func(stmt *gorm.Statement) error {
378+
result := make([]*Index, 0)
379+
if err := m.DB.Raw(indexSQL, stmt.Table).Scan(&result).Error; err != nil {
380+
return err
381+
}
382+
indexMap := make(map[string]*migrator.Index)
383+
for _, r := range result {
384+
idx, ok := indexMap[r.IndexName]
385+
if !ok {
386+
idx = &migrator.Index{
387+
TableName: stmt.Table,
388+
NameValue: r.IndexName,
389+
ColumnList: nil,
390+
PrimaryKeyValue: r.IsPrimaryKey,
391+
UniqueValue: r.IsUnique,
392+
}
393+
}
394+
idx.ColumnList = append(idx.ColumnList, r.ColumnName)
395+
indexMap[r.IndexName] = idx
396+
}
397+
for _, idx := range indexMap {
398+
indexes = append(indexes, idx)
399+
}
400+
return nil
401+
})
402+
return indexes, err
403+
}
404+
351405
func (m Migrator) HasConstraint(value interface{}, name string) bool {
352406
var count int64
353407
m.RunWithValue(value, func(stmt *gorm.Statement) error {
354-
constraint, chk, table := m.GuessConstraintAndTable(stmt, name)
408+
constraint, table := m.GuessConstraintInterfaceAndTable(stmt, name)
355409
if constraint != nil {
356-
name = constraint.Name
357-
} else if chk != nil {
358-
name = chk.Name
410+
name = constraint.GetName()
359411
}
360412

361413
tableCatalog, schema, tableName := splitFullQualifiedName(table)

0 commit comments

Comments
 (0)