Skip to content

Commit 384c216

Browse files
committed
Updated signature of makeComments with variadic models.
1 parent b9dbcea commit 384c216

File tree

2 files changed

+71
-32
lines changed

2 files changed

+71
-32
lines changed

database/pgComment.go

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,51 +8,52 @@ import (
88
"strings"
99
)
1010

11-
func makeComments(ctx context.Context, conn PgGoConnection, model interface{}) error {
12-
modelType := reflect.TypeOf(model)
13-
var tableName pg.Safe
11+
func makeComments(ctx context.Context, conn PgGoConnection, models ...interface{}) error {
12+
for _, model := range models {
13+
modelType := reflect.TypeOf(model)
14+
var tableName pg.Safe
15+
16+
for i := 0; i < modelType.NumField(); i++ {
17+
fieldType := modelType.Field(i)
18+
19+
if fieldType.Name == "tableName" {
20+
var ok bool
21+
tableName, ok = getPgName(fieldType)
22+
if !ok {
23+
tableName = pg.Safe(hasura.ToSnakeCase(modelType.Name()))
24+
}
25+
26+
pgCommentTag, ok := getPgComment(fieldType)
27+
if !ok {
28+
continue
29+
}
30+
31+
if _, err := conn.DB().ExecContext(ctx,
32+
`COMMENT ON TABLE ? IS ?`,
33+
tableName, pgCommentTag); err != nil {
34+
return err
35+
}
1436

15-
for i := 0; i < modelType.NumField(); i++ {
16-
fieldType := modelType.Field(i)
17-
18-
if fieldType.Name == "tableName" {
19-
var ok bool
20-
tableName, ok = getPgName(fieldType)
21-
if !ok {
22-
tableName = pg.Safe(hasura.ToSnakeCase(modelType.Name()))
37+
continue
2338
}
2439

2540
pgCommentTag, ok := getPgComment(fieldType)
2641
if !ok {
2742
continue
2843
}
2944

45+
columnName, ok := getPgName(fieldType)
46+
if !ok {
47+
columnName = pg.Safe(hasura.ToSnakeCase(fieldType.Name))
48+
}
49+
3050
if _, err := conn.DB().ExecContext(ctx,
31-
`COMMENT ON TABLE ? IS ?`,
32-
tableName, pgCommentTag); err != nil {
51+
`COMMENT ON COLUMN ?.? IS ?`,
52+
tableName, columnName, pgCommentTag); err != nil {
3353
return err
3454
}
35-
36-
continue
37-
}
38-
39-
pgCommentTag, ok := getPgComment(fieldType)
40-
if !ok {
41-
continue
42-
}
43-
44-
columnName, ok := getPgName(fieldType)
45-
if !ok {
46-
columnName = pg.Safe(hasura.ToSnakeCase(fieldType.Name))
47-
}
48-
49-
if _, err := conn.DB().ExecContext(ctx,
50-
`COMMENT ON COLUMN ?.? IS ?`,
51-
tableName, columnName, pgCommentTag); err != nil {
52-
return err
5355
}
5456
}
55-
5657
return nil
5758
}
5859

database/pgComment_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,44 @@ func TestMakeCommentsWithTableNameAndFieldsWithPgComment(t *testing.T) {
160160
assert.Empty(t, err)
161161
}
162162

163+
func TestMakeCommentsWithMultipleModels(t *testing.T) {
164+
type Ballot struct {
165+
//nolint
166+
tableName struct{} `pg:"ballots" pg-comment:"This multiple table name comment"`
167+
Ballot string `json:"ballot" pg-comment:"This is multiple field comment"`
168+
}
169+
170+
mockCtrl, mockPgDB, pgGo, ctx := createPgDbMock(t)
171+
defer mockCtrl.Finish()
172+
173+
models := []interface{}{Ballot{}, Ballot{}, Ballot{}}
174+
175+
// Assert prepare
176+
expectedTableParams := toInterfaceSlice([]pg.Safe{"ballots", "This multiple table name comment"})
177+
mockPgDB.
178+
EXPECT().
179+
ExecContext(ctx, "COMMENT ON TABLE ? IS ?",
180+
gomock.Eq(expectedTableParams)).
181+
Times(3).
182+
Return(nil, nil)
183+
184+
// Be aware: there is on issue with default order in checking
185+
// methods call: https://github.com/golang/mock/issues/653
186+
expectedParams := toInterfaceSlice([]pg.Safe{"ballots", "ballot", "This is multiple field comment"})
187+
mockPgDB.
188+
EXPECT().
189+
ExecContext(ctx, "COMMENT ON COLUMN ?.? IS ?",
190+
gomock.Eq(expectedParams)).
191+
Times(3).
192+
Return(nil, nil)
193+
194+
// Act
195+
err := makeComments(ctx, pgGo, models...)
196+
197+
// Assert
198+
assert.Empty(t, err)
199+
}
200+
163201
func createPgDbMock(t *testing.T) (*gomock.Controller, *mocks.MockPgDB, *PgGoMock, context.Context) {
164202
mockCtrl := gomock.NewController(t)
165203
mockPgDB := mocks.NewMockPgDB(mockCtrl)

0 commit comments

Comments
 (0)