Skip to content

Commit 88451a1

Browse files
committed
Only return UUID from EnqueueNotificationMessage
Changes made to dbgen are to avoid this panic: $ ./coderd/database/generate.sh generate panic: unknown return type: *dst.SelectorExpr goroutine 1 [running]: main.orderAndStubDatabaseFunctions({0x1400001ea80, 0x3c}, {0x102d019e0, 0x1}, {0x102d0bc75, 0xb}, 0x102df7b20) /Users/danny/Code/coder/coder/scripts/dbgen/main.go:367 +0x2588 main.run() /Users/danny/Code/coder/coder/scripts/dbgen/main.go:56 +0x118 main.main() /Users/danny/Code/coder/coder/scripts/dbgen/main.go:42 +0x1c exit status 2 Signed-off-by: Danny Kopping <danny@coder.com>
1 parent 5ff29c0 commit 88451a1

File tree

9 files changed

+36
-39
lines changed

9 files changed

+36
-39
lines changed

coderd/database/dbauthz/dbauthz.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,9 +1143,9 @@ func (q *querier) DeleteWorkspaceAgentPortSharesByTemplate(ctx context.Context,
11431143
return q.db.DeleteWorkspaceAgentPortSharesByTemplate(ctx, templateID)
11441144
}
11451145

