Skip to content

Commit 53a17ef

Browse files
committed
promoted: remove prisma from postgres store
1 parent f41105d commit 53a17ef

File tree

4 files changed

+117
-72
lines changed

4 files changed

+117
-72
lines changed

promoted/postgres/model.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package postgres
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"github.com/georgysavva/scany/v2/pgxscan"
8+
"github.com/jackc/pgx/v5/pgxpool"
9+
10+
commonpb "github.com/code-payments/flipchat-protobuf-api/generated/go/common/v1"
11+
12+
pg "github.com/code-payments/flipchat-server/database/postgres"
13+
"github.com/code-payments/flipchat-server/promoted"
14+
)
15+
16+
const (
17+
promotedChatsTableName = "flipchat_promotedchats"
18+
allPromotedChatFields = `"chatId", "topic", "score", "createdAt", "updatedAt"`
19+
)
20+
21+
type model struct {
22+
ChatID string `db:"chatId"`
23+
Topic string `db:"topic"`
24+
Score int `db:"score"`
25+
CreatedAt time.Time `db:"createdAt"`
26+
UpdatedAt time.Time `db:"updatedAt"`
27+
}
28+
29+
func toModel(promotedChat *promoted.PromotedChat) (*model, error) {
30+
return &model{
31+
ChatID: pg.Encode(promotedChat.ChatID.Value),
32+
Topic: promotedChat.Topic,
33+
Score: promotedChat.Score,
34+
}, nil
35+
}
36+
37+
func fromModel(m *model) (*promoted.PromotedChat, error) {
38+
decodedChatID, err := pg.Decode(m.ChatID)
39+
if err != nil {
40+
return nil, err
41+
}
42+
return &promoted.PromotedChat{
43+
ChatID: &commonpb.ChatId{Value: decodedChatID},
44+
Topic: m.Topic,
45+
Score: m.Score,
46+
}, nil
47+
}
48+
49+
func (m *model) dbUpsert(ctx context.Context, pool *pgxpool.Pool) error {
50+
query := `INSERT INTO ` + promotedChatsTableName + ` (` + allPromotedChatFields + `) VALUES ($1, $2, $3, NOW(), NOW()) ON CONFLICT ("chatId", "topic") DO UPDATE SET "score" = $3 WHERE ` + promotedChatsTableName + `."chatId" = $1 AND ` + promotedChatsTableName + `."topic" = $2 RETURNING` + allPromotedChatFields
51+
return pgxscan.Get(
52+
ctx,
53+
pool,
54+
m,
55+
query,
56+
m.ChatID,
57+
m.Topic,
58+
m.Score,
59+
)
60+
}
61+
62+
func dbGetPromotedChats(ctx context.Context, pool *pgxpool.Pool, topic string) ([]*model, error) {
63+
var res []*model
64+
query := `SELECT ` + allPromotedChatFields + ` FROM ` + promotedChatsTableName + ` WHERE "topic" = $1 ORDER BY "score" DESC`
65+
err := pgxscan.Select(
66+
ctx,
67+
pool,
68+
&res,
69+
query,
70+
topic,
71+
)
72+
if err != nil {
73+
if pgxscan.NotFound(err) {
74+
return nil, promoted.ErrNotFound
75+
}
76+
return nil, err
77+
}
78+
return res, nil
79+
}
80+
81+
func dbDemoteChat(ctx context.Context, pool *pgxpool.Pool, chatID *commonpb.ChatId, topic string) error {
82+
query := `DELETE FROM ` + promotedChatsTableName + ` WHERE "chatId" = $1 AND "topic" = $2`
83+
_, err := pool.Exec(
84+
ctx,
85+
query,
86+
pg.Encode(chatID.Value),
87+
topic,
88+
)
89+
return err
90+
}

promoted/postgres/server_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
account "github.com/code-payments/flipchat-server/account/postgres"
1212
chat "github.com/code-payments/flipchat-server/chat/postgres"
13-
prismatest "github.com/code-payments/flipchat-server/database/prisma/test"
1413

1514
"github.com/code-payments/flipchat-server/promoted/tests"
1615

