feat: define new methods
This commit is contained in:
parent
d2afedf5ba
commit
9048ea529c
@ -27,6 +27,8 @@ var regFullDataType = regexp.MustCompile(`\D*(\d+)\D?`)
|
|||||||
|
|
||||||
// TODO:? Create const vars for raw sql queries ?
|
// TODO:? Create const vars for raw sql queries ?
|
||||||
|
|
||||||
|
var _ gorm.Migrator = (*Migrator)(nil)
|
||||||
|
|
||||||
// Migrator m struct
|
// Migrator m struct
|
||||||
type Migrator struct {
|
type Migrator struct {
|
||||||
Config
|
Config
|
||||||
@ -539,6 +541,26 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m Migrator) MigrateColumnUnique(value interface{}, field *schema.Field, columnType gorm.ColumnType) error {
|
||||||
|
unique, ok := columnType.Unique()
|
||||||
|
if !ok || field.PrimaryKey {
|
||||||
|
return nil // skip primary key
|
||||||
|
}
|
||||||
|
// By default, ColumnType's Unique is not affected by UniqueIndex, so we don't care about UniqueIndex.
|
||||||
|
return m.RunWithValue(value, func(stmt *gorm.Statement) error {
|
||||||
|
// We're currently only receiving boolean values on `Unique` tag,
|
||||||
|
// so the UniqueConstraint name is fixed
|
||||||
|
constraint := m.DB.NamingStrategy.UniqueName(stmt.Table, field.DBName)
|
||||||
|
if unique && !field.Unique {
|
||||||
|
return m.DB.Migrator().DropConstraint(value, constraint)
|
||||||
|
}
|
||||||
|
if !unique && field.Unique {
|
||||||
|
return m.DB.Migrator().CreateConstraint(value, constraint)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// ColumnTypes return columnTypes []gorm.ColumnType and execErr error
|
// ColumnTypes return columnTypes []gorm.ColumnType and execErr error
|
||||||
func (m Migrator) ColumnTypes(value interface{}) ([]gorm.ColumnType, error) {
|
func (m Migrator) ColumnTypes(value interface{}) ([]gorm.ColumnType, error) {
|
||||||
columnTypes := make([]gorm.ColumnType, 0)
|
columnTypes := make([]gorm.ColumnType, 0)
|
||||||
|
@ -19,6 +19,7 @@ type Namer interface {
|
|||||||
RelationshipFKName(Relationship) string
|
RelationshipFKName(Relationship) string
|
||||||
CheckerName(table, column string) string
|
CheckerName(table, column string) string
|
||||||
IndexName(table, column string) string
|
IndexName(table, column string) string
|
||||||
|
UniqueName(table, column string) string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replacer replacer interface like strings.Replacer
|
// Replacer replacer interface like strings.Replacer
|
||||||
@ -26,6 +27,8 @@ type Replacer interface {
|
|||||||
Replace(name string) string
|
Replace(name string) string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _ Namer = (*NamingStrategy)(nil)
|
||||||
|
|
||||||
// NamingStrategy tables, columns naming strategy
|
// NamingStrategy tables, columns naming strategy
|
||||||
type NamingStrategy struct {
|
type NamingStrategy struct {
|
||||||
TablePrefix string
|
TablePrefix string
|
||||||
@ -85,6 +88,11 @@ func (ns NamingStrategy) IndexName(table, column string) string {
|
|||||||
return ns.formatName("idx", table, ns.toDBName(column))
|
return ns.formatName("idx", table, ns.toDBName(column))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UniqueName generate unique constraint name
|
||||||
|
func (ns NamingStrategy) UniqueName(table, column string) string {
|
||||||
|
return ns.formatName("uni", table, ns.toDBName(column))
|
||||||
|
}
|
||||||
|
|
||||||
func (ns NamingStrategy) formatName(prefix, table, name string) string {
|
func (ns NamingStrategy) formatName(prefix, table, name string) string {
|
||||||
formattedName := strings.ReplaceAll(strings.Join([]string{
|
formattedName := strings.ReplaceAll(strings.Join([]string{
|
||||||
prefix, table, name,
|
prefix, table, name,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user