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 <pierre-louis.bonicoli@libregerbil.fr>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Reviewed-on: https://gitea.com/xorm/xorm/pulls/2131
Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com>
Co-authored-by: Pierre-Louis Bonicoli <pilou@noreply.gitea.io>
Co-committed-by: Pierre-Louis Bonicoli <pilou@noreply.gitea.io>
This commit is contained in:
Pierre-Louis Bonicoli 2022-04-17 18:03:29 +08:00 committed by Lunny Xiao
parent e858b75756
commit ea9bba0d14
2 changed files with 16 additions and 10 deletions

View File

@ -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) 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 return createTableSQL + commentSQL, true, nil
} }

View File

@ -255,33 +255,31 @@ func TestDBVersion(t *testing.T) {
fmt.Println(testEngine.Dialect().URI().DBType, "version is", version) fmt.Println(testEngine.Dialect().URI().DBType, "version is", version)
} }
func TestGetColumns(t *testing.T) { func TestGetColumnsComment(t *testing.T) {
if testEngine.Dialect().URI().DBType != schemas.POSTGRES { switch testEngine.Dialect().URI().DBType {
case schemas.POSTGRES, schemas.MYSQL:
default:
t.Skip() t.Skip()
return return
} }
comment := "this is a comment"
type TestCommentStruct struct { type TestCommentStruct struct {
HasComment int HasComment int `xorm:"comment('this is a comment')"`
NoComment int NoComment int
} }
assertSync(t, new(TestCommentStruct)) 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() tables, err := testEngine.DBMetas()
assert.NoError(t, err) assert.NoError(t, err)
tableName := testEngine.GetColumnMapper().Obj2Table("TestCommentStruct") tableName := testEngine.GetColumnMapper().Obj2Table("TestCommentStruct")
var hasComment, noComment string var hasComment, noComment string
for _, table := range tables { for _, table := range tables {
if table.Name == tableName { if table.Name == tableName {
col := table.GetColumn("has_comment") col := table.GetColumn(testEngine.GetColumnMapper().Obj2Table("HasComment"))
assert.NotNil(t, col) assert.NotNil(t, col)
hasComment = col.Comment hasComment = col.Comment
col2 := table.GetColumn("no_comment") col2 := table.GetColumn(testEngine.GetColumnMapper().Obj2Table("NoComment"))
assert.NotNil(t, col2) assert.NotNil(t, col2)
noComment = col2.Comment noComment = col2.Comment
break break