@@ -23,12 +22,9 @@ func TestPromoted_PostgresServer(t *testing.T) {
2322
require.NoError(t, err)
2423
defer pool.Close()
2524

26-
client, disconnect := prismatest.NewTestClient(testEnv.DatabaseUrl, t)
27-
defer disconnect()
28-
2925
accountStore := account.NewInPostgres(pool)
3026
chatStore := chat.NewInPostgres(pool)
31-
testStore := NewInPostgres(client)
27+
testStore := NewInPostgres(pool)
3228
teardown := func() {
3329
testStore.(*store).reset()
3430
}

promoted/postgres/store.go

Lines changed: 25 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,70 +3,43 @@ package postgres
33
import (
44
"context"
55

6+
"github.com/jackc/pgx/v5/pgxpool"
7+
68
commonpb "github.com/code-payments/flipchat-protobuf-api/generated/go/common/v1"
7-
pg "github.com/code-payments/flipchat-server/database/postgres"
89

9-
"github.com/code-payments/flipchat-server/database/prisma/db"
1010
"github.com/code-payments/flipchat-server/promoted"
1111
)
1212

1313
type store struct {
14-
client *db.PrismaClient
15-
}
16-
17-
// reset clears the PromotedChat table (used for testing).
18-
func (s *store) reset() {
19-
ctx := context.Background()
20-
21-
promotedChats := s.client.PromotedChat.FindMany().Delete().Tx()
22-
err := s.client.Prisma.Transaction(promotedChats).Exec(ctx)
23-
if err != nil {
24-
panic(err)
25-
}
14+
pool *pgxpool.Pool
2615
}
2716

2817
// NewInPostgres creates a new PostgreSQL store for Promoted Chats.
29-
func NewInPostgres(client *db.PrismaClient) promoted.Store {
18+
func NewInPostgres(pool *pgxpool.Pool) promoted.Store {
3019
return &store{
31-
client: client,
20+
pool: pool,
3221
}
3322
}
3423

3524
// GetPromotedChats retrieves promoted chats by topic from PostgreSQL.
3625
func (s *store) GetPromotedChats(ctx context.Context, topic string) ([]*promoted.PromotedChat, error) {
37-
38-
prChats, err := s.client.PromotedChat.FindMany(
39-
db.PromotedChat.Topic.Equals(topic),
40-
).OrderBy(
41-
db.PromotedChat.Score.Order(db.SortOrderDesc),
42-
).Exec(ctx)
26+
models, err := dbGetPromotedChats(ctx, s.pool, topic)
4327
if err != nil {
4428
return nil, err
4529
}
4630

47-
var chats []*promoted.PromotedChat
48-
for _, prChat := range prChats {
49-
50-
decodedChatID, err := pg.Decode(prChat.ChatID)
31+
res := make([]*promoted.PromotedChat, len(models))
32+
for i, model := range models {
33+
res[i], err = fromModel(model)
5134
if err != nil {
5235
return nil, err
5336
}
54-
55-
chats = append(chats, &promoted.PromotedChat{
56-
ChatID: &commonpb.ChatId{Value: decodedChatID},
57-
Score: prChat.Score,
58-
Topic: prChat.Topic,
59-
CreatedAt: prChat.CreatedAt,
60-
UpdatedAt: prChat.UpdatedAt,
61-
})
6237
}
63-
64-
return chats, nil
38+
return res, nil
6539
}
6640

6741
// PromoteChat promotes a chat (or updates the score if it already exists).
6842
func (s *store) PromoteChat(ctx context.Context, chatID *commonpb.ChatId, topic string, score int) error {
69-
7043
if chatID == nil {
7144
return promoted.ErrInvalidChatID
7245
}
@@ -79,22 +52,16 @@ func (s *store) PromoteChat(ctx context.Context, chatID *commonpb.ChatId, topic
7952
return promoted.ErrInvalidTopic
8053
}
8154

82-
encodedChatID := pg.Encode(chatID.Value)
83-
84-
_, err := s.client.PromotedChat.UpsertOne(
85-
db.PromotedChat.ChatIDTopic(
86-
db.PromotedChat.ChatID.Equals(encodedChatID),
87-
db.PromotedChat.Topic.Equals(topic),
88-
),
89-
).Create(
90-
db.PromotedChat.Chat.Link(db.Chat.ID.Equals(encodedChatID)),
91-
db.PromotedChat.Topic.Set(topic),
92-
db.PromotedChat.Score.Set(score),
93-
).Update(
94-
db.PromotedChat.Score.Set(score),
95-
).Exec(ctx)
96-
97-
return err
55+
model, err := toModel(&promoted.PromotedChat{
56+
ChatID: chatID,
57+
Topic: topic,
58+
Score: score,
59+
})
60+
if err != nil {
61+
return err
62+
}
63+
64+
return model.dbUpsert(ctx, s.pool)
9865
}
9966

10067
func (s *store) DemoteChat(ctx context.Context, chatID *commonpb.ChatId, topic string) error {
@@ -106,16 +73,12 @@ func (s *store) DemoteChat(ctx context.Context, chatID *commonpb.ChatId, topic s
10673
return promoted.ErrInvalidTopic
10774
}
10875

109-
encodedChatID := pg.Encode(chatID.Value)
110-
111-
_, err := s.client.PromotedChat.FindMany(
112-
db.PromotedChat.ChatID.Equals(encodedChatID),
113-
db.PromotedChat.Topic.Equals(topic),
114-
).Delete().Exec(ctx)
76+
return dbDemoteChat(ctx, s.pool, chatID, topic)
77+
}
11578

79+
func (s *store) reset() {
80+
_, err := s.pool.Exec(context.Background(), "DELETE FROM "+promotedChatsTableName)
11681
if err != nil {
117-
return promoted.ErrNotFound
82+
panic(err)
11883
}
119-
120-
return nil
12184
}

promoted/postgres/store_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/stretchr/testify/require"
1010

1111
chat "github.com/code-payments/flipchat-server/chat/postgres"
12-
prismatest "github.com/code-payments/flipchat-server/database/prisma/test"
1312

1413
"github.com/code-payments/flipchat-server/promoted/tests"
1514

@@ -22,11 +21,8 @@ func TestPromoted_PostgresStore(t *testing.T) {
2221
require.NoError(t, err)
2322
defer pool.Close()
2423

25-
client, disconnect := prismatest.NewTestClient(testEnv.DatabaseUrl, t)
26-
defer disconnect()
27-
2824
chatStore := chat.NewInPostgres(pool)
29-
testStore := NewInPostgres(client)
25+
testStore := NewInPostgres(pool)
3026
teardown := func() {
3127
testStore.(*store).reset()
3228
}

0 commit comments

Comments
 (0)