Skip to content

Commit

Permalink
Migrator AlterColumn with full data type
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Feb 10, 2022
1 parent df23650 commit a0aceeb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 6 additions & 0 deletions gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ type Config struct {
cacheStore *sync.Map
}

// Apply update config to new config
func (c *Config) Apply(config *Config) error {
if config != c {
*config = *c
}
return nil
}

// AfterInitialize initialize plugins after db connected
func (c *Config) AfterInitialize(db *DB) error {
if db != nil {
for _, plugin := range c.Plugins {
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion migrator/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit a0aceeb

Please sign in to comment.