Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go: use sqlc for all db calls #3235

Open
2 of 44 tasks
Forfold opened this issue Aug 18, 2023 · 0 comments
Open
2 of 44 tasks

go: use sqlc for all db calls #3235

Forfold opened this issue Aug 18, 2023 · 0 comments
Labels
enhancement New feature or request go Pull requests that update Go code help wanted Extra attention is needed tech migration Tasks related to technology, library, or paradigm transitions.

Comments

@Forfold
Copy link
Contributor

Forfold commented Aug 18, 2023

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
  • 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
  • user/contactmethod/store.go
  • user/favorite/store.go
  • user/notificationrule/store.go
  • util/sqlprepare.go
@Forfold Forfold added enhancement New feature or request help wanted Extra attention is needed go Pull requests that update Go code labels Aug 18, 2023
@mastercactapus mastercactapus added the tech migration Tasks related to technology, library, or paradigm transitions. label Oct 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request go Pull requests that update Go code help wanted Extra attention is needed tech migration Tasks related to technology, library, or paradigm transitions.
Projects
None yet
Development

No branches or pull requests

2 participants