PostgreSQL: enable comment on column #2131

Merged
lunny merged 4 commits from pilou/xorm:postgresql_handle_column_comment into master 2022-04-17 10:03:30 +00:00
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