1146-
func (q *querier) EnqueueNotificationMessage(ctx context.Context, arg database.EnqueueNotificationMessageParams) (database.NotificationMessage, error) {
1146+
func (q *querier) EnqueueNotificationMessage(ctx context.Context, arg database.EnqueueNotificationMessageParams) (uuid.UUID, error) {
11471147
if err := q.authorizeContext(ctx, policy.ActionCreate, rbac.ResourceSystem); err != nil {
1148-
return database.NotificationMessage{}, err
1148+
return uuid.UUID{}, err
11491149
}
11501150
return q.db.EnqueueNotificationMessage(ctx, arg)
11511151
}

coderd/database/dbmem/dbmem.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1817,10 +1817,10 @@ func (q *FakeQuerier) DeleteWorkspaceAgentPortSharesByTemplate(_ context.Context
18171817
return nil
18181818
}
18191819

1820-
func (q *FakeQuerier) EnqueueNotificationMessage(_ context.Context, arg database.EnqueueNotificationMessageParams) (database.NotificationMessage, error) {
1820+
func (q *FakeQuerier) EnqueueNotificationMessage(_ context.Context, arg database.EnqueueNotificationMessageParams) (uuid.UUID, error) {
18211821
err := validateDatabaseType(arg)
18221822
if err != nil {
1823-
return database.NotificationMessage{}, err
1823+
return uuid.UUID{}, err
18241824
}
18251825

18261826
q.mutex.Lock()
@@ -1829,7 +1829,7 @@ func (q *FakeQuerier) EnqueueNotificationMessage(_ context.Context, arg database
18291829
var payload types.MessagePayload
18301830
err = json.Unmarshal(arg.Payload, &payload)
18311831
if err != nil {
1832-
return database.NotificationMessage{}, err
1832+
return uuid.UUID{}, err
18331833
}
18341834

18351835
nm := database.NotificationMessage{
@@ -1847,7 +1847,7 @@ func (q *FakeQuerier) EnqueueNotificationMessage(_ context.Context, arg database
18471847

18481848
q.notificationMessages = append(q.notificationMessages, nm)
18491849

1850-
return nm, err
1850+
return nm.ID, err
18511851
}
18521852

18531853
func (q *FakeQuerier) FavoriteWorkspace(_ context.Context, arg uuid.UUID) error {

coderd/database/dbmetrics/dbmetrics.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/querier.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

Lines changed: 5 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/notifications.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ VALUES (@id,
1919
@payload::jsonb,
2020
@targets,
2121
@created_by)
22-
RETURNING *;
22+
RETURNING id;
2323

2424
-- Acquires the lease for a given count of notification messages, to enable concurrent dequeuing and subsequent sending.
2525
-- Only rows that aren't already leased (or ones which are leased but have exceeded their lease period) are returned.

coderd/notifications/manager_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,15 @@ func newEnqueueInterceptor(db notifications.Store, metadataFn func() database.Fe
219219
return &enqueueInterceptor{Store: db, payload: make(chan types.MessagePayload, 1), metadataFn: metadataFn}
220220
}
221221

222-
func (e *enqueueInterceptor) EnqueueNotificationMessage(_ context.Context, arg database.EnqueueNotificationMessageParams) (database.NotificationMessage, error) {
222+
func (e *enqueueInterceptor) EnqueueNotificationMessage(_ context.Context, arg database.EnqueueNotificationMessageParams) (uuid.UUID, error) {
223223
var payload types.MessagePayload
224224
err := json.Unmarshal(arg.Payload, &payload)
225225
if err != nil {
226-
return database.NotificationMessage{}, err
226+
return uuid.UUID{}, err
227227
}
228228

229229
e.payload <- payload
230-
return database.NotificationMessage{}, err
230+
return uuid.UUID{}, err
231231
}
232232

233233
func (e *enqueueInterceptor) FetchNewMessageMetadata(_ context.Context, _ database.FetchNewMessageMetadataParams) (database.FetchNewMessageMetadataRow, error) {

coderd/notifications/spec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type Store interface {
1818
AcquireNotificationMessages(ctx context.Context, params database.AcquireNotificationMessagesParams) ([]database.AcquireNotificationMessagesRow, error)
1919
BulkMarkNotificationMessagesSent(ctx context.Context, arg database.BulkMarkNotificationMessagesSentParams) (int64, error)
2020
BulkMarkNotificationMessagesFailed(ctx context.Context, arg database.BulkMarkNotificationMessagesFailedParams) (int64, error)
21-
EnqueueNotificationMessage(ctx context.Context, arg database.EnqueueNotificationMessageParams) (database.NotificationMessage, error)
21+
EnqueueNotificationMessage(ctx context.Context, arg database.EnqueueNotificationMessageParams) (uuid.UUID, error)
2222
FetchNewMessageMetadata(ctx context.Context, arg database.FetchNewMessageMetadataParams) (database.FetchNewMessageMetadataRow, error)
2323
GetNotificationMessagesByStatus(ctx context.Context, arg database.GetNotificationMessagesByStatusParams) ([]database.NotificationMessage, error)
2424
}

scripts/dbgen/main.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -342,11 +342,22 @@ func orderAndStubDatabaseFunctions(filePath, receiver, structName string, stub f
342342
switch typ := r.Type.(type) {
343343
case *dst.StarExpr, *dst.ArrayType:
344344
returnStmt.Results = append(returnStmt.Results, dst.NewIdent("nil"))
345-
case *dst.Ident:
346-
if typ.Path != "" {
347-
returnStmt.Results = append(returnStmt.Results, dst.NewIdent(fmt.Sprintf("%s.%s{}", path.Base(typ.Path), typ.Name)))
345+
case *dst.Ident, *dst.SelectorExpr:
346+
var ident *dst.Ident
347+
348+
// SelectorExpr contains a *dst.Ident
349+
if sel, ok := typ.(*dst.SelectorExpr); ok {
350+
if ident, ok = sel.X.(*dst.Ident); !ok {
351+
panic(fmt.Sprintf("SelectorExpr does not contain expected type %T: got %T", ident, sel.X))
352+
}
353+
} else {
354+
ident = typ.(*dst.Ident)
355+
}
356+
357+
if ident.Path != "" {
358+
returnStmt.Results = append(returnStmt.Results, dst.NewIdent(fmt.Sprintf("%s.%s{}", path.Base(ident.Path), ident.Name)))
348359
} else {
349-
switch typ.Name {
360+
switch ident.Name {
350361
case "uint8", "uint16", "uint32", "uint64", "uint", "uintptr",
351362
"int8", "int16", "int32", "int64", "int",
352363
"byte", "rune",
@@ -359,8 +370,10 @@ func orderAndStubDatabaseFunctions(filePath, receiver, structName string, stub f
359370
returnStmt.Results = append(returnStmt.Results, dst.NewIdent("false"))
360371
case "error":
361372
returnStmt.Results = append(returnStmt.Results, dst.NewIdent("err"))
373+
case "uuid":
374+
returnStmt.Results = append(returnStmt.Results, dst.NewIdent("uuid"))
362375
default:
363-
panic(fmt.Sprintf("unknown ident: %#v", r.Type))
376+
panic(fmt.Sprintf("unknown ident (%q): %#v", ident.Name, r.Type))
364377
}
365378
}
366379
default:

0 commit comments

Comments
 (0)