Skip to content

Commit

Permalink
chore: Refactor db layer (#45)
Browse files Browse the repository at this point in the history
sqlc is amazing but because we want to have internal methods like
save/update and have a exported method to handle them both we need our
own interface and layer for this.

We have now made everything use our repository so that use this
abstraction across the repository (and have generated mock code to avoid
calling the db)
  • Loading branch information
kevinrobayna authored Oct 3, 2023
1 parent ef34ad7 commit 1f9c0fa
Show file tree
Hide file tree
Showing 18 changed files with 342 additions and 297 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
slack/slackclient/mock_slackclient linguist-generated
lib/db/mock_db linguist-generated
gen linguist-generated
11 changes: 5 additions & 6 deletions assets/queries/rotas.sql → assets/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ from ROTAS
WHERE ROTAS.CHANNEL_ID = $1
AND ROTAS.TEAM_ID = $2;

-- name: SaveRota :one
-- name: saveRota :one
INSERT INTO ROTAS (TEAM_ID, CHANNEL_ID, NAME, METADATA)
VALUES ($1, $2, $3, $4) RETURNING ID;

-- name: UpdateRota :one
-- name: updateRota :one
UPDATE ROTAS
SET NAME = $1,
METADATA = $2
WHERE ID = $3
RETURNING ID;
SET NAME = $1,
METADATA = $2
WHERE ID = $3 RETURNING ID;
3 changes: 0 additions & 3 deletions cmd/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"go.uber.org/zap/zapio"

genSlack "github.com/rotabot-io/rotabot/gen/slack"
"github.com/rotabot-io/rotabot/lib/db"
"github.com/rotabot-io/rotabot/lib/middleware"
"github.com/rotabot-io/rotabot/lib/zapctx"
)
Expand All @@ -30,8 +29,6 @@ type ServerParams struct {
AppComponent string
MetricsComponent string

Queries *db.Queries

SlackSigningSecret string
SlackService genSlack.Service

Expand Down
21 changes: 0 additions & 21 deletions lib/db/metadata.go

This file was deleted.

85 changes: 85 additions & 0 deletions lib/db/mock_db/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions lib/db/queries.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package db

import (
"context"
"errors"

"github.com/jackc/pgx/v5/pgconn"
"github.com/rotabot-io/rotabot/lib/zapctx"
"go.uber.org/zap"
)

type CreateOrUpdateRotaParams struct {
RotaID string
TeamID string
ChannelID string
Name string
Metadata RotaMetadata
}

func (q *Queries) CreateOrUpdateRota(ctx context.Context, p CreateOrUpdateRotaParams) (string, error) {
l := zapctx.Logger(ctx)
var rotaId string
var err error
if p.RotaID != "" {
rotaId, err = q.updateRota(ctx, updateRotaParams{
ID: p.RotaID,
Name: p.Name,
Metadata: p.Metadata,
})
} else {
rotaId, err = q.saveRota(ctx, saveRotaParams{
Name: p.Name,
TeamID: p.TeamID,
ChannelID: p.ChannelID,
Metadata: p.Metadata,
})
}
if err != nil {
var pgError *pgconn.PgError
if errors.As(err, &pgError) {
switch pgError.Code {
case "23505":
return "", ErrAlreadyExists
}
}
l.Error("failed to save rota", zap.Error(err))
return "", err
}
return rotaId, nil
}
23 changes: 11 additions & 12 deletions lib/db/rotas.sql.go → lib/db/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1f9c0fa

Please sign in to comment.