From 486c344ba3750a9d2293372ad4e6f510e3371bf3 Mon Sep 17 00:00:00 2001 From: brookechen Date: Tue, 11 Jul 2023 17:10:36 +0000 Subject: [PATCH] =?UTF-8?q?In=20SQLite3,=20Sync=20doesn't=20support=20Modi?= =?UTF-8?q?fy=20Column=EF=BC=9AError:=20near=20MODIFY:=20syntax=20error=20?= =?UTF-8?q?(#2267)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit rebase Co-authored-by: brookechen Co-authored-by: brookechen Co-authored-by: Lunny Xiao Reviewed-on: https://gitea.com/xorm/xorm/pulls/2267 Co-authored-by: brookechen Co-committed-by: brookechen --- dialects/mysql.go | 3 +++ integrations/engine_test.go | 42 +++++++++++++++++++++++++++++++++++++ sync.go | 5 ++++- 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/dialects/mysql.go b/dialects/mysql.go index 5663d1dd..82505707 100644 --- a/dialects/mysql.go +++ b/dialects/mysql.go @@ -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) } diff --git a/integrations/engine_test.go b/integrations/engine_test.go index 730a424e..86ed7344 100644 --- a/integrations/engine_test.go +++ b/integrations/engine_test.go @@ -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 { diff --git a/sync.go b/sync.go index 6583e341..11e75404 100644 --- a/sync.go +++ b/sync.go @@ -181,7 +181,10 @@ func (session *Session) SyncWithOptions(opts SyncOptions, beans ...interface{}) } } } else if col.Comment != oriCol.Comment { - _, err = session.exec(engine.dialect.ModifyColumnSQL(tbNameWithSchema, col)) + 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 {