-
Notifications
You must be signed in to change notification settings - Fork 0
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
[EPIC] sqlc 도입 #69
Merged
[EPIC] sqlc 도입 #69
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
barabobBOB
approved these changes
Apr 26, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
달아주신 코멘트도 모두 확인했습니다 :)
🙇 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Epic 진행상황
sqlc
sqlc는 타입 세이프하게 SQL 코드를 Go 코드로 변환시켜주는 툴입니다.
기존의 database/sql이나 pgx 등의 DB 클라이언트를 지원해서 그대로 사용할 수 있습니다.
jet와 goqu도 좋은 평가를 받고 있어서 jet으로 시도했었는데요.
서베이한 결과, sqlc가 제일 깔끔하고 우리 니즈와 개발 플로우에 맞는 것 같습니다. (SQL 손수 작성 -> 코드 매핑 => 자동화)
SQL 쿼리는 그대로 쓰면서 쿼리 빌딩 자체를 안해도 되는 게 좋았습니다.
sqlc 사용법
sqlc를 설치합니다.
go-migrate를 통해 db/migrations 밑에 있는 sql 마이그레이션 파일과
queries/*sql
파일들을 순차적으로 읽어databasegen
패키지 (internal/database/gen
경로)에 readonlydb.go
,model.go
,queries.go
를 생성합니다. (참고)마이그레이션이 생길 때마다 돌려주면 새로운 모델, 쿼리를 사용할 수 있습니다.
컨디셔널 쿼리 작성할 때 조금의 주의사항이 필요한데 공식문서 보시거나
queries/users.sql
을 참고하시면 됩니다.참고사항
사실 유용한 기능이 더 있는데 조금씩 도입해보겠습니다.