Skip to content

Commit b491e9a

Browse files
committed
Added addColumn() function and rewrited migration v180
1 parent d9898c3 commit b491e9a

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

models/migrations/migrations.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,3 +891,13 @@ func modifyColumn(x *xorm.Engine, tableName string, col *schemas.Column) error {
891891
}
892892
return nil
893893
}
894+
895+
// addColumn adds new column to the specified table
896+
func addColumn(x *xorm.Engine, tableName string, col *schemas.Column) error {
897+
alterSQL := x.Dialect().AddColumnSQL(tableName, col)
898+
if _, err := x.Exec(alterSQL); err != nil {
899+
log.Error("Add column %s to table %s failed: %v", col.Name, tableName, err)
900+
return err
901+
}
902+
return nil
903+
}

models/migrations/v180.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@ package migrations
66

77
import (
88
"xorm.io/xorm"
9+
"xorm.io/xorm/schemas"
910
)
1011

1112
func addWhitespaceBehaviorUserColumn(x *xorm.Engine) error {
12-
type User struct {
13-
WhitespaceBehavior bool `xorm:"NOT NULL DEFAULT false"`
14-
}
15-
16-
return x.Sync2(new(User))
13+
return addColumn(x, "user", &schemas.Column{
14+
Name: "WhitespaceBehavior",
15+
SQLType: schemas.SQLType{
16+
Name: schemas.Text,
17+
},
18+
Default: "",
19+
Nullable: false,
20+
})
1721
}

0 commit comments

Comments
 (0)