From ea9bba0d145211974a5bbe9d6b79324c257b6fc4 Mon Sep 17 00:00:00 2001 From: Pierre-Louis Bonicoli Date: Sun, 17 Apr 2022 18:03:29 +0800 Subject: [PATCH] PostgreSQL: enable comment on column (#2131) The [oldest unsupported version documentation](https://www.postgresql.org/docs/7.1/sql-comment.html) states that comment on a column is supported. Update `TestGetColumnsComment` in order to check both MySQL/MariaDB and PostgreSQL. Co-authored-by: Pierre-Louis Bonicoli Co-authored-by: Lunny Xiao Reviewed-on: https://gitea.com/xorm/xorm/pulls/2131 Reviewed-by: Lunny Xiao Co-authored-by: Pierre-Louis Bonicoli Co-committed-by: Pierre-Louis Bonicoli --- dialects/postgres.go | 8 ++++++++ integrations/engine_test.go | 18 ++++++++---------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/dialects/postgres.go b/dialects/postgres.go index a5b080aa..83e4187f 100644 --- a/dialects/postgres.go +++ b/dialects/postgres.go @@ -1354,6 +1354,14 @@ func (db *postgres) CreateTableSQL(ctx context.Context, queryer core.Queryer, ta commentSQL += fmt.Sprintf("COMMENT ON TABLE %s IS '%s'", quoter.Quote(tableName), table.Comment) } + for _, colName := range table.ColumnsSeq() { + col := table.GetColumn(colName) + + if len(col.Comment) > 0 { + commentSQL += fmt.Sprintf("COMMENT ON COLUMN %s.%s IS '%s'", quoter.Quote(tableName), quoter.Quote(col.Name), col.Comment) + } + } + return createTableSQL + commentSQL, true, nil } diff --git a/integrations/engine_test.go b/integrations/engine_test.go index cdcdd6be..997c8962 100644 --- a/integrations/engine_test.go +++ b/integrations/engine_test.go @@ -255,33 +255,31 @@ func TestDBVersion(t *testing.T) { fmt.Println(testEngine.Dialect().URI().DBType, "version is", version) } -func TestGetColumns(t *testing.T) { - if testEngine.Dialect().URI().DBType != schemas.POSTGRES { +func TestGetColumnsComment(t *testing.T) { + switch testEngine.Dialect().URI().DBType { + case schemas.POSTGRES, schemas.MYSQL: + default: t.Skip() return } + comment := "this is a comment" type TestCommentStruct struct { - HasComment int + HasComment int `xorm:"comment('this is a comment')"` NoComment int } assertSync(t, new(TestCommentStruct)) - comment := "this is a comment" - sql := fmt.Sprintf("comment on column %s.%s is '%s'", testEngine.TableName(new(TestCommentStruct), true), "has_comment", comment) - _, err := testEngine.Exec(sql) - assert.NoError(t, err) - tables, err := testEngine.DBMetas() assert.NoError(t, err) tableName := testEngine.GetColumnMapper().Obj2Table("TestCommentStruct") var hasComment, noComment string for _, table := range tables { if table.Name == tableName { - col := table.GetColumn("has_comment") + col := table.GetColumn(testEngine.GetColumnMapper().Obj2Table("HasComment")) assert.NotNil(t, col) hasComment = col.Comment - col2 := table.GetColumn("no_comment") + col2 := table.GetColumn(testEngine.GetColumnMapper().Obj2Table("NoComment")) assert.NotNil(t, col2) noComment = col2.Comment break