Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: distinguish between Unique and UniqueIndex #106

Merged
merged 4 commits into from
Feb 6, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 56 additions & 4 deletions migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ import (
"gorm.io/gorm/schema"
)

const indexSQL = `
SELECT
i.name AS index_name,
i.is_unique,
i.is_primary_key,
col.name AS column_name
FROM
sys.indexes i
LEFT JOIN sys.index_columns ic ON ic.object_id = i.object_id AND ic.index_id = i.index_id
LEFT JOIN sys.all_columns col ON col.column_id = ic.column_id AND col.object_id = ic.object_id
WHERE
i.name IS NOT NULL
AND i.is_unique_constraint = 0
AND i.object_id = OBJECT_ID(?)
`

type Migrator struct {
migrator.Migrator
}
Expand Down Expand Up @@ -348,14 +364,50 @@ func (m Migrator) RenameIndex(value interface{}, oldName, newName string) error
})
}

type Index struct {
TableName string
ColumnName string
IndexName string
IsUnique sql.NullBool
IsPrimaryKey sql.NullBool
}

func (m Migrator) GetIndexes(value interface{}) ([]gorm.Index, error) {
indexes := make([]gorm.Index, 0)
err := m.RunWithValue(value, func(stmt *gorm.Statement) error {
result := make([]*Index, 0)
if err := m.DB.Raw(indexSQL, stmt.Table).Scan(&result).Error; err != nil {
return err
}
indexMap := make(map[string]*migrator.Index)
for _, r := range result {
idx, ok := indexMap[r.IndexName]
if !ok {
idx = &migrator.Index{
TableName: stmt.Table,
NameValue: r.IndexName,
ColumnList: nil,
PrimaryKeyValue: r.IsPrimaryKey,
UniqueValue: r.IsUnique,
}
}
idx.ColumnList = append(idx.ColumnList, r.ColumnName)
indexMap[r.IndexName] = idx
}
for _, idx := range indexMap {
indexes = append(indexes, idx)
}
return nil
})
return indexes, err
}

func (m Migrator) HasConstraint(value interface{}, name string) bool {
var count int64
m.RunWithValue(value, func(stmt *gorm.Statement) error {
constraint, chk, table := m.GuessConstraintAndTable(stmt, name)
constraint, table := m.GuessConstraintInterfaceAndTable(stmt, name)
if constraint != nil {
name = constraint.Name
} else if chk != nil {
name = chk.Name
name = constraint.GetName()
}

tableCatalog, schema, tableName := splitFullQualifiedName(table)
Expand Down