Migrator AlterColumn with full data type

This commit is contained in:
Jinzhu 2022-02-09 17:39:01 +08:00
parent df2365057b
commit a0aceeb33e
2 changed files with 7 additions and 1 deletions

View File

@ -59,6 +59,7 @@ type Config struct {
cacheStore *sync.Map cacheStore *sync.Map
} }
// Apply update config to new config
func (c *Config) Apply(config *Config) error { func (c *Config) Apply(config *Config) error {
if config != c { if config != c {
*config = *c *config = *c
@ -66,6 +67,7 @@ func (c *Config) Apply(config *Config) error {
return nil return nil
} }
// AfterInitialize initialize plugins after db connected
func (c *Config) AfterInitialize(db *DB) error { func (c *Config) AfterInitialize(db *DB) error {
if db != nil { if db != nil {
for _, plugin := range c.Plugins { for _, plugin := range c.Plugins {
@ -77,6 +79,7 @@ func (c *Config) AfterInitialize(db *DB) error {
return nil return nil
} }
// Option gorm option interface
type Option interface { type Option interface {
Apply(*Config) error Apply(*Config) error
AfterInitialize(*DB) error AfterInitialize(*DB) error
@ -381,10 +384,12 @@ func (db *DB) getInstance() *DB {
return db return db
} }
// Expr returns clause.Expr, which can be used to pass SQL expression as params
func Expr(expr string, args ...interface{}) clause.Expr { func Expr(expr string, args ...interface{}) clause.Expr {
return clause.Expr{SQL: expr, Vars: args} return clause.Expr{SQL: expr, Vars: args}
} }
// SetupJoinTable setup join table schema
func (db *DB) SetupJoinTable(model interface{}, field string, joinTable interface{}) error { func (db *DB) SetupJoinTable(model interface{}, field string, joinTable interface{}) error {
var ( var (
tx = db.getInstance() tx = db.getInstance()
@ -435,6 +440,7 @@ func (db *DB) SetupJoinTable(model interface{}, field string, joinTable interfac
return nil return nil
} }
// Use use plugin
func (db *DB) Use(plugin Plugin) error { func (db *DB) Use(plugin Plugin) error {
name := plugin.Name() name := plugin.Name()
if _, ok := db.Plugins[name]; ok { if _, ok := db.Plugins[name]; ok {

View File

@ -337,7 +337,7 @@ func (m Migrator) DropColumn(value interface{}, name string) error {
func (m Migrator) AlterColumn(value interface{}, field string) error { func (m Migrator) AlterColumn(value interface{}, field string) error {
return m.RunWithValue(value, func(stmt *gorm.Statement) error { return m.RunWithValue(value, func(stmt *gorm.Statement) error {
if field := stmt.Schema.LookUpField(field); field != nil { if field := stmt.Schema.LookUpField(field); field != nil {
fileType := clause.Expr{SQL: m.DataTypeOf(field)} fileType := m.FullDataTypeOf(field)
return m.DB.Exec( return m.DB.Exec(
"ALTER TABLE ? ALTER COLUMN ? TYPE ?", "ALTER TABLE ? ALTER COLUMN ? TYPE ?",
m.CurrentTable(stmt), clause.Column{Name: field.DBName}, fileType, m.CurrentTable(stmt), clause.Column{Name: field.DBName}, fileType,