Skip to content

Commit

Permalink
Continue sqlc migration
Browse files Browse the repository at this point in the history
  • Loading branch information
neekolas committed Apr 26, 2024
1 parent 520f1c1 commit 1163957
Show file tree
Hide file tree
Showing 9 changed files with 454 additions and 230 deletions.
6 changes: 3 additions & 3 deletions pkg/metrics/mls.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"

"github.com/prometheus/client_golang/prometheus"
mlsstore "github.com/xmtp/xmtp-node-go/pkg/mls/store"
"github.com/xmtp/xmtp-node-go/pkg/mls/store/queries"
"go.uber.org/zap"
)

Expand All @@ -25,7 +25,7 @@ var mlsSentGroupMessageCount = prometheus.NewCounterVec(
appClientVersionTagKeys,
)

func EmitMLSSentGroupMessage(ctx context.Context, log *zap.Logger, msg *mlsstore.GroupMessage) {
func EmitMLSSentGroupMessage(ctx context.Context, log *zap.Logger, msg *queries.GroupMessage) {
labels := contextLabels(ctx)
mlsSentGroupMessageSize.With(labels).Observe(float64(len(msg.Data)))
mlsSentGroupMessageCount.With(labels).Inc()
Expand All @@ -48,7 +48,7 @@ var mlsSentWelcomeMessageCount = prometheus.NewCounterVec(
appClientVersionTagKeys,
)

func EmitMLSSentWelcomeMessage(ctx context.Context, log *zap.Logger, msg *mlsstore.WelcomeMessage) {
func EmitMLSSentWelcomeMessage(ctx context.Context, log *zap.Logger, msg *queries.WelcomeMessage) {
labels := contextLabels(ctx)
mlsSentWelcomeMessageSize.With(labels).Observe(float64(len(msg.Data)))
mlsSentWelcomeMessageCount.With(labels).Inc()
Expand Down
6 changes: 3 additions & 3 deletions pkg/mls/api/v1/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,9 @@ func (s *Service) SendGroupMessages(ctx context.Context, req *mlsv1.SendGroupMes
msgB, err := pb.Marshal(&mlsv1.GroupMessage{
Version: &mlsv1.GroupMessage_V1_{
V1: &mlsv1.GroupMessage_V1{
Id: msg.Id,
Id: uint64(msg.ID),
CreatedNs: uint64(msg.CreatedAt.UnixNano()),
GroupId: msg.GroupId,
GroupId: msg.GroupID,
Data: msg.Data,
},
},
Expand Down Expand Up @@ -312,7 +312,7 @@ func (s *Service) SendWelcomeMessages(ctx context.Context, req *mlsv1.SendWelcom
msgB, err := pb.Marshal(&mlsv1.WelcomeMessage{
Version: &mlsv1.WelcomeMessage_V1_{
V1: &mlsv1.WelcomeMessage_V1{
Id: msg.Id,
Id: uint64(msg.ID),
CreatedNs: uint64(msg.CreatedAt.UnixNano()),
InstallationKey: msg.InstallationKey,
Data: msg.Data,
Expand Down
11 changes: 5 additions & 6 deletions pkg/mls/api/v1/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/uptrace/bun"
wakupb "github.com/waku-org/go-waku/waku/v2/protocol/pb"
mlsstore "github.com/xmtp/xmtp-node-go/pkg/mls/store"
"github.com/xmtp/xmtp-node-go/pkg/mls/store/queries"
"github.com/xmtp/xmtp-node-go/pkg/mlsvalidate"
"github.com/xmtp/xmtp-node-go/pkg/proto/identity/associations"
mlsv1 "github.com/xmtp/xmtp-node-go/pkg/proto/mls/api/v1"
Expand Down Expand Up @@ -120,12 +121,10 @@ func TestRegisterInstallation(t *testing.T) {
require.NoError(t, err)
require.Equal(t, installationId, res.InstallationKey)

installations := []mlsstore.Installation{}
err = mlsDb.NewSelect().Model(&installations).Where("id = ?", installationId).Scan(ctx)
installation, err := queries.New(mlsDb.DB).GetInstallation(ctx, installationId)
require.NoError(t, err)

require.Len(t, installations, 1)
require.Equal(t, accountAddress, installations[0].WalletAddress)
require.Equal(t, accountAddress, installation.WalletAddress)
}

func TestRegisterInstallationError(t *testing.T) {
Expand Down Expand Up @@ -170,9 +169,9 @@ func TestUploadKeyPackage(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, uploadRes)

installation := &mlsstore.Installation{}
err = mlsDb.NewSelect().Model(installation).Where("id = ?", installationId).Scan(ctx)
installation, err := queries.New(mlsDb.DB).GetInstallation(ctx, installationId)
require.NoError(t, err)
require.Equal(t, accountAddress, installation.WalletAddress)
}

func TestFetchKeyPackages(t *testing.T) {
Expand Down
58 changes: 0 additions & 58 deletions pkg/mls/store/models.go

This file was deleted.

86 changes: 72 additions & 14 deletions pkg/mls/store/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ FROM
SELECT
*
FROM
json_populate_recordset(NULL::inbox_filter, sqlc.arg(filters)) AS b(inbox_id,
json_populate_recordset(NULL::inbox_filter, @filters) AS b(inbox_id,
sequence_id)) AS b ON b.inbox_id = a.inbox_id
AND a.sequence_id > b.sequence_id
ORDER BY
Expand Down Expand Up @@ -79,6 +79,14 @@ WHERE (address, inbox_id, association_sequence_id) =(
INSERT INTO installations(id, wallet_address, created_at, updated_at, credential_identity, key_package, expiration)
VALUES ($1, $2, $3, $3, $4, $5, $6);

-- name: GetInstallation :one
SELECT
*
FROM
installations
WHERE
id = $1;

-- name: UpdateKeyPackage :execrows
UPDATE
installations
Expand All @@ -96,7 +104,7 @@ SELECT
FROM
installations
WHERE
id = ANY (sqlc.arg(installation_ids)::BYTEA[]);
id = ANY (@installation_ids::BYTEA[]);

-- name: GetIdentityUpdates :many
SELECT
Expand Down Expand Up @@ -131,49 +139,99 @@ INSERT INTO welcome_messages(installation_key, data, installation_key_data_hash,
RETURNING
*;

-- name: QueryGroupMessagesAsc :many
-- name: GetAllGroupMessages :many
SELECT
*
FROM
group_messages
ORDER BY
id ASC;

-- name: QueryGroupMessages :many
SELECT
*
FROM
group_messages
WHERE
group_id = @group_id
ORDER BY
CASE WHEN @sort_desc::BOOL THEN
id
END DESC,
CASE WHEN @sort_desc::BOOL = FALSE THEN
id
END ASC
LIMIT @numrows;

-- name: QueryGroupMessagesWithCursorAsc :many
SELECT
*
FROM
group_messages
WHERE
group_id = @group_id
AND id > @cursor
ORDER BY
id ASC
LIMIT @numrows;

-- name: QueryGroupMessagesDesc :many
-- name: QueryGroupMessagesWithCursorDesc :many
SELECT
*
FROM
group_messages
WHERE
group_id = @group_id
AND id < @cursor
ORDER BY
id DESC
LIMIT @numrows;

-- name: QueryGroupMessagesWithCursorAsc :many
-- name: GetAllWelcomeMessages :many
SELECT
*
FROM
group_messages
welcome_messages
ORDER BY
id ASC;

-- name: QueryWelcomeMessages :many
SELECT
*
FROM
welcome_messages
WHERE
installation_key = @installation_key
ORDER BY
CASE WHEN @sort_desc::BOOL THEN
id
END DESC,
CASE WHEN @sort_desc::BOOL = FALSE THEN
id
END ASC
LIMIT @numrows;

-- name: QueryWelcomeMessagesWithCursorAsc :many
SELECT
*
FROM
welcome_messages
WHERE
group_id = $1
AND id > $2
installation_key = @installation_key
AND id > @cursor
ORDER BY
id ASC
LIMIT $3;
LIMIT @numrows;

-- name: QueryGroupMessagesWithCursorDesc :many
-- name: QueryWelcomeMessagesWithCursorDesc :many
SELECT
*
FROM
group_messages
welcome_messages
WHERE
group_id = $1
AND id < $2
installation_key = @installation_key
AND id < @cursor
ORDER BY
id DESC
LIMIT $3;
LIMIT @numrows;

Loading

0 comments on commit 1163957

Please sign in to comment.