Skip to content

Commit 419f35e

Browse files
committed
push: remove prisma from postgres store
1 parent 854054f commit 419f35e

File tree

9 files changed

+190
-166
lines changed

9 files changed

+190
-166
lines changed

intent/postgres/model.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
const (
15-
intentsTableName = `flipchat_intents`
15+
intentsTableName = "flipchat_intents"
1616
allIntentFields = `"id", "isFulfilled", "createdAt", "updatedAt"`
1717
)
1818

intent/postgres/store.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ import (
99
"github.com/code-payments/flipchat-server/intent"
1010
)
1111

12-
const (
13-
metricsStructName = "intent.postgres.store"
14-
)
15-
1612
type store struct {
1713
pool *pgxpool.Pool
1814
}

push/postgres/messaging_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ 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
chat "github.com/code-payments/flipchat-server/chat/postgres"
@@ -23,10 +22,7 @@ func TestPush_PostgresMessaging(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-
29-
pushes := NewInPostgres(client)
25+
pushes := NewInPostgres(pool)
3026
profiles := profile.NewInPostgres(pool)
3127
chats := chat.NewInPostgres(pool)
3228

push/postgres/model.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package postgres
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
8+
"github.com/georgysavva/scany/v2/pgxscan"
9+
"github.com/jackc/pgx/v5/pgxpool"
10+
11+
commonpb "github.com/code-payments/flipchat-protobuf-api/generated/go/common/v1"
12+
pushpb "github.com/code-payments/flipchat-protobuf-api/generated/go/push/v1"
13+
14+
pg "github.com/code-payments/flipchat-server/database/postgres"
15+
"github.com/code-payments/flipchat-server/push"
16+
)
17+
18+
const (
19+
pushTokensTableName = "flipchat_pushtokens"
20+
allPushTokenFields = `"userId", "appInstallId", "token", "type", "createdAt", "updatedAt"`
21+
)
22+
23+
type model struct {
24+
UserID string `db:"userId"`
25+
AppInstallID string `db:"appInstallId"`
26+
Token string `db:"token"`
27+
Type int `db:"type"`
28+
CreatedAt time.Time `db:"createdAt"`
29+
UpdatedAt time.Time `db:"updatedAt"`
30+
}
31+
32+
func toModel(userID *commonpb.UserId, token push.Token) (*model, error) {
33+
return &model{
34+
UserID: pg.Encode(userID.Value),
35+
AppInstallID: token.AppInstallID,
36+
Token: token.Token,
37+
Type: int(token.Type),
38+
}, nil
39+
}
40+
41+
func fromModel(m *model) (push.Token, error) {
42+
return push.Token{
43+
Type: pushpb.TokenType(m.Type),
44+
AppInstallID: m.AppInstallID,
45+
Token: m.Token,
46+
}, nil
47+
}
48+
49+
func (m *model) dbAdd(ctx context.Context, pool *pgxpool.Pool) error {
50+
query := `INSERT INTO ` + pushTokensTableName + ` (` + allPushTokenFields + `) VALUES ($1, $2, $3, $4, NOW(), NOW()) ON CONFLICT ("userId", "appInstallId") DO UPDATE SET "token" = $3, "updatedAt" = NOW() WHERE ` + pushTokensTableName + `."userId" = $1 AND ` + pushTokensTableName + `."appInstallId" = $2 RETURNING ` + allPushTokenFields
51+
return pgxscan.Get(
52+
ctx,
53+
pool,
54+
m,
55+
query,
56+
m.UserID,
57+
m.AppInstallID,
58+
m.Token,
59+
m.Type,
60+
)
61+
}
62+
63+
func dbGetTokensBatch(ctx context.Context, pool *pgxpool.Pool, userIDs ...*commonpb.UserId) ([]*model, error) {
64+
var res []*model
65+
66+
queryParameters := make([]any, len(userIDs))
67+
68+
query := `SELECT ` + allPushTokenFields + ` FROM ` + pushTokensTableName + ` WHERE "userId" IN (`
69+
for i, userID := range userIDs {
70+
queryParameters[i] = pg.Encode(userID.Value)
71+
if i > 0 {
72+
query += fmt.Sprintf(",$%d", i+1)
73+
} else {
74+
query += fmt.Sprintf("$%d", i+1)
75+
}
76+
}
77+
query += ")"
78+
79+
err := pgxscan.Select(
80+
ctx,
81+
pool,
82+
&res,
83+
query,
84+
queryParameters...,
85+
)
86+
if err != nil {
87+
if pgxscan.NotFound(err) {
88+
return nil, nil
89+
}
90+
return nil, err
91+
}
92+
return res, nil
93+
}
94+
95+
func dbDeleteToken(ctx context.Context, pool *pgxpool.Pool, tokenType pushpb.TokenType, token string) error {
96+
query := `DELETE FROM ` + pushTokensTableName + ` WHERE "token" = $1 and "type" = $2`
97+
_, err := pool.Exec(ctx, query, token, tokenType)
98+
return err
99+
}
100+
101+
func dbClearTokens(ctx context.Context, pool *pgxpool.Pool, userID *commonpb.UserId) error {
102+
query := `DELETE FROM ` + pushTokensTableName + ` WHERE "userId" = $1`
103+
_, err := pool.Exec(ctx, query, pg.Encode(userID.Value))
104+
return err
105+
}

push/postgres/pusher_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/push/tests"
1112

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

1517
func TestPush_PostgresPusher(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
}

push/postgres/server_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/push/tests"
1112

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

1517
func TestPush_PostgresServer(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
}

0 commit comments

Comments
 (0)