Merge 808e1b68536bd659e4423a1672c86ea8f7d9778f into 0e1cb6ece9d27b56ee6c1e514987175bba94711b
This commit is contained in:
commit
8c222149d7
@ -33,6 +33,8 @@ type Dialect interface {
|
||||
HasTable(tableName string) bool
|
||||
// HasColumn check has column or not
|
||||
HasColumn(tableName string, columnName string) bool
|
||||
// TableColumns return all columns
|
||||
TableColumns(tableName string) []string
|
||||
|
||||
// LimitAndOffsetSQL return generated SQL with Limit and Offset, as mssql has special case
|
||||
LimitAndOffsetSQL(limit, offset interface{}) string
|
||||
|
@ -119,6 +119,12 @@ func (s commonDialect) HasColumn(tableName string, columnName string) bool {
|
||||
s.db.QueryRow("SELECT count(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = ? AND table_name = ? AND column_name = ?", currentDatabase, tableName, columnName).Scan(&count)
|
||||
return count > 0
|
||||
}
|
||||
func (s commonDialect) TableColumns(tableName string) []string {
|
||||
|
||||
var columns []string
|
||||
s.db.QueryRow("SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE table_schema = ? AND table_name = ?", s.CurrentDatabase(), tableName).Scan(&columns)
|
||||
return columns
|
||||
}
|
||||
|
||||
func (s commonDialect) CurrentDatabase() (name string) {
|
||||
s.db.QueryRow("SELECT DATABASE()").Scan(&name)
|
||||
|
8
scope.go
8
scope.go
@ -1196,8 +1196,14 @@ func (scope *Scope) autoMigrate() *Scope {
|
||||
if !scope.Dialect().HasTable(tableName) {
|
||||
scope.createTable()
|
||||
} else {
|
||||
columns := scope.Dialect().TableColumns(tableName)
|
||||
columnsM := map[string]bool{}
|
||||
for _, column := range columns {
|
||||
columnsM[column] = true
|
||||
}
|
||||
|
||||
for _, field := range scope.GetModelStruct().StructFields {
|
||||
if !scope.Dialect().HasColumn(tableName, field.DBName) {
|
||||
if _, ok := columnsM[field.DBName]; !ok {
|
||||
if field.IsNormal {
|
||||
sqlTag := scope.Dialect().DataTypeOf(field)
|
||||
scope.Raw(fmt.Sprintf("ALTER TABLE %v ADD %v %v;", quotedTableName, scope.Quote(field.DBName), sqlTag)).Exec()
|
||||
|
Loading…
x
Reference in New Issue
Block a user