Add AddColumn method

This commit is contained in:
Maxim Lanin 2017-02-16 18:58:35 +03:00
parent 5050a58b45
commit 3b699fdf3e
2 changed files with 11 additions and 0 deletions

View File

@ -552,6 +552,13 @@ func (s *DB) AutoMigrate(values ...interface{}) *DB {
return db
}
// AddColumn add column with type
func (s *DB) AddColumn(column string, typ string) *DB {
scope := s.clone().NewScope(s.Value)
scope.addColumn(column, typ)
return scope.db
}
// ModifyColumn modify column to type
func (s *DB) ModifyColumn(column string, typ string) *DB {
scope := s.clone().NewScope(s.Value)

View File

@ -1124,6 +1124,10 @@ func (scope *Scope) dropTable() *Scope {
return scope
}
func (scope *Scope) addColumn(column string, typ string) {
scope.Raw(fmt.Sprintf("ALTER TABLE %v ADD %v %v", scope.QuotedTableName(), scope.Quote(column), typ)).Exec()
}
func (scope *Scope) modifyColumn(column string, typ string) {
scope.Raw(fmt.Sprintf("ALTER TABLE %v MODIFY %v %v", scope.QuotedTableName(), scope.Quote(column), typ)).Exec()
}