Skip to content

Commit 8fec936

Browse files
committed
coderd/database: only generate ID at database side, order messages by created_at instead of id
1 parent 76b949c commit 8fec936

File tree

6 files changed

+17
-13
lines changed

6 files changed

+17
-13
lines changed

coderd/chat.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"net/http"
77
"time"
88

9-
"github.com/google/uuid"
109
"github.com/kylecarbs/aisdk-go"
1110

1211
"github.com/coder/coder/v2/coderd/ai"
@@ -33,7 +32,6 @@ func (api *API) postChats(w http.ResponseWriter, r *http.Request) {
3332
ctx := r.Context()
3433

3534
chat, err := api.Database.InsertChat(ctx, database.InsertChatParams{
36-
ID: uuid.New(),
3735
OwnerID: apiKey.UserID,
3836
CreatedAt: time.Now(),
3937
UpdatedAt: time.Now(),

coderd/database/dbauthz/dbauthz_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5344,7 +5344,7 @@ func (s *MethodTestSuite) TestChat() {
53445344
OwnerID: u1.ID,
53455345
})
53465346
_, _, _ = createChat(s.T(), db) // other user's chat
5347-
check.Args(u1.ID).Asserts(u1c2, policy.ActionRead, u1c1, policy.ActionRead).Returns([]database.Chat{u1c1, u1c2})
5347+
check.Args(u1.ID).Asserts(u1c2, policy.ActionRead, u1c1, policy.ActionRead).Returns([]database.Chat{u1c2, u1c1})
53485348
}))
53495349

53505350
s.Run("InsertChat", s.Subtest(func(db database.Store, check *expects) {

coderd/database/dbgen/dbgen.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ func APIKey(t testing.TB, db database.Store, seed database.APIKey) (key database
144144

145145
func Chat(t testing.TB, db database.Store, seed database.Chat) database.Chat {
146146
chat, err := db.InsertChat(genCtx, database.InsertChatParams{
147-
ID: takeFirst(seed.ID, uuid.New()),
148147
OwnerID: takeFirst(seed.OwnerID, uuid.New()),
149148
CreatedAt: takeFirst(seed.CreatedAt, dbtime.Now()),
150149
UpdatedAt: takeFirst(seed.UpdatedAt, dbtime.Now()),

coderd/database/dbmem/dbmem.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2916,6 +2916,9 @@ func (q *FakeQuerier) GetChatsByOwnerID(ctx context.Context, ownerID uuid.UUID)
29162916
chats = append(chats, chat)
29172917
}
29182918
}
2919+
sort.Slice(chats, func(i, j int) bool {
2920+
return chats[i].CreatedAt.After(chats[j].CreatedAt)
2921+
})
29192922
return chats, nil
29202923
}
29212924

@@ -8447,7 +8450,13 @@ func (q *FakeQuerier) InsertChat(ctx context.Context, arg database.InsertChatPar
84478450
q.mutex.Lock()
84488451
defer q.mutex.Unlock()
84498452

8450-
chat := database.Chat(arg)
8453+
chat := database.Chat{
8454+
ID: uuid.New(),
8455+
CreatedAt: arg.CreatedAt,
8456+
UpdatedAt: arg.UpdatedAt,
8457+
OwnerID: arg.OwnerID,
8458+
Title: arg.Title,
8459+
}
84518460
q.chats = append(q.chats, chat)
84528461

84538462
return chat, nil

coderd/database/queries.sql.go

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

coderd/database/queries/chat.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-- name: InsertChat :one
2-
INSERT INTO chats (id, owner_id, created_at, updated_at, title)
3-
VALUES ($1, $2, $3, $4, $5)
2+
INSERT INTO chats (owner_id, created_at, updated_at, title)
3+
VALUES ($1, $2, $3, $4)
44
RETURNING *;
55

66
-- name: UpdateChatByID :exec
@@ -30,7 +30,7 @@ RETURNING chat_messages.*;
3030
-- name: GetChatMessagesByChatID :many
3131
SELECT * FROM chat_messages
3232
WHERE chat_id = $1
33-
ORDER BY id ASC;
33+
ORDER BY created_at ASC;
3434

3535
-- name: DeleteChat :exec
3636
DELETE FROM chats WHERE id = $1;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy