Skip to content

Commit 854054f

Browse files
committed
intent: remove prisma from postgres store
1 parent 4322ba7 commit 854054f

File tree

5 files changed

+69
-85
lines changed

5 files changed

+69
-85
lines changed

chat/postgres/server_test.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ import (
66
"context"
77
"testing"
88

9-
prismatest "github.com/code-payments/flipchat-server/database/prisma/test"
109
"github.com/stretchr/testify/require"
1110

1211
account "github.com/code-payments/flipchat-server/account/postgres"
1312
intent "github.com/code-payments/flipchat-server/intent/postgres"
14-
messaging "github.com/code-payments/flipchat-server/messaging/memory"
13+
messaging "github.com/code-payments/flipchat-server/messaging/postgres"
1514
profile "github.com/code-payments/flipchat-server/profile/postgres"
1615

1716
"github.com/code-payments/flipchat-server/chat/tests"
@@ -25,19 +24,17 @@ func TestChat_PostgresServer(t *testing.T) {
2524
require.NoError(t, err)
2625
defer pool.Close()
2726

28-
client, disconnect := prismatest.NewTestClient(testEnv.DatabaseUrl, t)
29-
defer disconnect()
30-
3127
chats := NewInPostgres(pool)
3228
accounts := account.NewInPostgres(pool)
3329
profiles := profile.NewInPostgres(pool)
34-
intents := intent.NewInPostgres(client)
35-
messages := messaging.NewInMemory() // TODO: Implement Postgres messaging
30+
intents := intent.NewInPostgres(pool)
31+
messages := messaging.NewInPostgresMessages(pool)
32+
pointers := messaging.NewInPostgresPointers(pool)
3633

3734
teardown := func() {
3835
chats.(*store).reset()
3936
}
4037

4138
tests.RunServerTests(
42-
t, accounts, profiles, chats, messages, messages, intents, teardown)
39+
t, accounts, profiles, chats, messages, pointers, intents, teardown)
4340
}

intent/postgres/model.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package postgres
2+
3+
import (
4+
"context"
5+
6+
"github.com/georgysavva/scany/v2/pgxscan"
7+
"github.com/jackc/pgx/v5/pgxpool"
8+
9+
commonpb "github.com/code-payments/flipchat-protobuf-api/generated/go/common/v1"
10+
11+
pg "github.com/code-payments/flipchat-server/database/postgres"
12+
)
13+
14+
const (
15+
intentsTableName = `flipchat_intents`
16+
allIntentFields = `"id", "isFulfilled", "createdAt", "updatedAt"`
17+
)
18+
19+
func dbIsFulfilled(ctx context.Context, pool *pgxpool.Pool, id *commonpb.IntentId) (bool, error) {
20+
var res bool
21+
query := `SELECT "isFulfilled" FROM ` + intentsTableName + ` WHERE "id" = $1`
22+
err := pgxscan.Get(
23+
ctx,
24+
pool,
25+
&res,
26+
query,
27+
pg.Encode(id.Value),
28+
)
29+
if pgxscan.NotFound(err) {
30+
return false, nil
31+
}
32+
return res, err
33+
}
34+
35+
func dbMarkFulfilled(ctx context.Context, pool *pgxpool.Pool, id *commonpb.IntentId) error {
36+
query := `INSERT INTO ` + intentsTableName + ` (` + allIntentFields + `) VALUES ($1, true, NOW(), NOW()) ON CONFLICT ("id") DO NOTHING`
37+
_, err := pool.Exec(ctx, query, pg.Encode(id.Value))
38+
return err
39+
}

intent/postgres/store.go

Lines changed: 17 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@ package postgres
22

33
import (
44
"context"
5-
"errors"
65

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

10-
"github.com/code-payments/code-server/pkg/metrics"
11-
12-
"github.com/code-payments/flipchat-server/database/prisma/db"
139
"github.com/code-payments/flipchat-server/intent"
1410
)
1511

@@ -18,79 +14,32 @@ const (
1814
)
1915

2016
type store struct {
21-
client *db.PrismaClient
17+
pool *pgxpool.Pool
2218
}
2319

