goalert icon indicating copy to clipboard operation
goalert copied to clipboard

go: use sqlc for all db calls

Open Forfold opened this issue 1 year ago • 0 comments

The purpose of this issue is to track migrating all prepared DB statements to use sqlc.

How to migrate a prepared statement to `sqlc`
  1. Create a file named queries.sql in the directory you are working in
  2. Add your query with the comment above declaring its name and return type. Here are three examples: a query returning one row, a query returning multiple rows, and a query updating rows (and not returning anything):
-- name: AlertHasEPState :one
SELECT
  EXISTS (
      SELECT
          1
      FROM
          escalation_policy_state
      WHERE
          alert_id = $1) AS has_ep_state;

-- name: AlertFeedback :many
SELECT
  alert_id,
  noise_reason
FROM
  alert_feedback
WHERE
  alert_id = ANY($1::int[]);

-- name: SetAlertFeedback :exec
INSERT INTO alert_feedback(alert_id, noise_reason)
  VALUES ($1, $2)
ON CONFLICT (alert_id)
  DO UPDATE SET
      noise_reason = $2
  WHERE
      alert_feedback.alert_id = $1;
  1. Generate your queries to call with gadb: make generate
  2. Call your query:
row, err := gadb.New(tx).AlertHasEPState(ctx, int64(id))
		if err != nil {
			return fmt.Errorf("check ep state: %w", err)
		}

Notes:

  • Sometimes you'll need to cast types (e.g., $1::int but then it loses the name, so if there's multiple args it will be things like Column1 so it's better to switch to named args, in that case, @id::int
  • If your query is executing (insert or update) but still returning rows, be sure to tag it as :one or :many, respectively. If it doesn't return anything, tag it as :exec after the name.

Files with prepared db statements to migrate to sqlc:

  • [ ] alert/store.go
  • [ ] alert/alerlog/store.go
  • [ ] alert/alertmetrics/store.go
  • [ ] auth/handler.go
  • [ ] auth/basic/db.go
  • [ ] auth/nonce/store.go
  • [ ] config/store.go
  • [ ] engine/backend.go
  • [ ] engine/cleanupmanager/db.go
  • [ ] engine/compatmanager/db.go
  • [ ] engine/escalationmanager/db.go
  • [ ] engine/escalationmanager/update.go
  • [ ] engine/heartbeatmanager/db.go
  • [ ] engine/message/db.go
  • [ ] engine/metricsmanager/db.go
  • [ ] engine/npcyclemanager/db.go
  • [ ] engine/processinglock/conn.go
  • [ ] engine/processinglock/lock.go
  • [ ] engine/rotationmanager/db.go
  • [ ] engine/schedulemanager/db.go
  • [ ] engine/verifymanager/db.go
  • [ ] engine/verifymanager/update.go
  • [ ] escalation/store.go
  • [ ] gadb/db.go
  • [ ] heartbeat/store.go
  • [x] integrationkey/store.go
  • [ ] keyring/store.go
  • [ ] label/store.go
  • [ ] limit/store.go
  • [ ] notice/store.go
  • [ ] notification/store.go
  • [ ] notification/twilio/dbsms.go
  • [ ] notificationchannel/store.go
  • [ ] oncall/store.go
  • [ ] override/store.go
  • [ ] schedule/store.go
  • [ ] schedule/rotation/store.go
  • [ ] schedule/rule/store.go
  • [ ] service/store.go
  • [ ] user/store.go
  • [x] user/contactmethod/store.go
  • [ ] user/favorite/store.go
  • [ ] user/notificationrule/store.go
  • [ ] util/sqlprepare.go

Forfold avatar Aug 18 '23 16:08 Forfold