Merge branch 'master' into lunny/fix_join

This commit is contained in:
Lunny Xiao 2023-07-12 01:36:14 +08:00
commit 0cbecf9abc
3 changed files with 49 additions and 1 deletions

View File

@ -397,6 +397,9 @@ func (db *mysql) AddColumnSQL(tableName string, col *schemas.Column) string {
// ModifyColumnSQL returns a SQL to modify SQL // ModifyColumnSQL returns a SQL to modify SQL
func (db *mysql) ModifyColumnSQL(tableName string, col *schemas.Column) string { func (db *mysql) ModifyColumnSQL(tableName string, col *schemas.Column) string {
s, _ := ColumnString(db.dialect, col, false, true) 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) return fmt.Sprintf("ALTER TABLE %s MODIFY COLUMN %s", db.quoter.Quote(tableName), s)
} }

View File

@ -289,6 +289,48 @@ func TestGetColumnsComment(t *testing.T) {
assert.Zero(t, noComment) 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) { func TestGetColumnsLength(t *testing.T) {
var max_length int64 var max_length int64
switch testEngine.Dialect().URI().DBType { switch testEngine.Dialect().URI().DBType {

View File

@ -181,8 +181,11 @@ func (session *Session) SyncWithOptions(opts SyncOptions, beans ...interface{})
} }
} }
} else if col.Comment != oriCol.Comment { } 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)) _, err = session.exec(engine.dialect.ModifyColumnSQL(tbNameWithSchema, col))
} }
}
if col.Default != oriCol.Default { if col.Default != oriCol.Default {
switch { switch {