24-
func NewInPostgres(client *db.PrismaClient) intent.Store {
20+
func NewInPostgres(pool *pgxpool.Pool) intent.Store {
2521
return &store{
26-
client,
27-
}
28-
}
29-
30-
func (s *store) reset() {
31-
ctx := context.Background()
32-
33-
intents := s.client.Intent.FindMany().Delete().Tx()
34-
35-
err := s.client.Prisma.Transaction(intents).Exec(ctx)
36-
if err != nil {
37-
panic(err)
22+
pool: pool,
3823
}
3924
}
4025

4126
func (s *store) IsFulfilled(ctx context.Context, id *commonpb.IntentId) (bool, error) {
42-
tracer := metrics.TraceMethodCall(ctx, metricsStructName, "GetChatID")
43-
defer tracer.End()
44-
45-
res, err := func() (bool, error) {
46-
encodedIntentID := pg.Encode(id.Value, pg.Base58)
47-
48-
intent, err := s.client.Intent.FindFirst(
49-
db.Intent.ID.Equals(encodedIntentID),
50-
).Exec(ctx)
51-
52-
if errors.Is(err, db.ErrNotFound) || intent == nil {
53-
return false, nil
54-
}
55-
56-
return intent.IsFulfilled, nil
57-
}()
58-
59-
tracer.OnError(err)
60-
61-
return res, err
27+
return dbIsFulfilled(ctx, s.pool, id)
6228
}
6329

6430
func (s *store) MarkFulfilled(ctx context.Context, id *commonpb.IntentId) error {
65-
tracer := metrics.TraceMethodCall(ctx, metricsStructName, "MarkFulfilled")
66-
defer tracer.End()
67-
68-
err := func() error {
69-
encodedIntentID := pg.Encode(id.Value, pg.Base58)
70-
71-
ok, err := s.IsFulfilled(ctx, id)
72-
if err != nil {
73-
return err
74-
}
75-
76-
if ok {
77-
return intent.ErrAlreadyFulfilled
78-
}
79-
80-
// Upsert the intent with the new fulfilled status
81-
_, err = s.client.Intent.UpsertOne(
82-
db.Intent.ID.Equals(encodedIntentID),
83-
).Create(
84-
db.Intent.ID.Set(encodedIntentID),
85-
db.Intent.IsFulfilled.Set(true),
86-
).Update(
87-
db.Intent.IsFulfilled.Set(true),
88-
).Exec(ctx)
89-
31+
isFulfilled, err := dbIsFulfilled(ctx, s.pool, id)
32+
if err != nil {
9033
return err
91-
}()
92-
93-
tracer.OnError(err)
34+
} else if isFulfilled {
35+
return intent.ErrAlreadyFulfilled
36+
}
37+
return dbMarkFulfilled(ctx, s.pool, id)
38+
}
9439

95-
return err
40+
func (s *store) reset() {
41+
_, err := s.pool.Exec(context.Background(), "DELETE FROM "+intentsTableName)
42+
if err != nil {
43+
panic(err)
44+
}
9645
}

intent/postgres/store_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,23 @@
33
package postgres
44

55
import (
6+
"context"
67
"testing"
78

8-
prismatest "github.com/code-payments/flipchat-server/database/prisma/test"
9+
"github.com/stretchr/testify/require"
910

1011
"github.com/code-payments/flipchat-server/intent/tests"
1112

13+
"github.com/jackc/pgx/v5/pgxpool"
1214
_ "github.com/jackc/pgx/v5/stdlib"
1315
)
1416

1517
func TestIntent_PostgresStore(t *testing.T) {
16-
client, disconnect := prismatest.NewTestClient(testEnv.DatabaseUrl, t)
17-
defer disconnect()
18+
pool, err := pgxpool.New(context.Background(), testEnv.DatabaseUrl)
19+
require.NoError(t, err)
20+
defer pool.Close()
1821

19-
testStore := NewInPostgres(client)
22+
testStore := NewInPostgres(pool)
2023
teardown := func() {
2124
testStore.(*store).reset()
2225
}

messaging/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
intent "github.com/code-payments/flipchat-server/intent/postgres"
1514

1615
"github.com/code-payments/flipchat-server/messaging/tests"
@@ -24,12 +23,9 @@ func TestMessaging_PostgresServer(t *testing.T) {
2423
require.NoError(t, err)
2524
defer pool.Close()
2625

27-
client, disconnect := prismatest.NewTestClient(testEnv.DatabaseUrl, t)
28-
defer disconnect()
29-
3026
accounts := account.NewInPostgres(pool)
3127
chats := chat.NewInPostgres(pool)
32-
intents := intent.NewInPostgres(client)
28+
intents := intent.NewInPostgres(pool)
3329
messages := NewInPostgresMessages(pool)
3430
pointers := NewInPostgresPointers(pool)
3531

0 commit comments

Comments
 (0)