From a0aceeb33e7eabbecae5b7fd2eef874b1a77b086 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Wed, 9 Feb 2022 17:39:01 +0800 Subject: [PATCH] Migrator AlterColumn with full data type --- gorm.go | 6 ++++++ migrator/migrator.go | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/gorm.go b/gorm.go index a982bee4..7967b094 100644 --- a/gorm.go +++ b/gorm.go @@ -59,6 +59,7 @@ type Config struct { cacheStore *sync.Map } +// Apply update config to new config func (c *Config) Apply(config *Config) error { if config != c { *config = *c @@ -66,6 +67,7 @@ func (c *Config) Apply(config *Config) error { return nil } +// AfterInitialize initialize plugins after db connected func (c *Config) AfterInitialize(db *DB) error { if db != nil { for _, plugin := range c.Plugins { @@ -77,6 +79,7 @@ func (c *Config) AfterInitialize(db *DB) error { return nil } +// Option gorm option interface type Option interface { Apply(*Config) error AfterInitialize(*DB) error @@ -381,10 +384,12 @@ func (db *DB) getInstance() *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 { return clause.Expr{SQL: expr, Vars: args} } +// SetupJoinTable setup join table schema func (db *DB) SetupJoinTable(model interface{}, field string, joinTable interface{}) error { var ( tx = db.getInstance() @@ -435,6 +440,7 @@ func (db *DB) SetupJoinTable(model interface{}, field string, joinTable interfac return nil } +// Use use plugin func (db *DB) Use(plugin Plugin) error { name := plugin.Name() if _, ok := db.Plugins[name]; ok { diff --git a/migrator/migrator.go b/migrator/migrator.go index 138917fb..80c4e2b3 100644 --- a/migrator/migrator.go +++ b/migrator/migrator.go @@ -337,7 +337,7 @@ func (m Migrator) DropColumn(value interface{}, name string) error { 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 { - fileType := clause.Expr{SQL: m.DataTypeOf(field)} + fileType := m.FullDataTypeOf(field) return m.DB.Exec( "ALTER TABLE ? ALTER COLUMN ? TYPE ?", m.CurrentTable(stmt), clause.Column{Name: field.DBName}, fileType,