forked from mirror/gorm
44 lines
1.1 KiB
Go
44 lines
1.1 KiB
Go
|
package mysql
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
|
||
|
"github.com/jinzhu/gorm"
|
||
|
"github.com/jinzhu/gorm/clause"
|
||
|
"github.com/jinzhu/gorm/migrator"
|
||
|
)
|
||
|
|
||
|
type Migrator struct {
|
||
|
migrator.Migrator
|
||
|
}
|
||
|
|
||
|
func (m Migrator) AlterColumn(value interface{}, field string) error {
|
||
|
return m.RunWithValue(value, func(stmt *gorm.Statement) error {
|
||
|
if field := stmt.Schema.LookUpField(field); field != nil {
|
||
|
return m.DB.Exec(
|
||
|
"ALTER TABLE ? MODIFY COLUMN ? TYPE ?",
|
||
|
clause.Table{Name: stmt.Table}, clause.Column{Name: field.DBName}, clause.Expr{SQL: field.DBDataType},
|
||
|
).Error
|
||
|
}
|
||
|
return fmt.Errorf("failed to look up field with name: %s", field)
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func (m Migrator) DropConstraint(value interface{}, name string) error {
|
||
|
return m.RunWithValue(value, func(stmt *gorm.Statement) error {
|
||
|
for _, chk := range stmt.Schema.ParseCheckConstraints() {
|
||
|
if chk.Name == name {
|
||
|
return m.DB.Exec(
|
||
|
"ALTER TABLE ? DROP CHECK ?",
|
||
|
clause.Table{Name: stmt.Table}, clause.Column{Name: name},
|
||
|
).Error
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return m.DB.Exec(
|
||
|
"ALTER TABLE ? DROP FOREIGN KEY ?",
|
||
|
clause.Table{Name: stmt.Table}, clause.Column{Name: name},
|
||
|
).Error
|
||
|
})
|
||
|
}
|