In SQLite3, Sync doesn't support Modify Column:Error: near MODIFY: syntax error (#2267)
rebase Co-authored-by: brookechen <brookechen@noreply.gitea.io> Co-authored-by: brookechen <brookechen@tencent.com> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-on: https://gitea.com/xorm/xorm/pulls/2267 Co-authored-by: brookechen <brookechen@noreply.gitea.com> Co-committed-by: brookechen <brookechen@noreply.gitea.com>
This commit is contained in:
parent
52b01ce67f
commit
486c344ba3
|
@ -397,6 +397,9 @@ func (db *mysql) AddColumnSQL(tableName string, col *schemas.Column) string {
|
|||
// ModifyColumnSQL returns a SQL to modify SQL
|
||||
func (db *mysql) ModifyColumnSQL(tableName string, col *schemas.Column) string {
|
||||
s, _ := ColumnString(db.dialect, col, false, true)
|
||||
if col.Comment != "" {
|
||||
s += fmt.Sprintf(" COMMENT '%s'", col.Comment)
|
||||
}
|
||||
return fmt.Sprintf("ALTER TABLE %s MODIFY COLUMN %s", db.quoter.Quote(tableName), s)
|
||||
}
|
||||
|
||||
|
|
|
@ -289,6 +289,48 @@ func TestGetColumnsComment(t *testing.T) {
|
|||
assert.Zero(t, noComment)
|
||||
}
|
||||
|
||||
type TestCommentUpdate struct {
|
||||
HasComment int `xorm:"bigint comment('this is a comment before update')"`
|
||||
}
|
||||
|
||||
func (m *TestCommentUpdate) TableName() string {
|
||||
return "test_comment_struct"
|
||||
}
|
||||
|
||||
type TestCommentUpdate2 struct {
|
||||
HasComment int `xorm:"bigint comment('this is a comment after update')"`
|
||||
}
|
||||
|
||||
func (m *TestCommentUpdate2) TableName() string {
|
||||
return "test_comment_struct"
|
||||
}
|
||||
|
||||
func TestColumnCommentUpdate(t *testing.T) {
|
||||
comment := "this is a comment after update"
|
||||
assertSync(t, new(TestCommentUpdate))
|
||||
assert.NoError(t, testEngine.Sync2(new(TestCommentUpdate2))) // modify table column comment
|
||||
|
||||
switch testEngine.Dialect().URI().DBType {
|
||||
case schemas.POSTGRES, schemas.MYSQL: // only postgres / mysql dialect implement the feature of modify comment in postgres.ModifyColumnSQL
|
||||
default:
|
||||
t.Skip()
|
||||
return
|
||||
}
|
||||
tables, err := testEngine.DBMetas()
|
||||
assert.NoError(t, err)
|
||||
tableName := "test_comment_struct"
|
||||
var hasComment string
|
||||
for _, table := range tables {
|
||||
if table.Name == tableName {
|
||||
col := table.GetColumn(testEngine.GetColumnMapper().Obj2Table("HasComment"))
|
||||
assert.NotNil(t, col)
|
||||
hasComment = col.Comment
|
||||
break
|
||||
}
|
||||
}
|
||||
assert.Equal(t, comment, hasComment)
|
||||
}
|
||||
|
||||
func TestGetColumnsLength(t *testing.T) {
|
||||
var max_length int64
|
||||
switch testEngine.Dialect().URI().DBType {
|
||||
|
|
3
sync.go
3
sync.go
|
@ -181,8 +181,11 @@ func (session *Session) SyncWithOptions(opts SyncOptions, beans ...interface{})
|
|||
}
|
||||
}
|
||||
} else if col.Comment != oriCol.Comment {
|
||||
if engine.dialect.URI().DBType == schemas.POSTGRES ||
|
||||
engine.dialect.URI().DBType == schemas.MYSQL {
|
||||
_, err = session.exec(engine.dialect.ModifyColumnSQL(tbNameWithSchema, col))
|
||||
}
|
||||
}
|
||||
|
||||
if col.Default != oriCol.Default {
|
||||
switch {
|
||||
|
|
Loading…
Reference in New Issue