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

Feat/connect db #11

Merged
merged 12 commits into from
Jan 22, 2025
Merged

Feat/connect db #11

merged 12 commits into from
Jan 22, 2025

Conversation

pilyang
Copy link
Member

@pilyang pilyang commented Jan 17, 2025

관련 이슈

close #8

변경 내용

  • ent 초기 설정
  • db 연결 코드 작성
  • Company 스키마(모델) 추가
    • ent 기본 코드들 자동생성하려면 초기 스키마를 하나 추가해야해서 임의로 추가
  • config 분리

요거 확인 부탁

  1. 전반적인 파일 구조등

파일 구조와 분리등을 어케하지 아직 감이 잘 안오네용.. ent관련은 문서대로 프레임워크에서 시키는대로 일단 진행했고,
디비연결과 configuration쪽은 분리를 좀 했습니다.
이부분 어떤지나 한번 확인 부탁드려요...

  1. company 스키마

company는 우선

회사명
로고
블로그주소
rss 링크

필드를 모두 not null로 가지고 있긴 한데,, 이것도 더 필요할 필드나 아님 수정 필요한 부분 있을지 의견 주셔용
아직 운영디비 반영 X => 배포 전 테이블 생성 필요
(이부분도 스키마파일에 변동 생기면 자동으로 디비에 반영하라고 코멘트 남겨주는 워크플로우 나중에 하나 추가하면 좋것다)

  1. 로컬환경 디비 마이그레이션 방식 (중요)

우선 운영은 스키마 확인하고 쿼리 확인하면서 수동으로 마이그레이션 작업

로컬과 테스트디비의 경우 어케할지가 고민인데, 일단 여기에서 migrations내에 생성된 sql파일들은 지금으로선 사용하는게 아니라 버젼 기록용의 의미가 있게됨. 지금코드대로라면..
이유 : 지금은 local환경이면 ent에서 자동으로 디비 마이그래이션 작업 하도록 해둠 (spring의 ddl auto정도로 생각하면 될듯 우선)

그런데 이거를 atlas로 마이그래이션 관리하면 코드실행시 자동 마이그래이션이 아닌 atlas도구 사용해서 로컬에서 직접 마이그래이션 할 수 있도록 하는게 좋을까 고민이됨.
그렇게 되면 실제로 운영에 실행한 쿼리로 migrations들을 잘 관리하면서 좀 더 잘 관리가 될라나 싶은,,,

이거 내용 파악 위해서 ent에서 자동생성해서 실행되는 마이그래이션 쿼리와, atlas에서 자동생성하는 쿼리 차이가 있는지등 좀 더 확인하고 고민해볼 필요가 있을것 같긴 함

(이건 회의때 좀 더 자세히 이야기해봐용.. 노션에도 정리해둘께)

참고해야할 내용

  • ent 코드 generate나 migrations 자동생성등 플로우는 ent 공식문서 참고
    • 관련 내용들은 추후 notion에 업데이트 및 정리 예정
    • ent 하위파일은 기본적으로 자동생성되는 파일들이고, schema하위 파일에서 필요한 내용 수정 후 코드 generate진행
  • migrations 관리 : atlas
  • 서버 라우터등 설정은 후에 따로 분리하려고 여기서는 안건들임, 작업하면서 내용 확인후 맞춰서 변경해야할 것 같음

@pilyang pilyang requested a review from iamjooon2 January 17, 2025 12:57
@pilyang pilyang self-assigned this Jan 17, 2025
Copy link

report: Run #45

Tests 📝 Passed ✅ Failed ❌ Skipped ⏭️ Pending ⏳ Other ❓ Flaky 🍂 Duration ⏱️
1 1 0 0 0 0 0 1ms

🎉 All tests passed!

Github Test Reporter

Copy link
Contributor

@iamjooon2 iamjooon2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생하셨습니다!

운영환경 테이블 관리는 있다가 얘기해보아요ㅎㅎ

Comment on lines 1 to 13
#!/bin/bash

if [ -z "$1" ]; then
echo "Usage: $0 <migration_name>"
exit 1
fi

MIGRATION_NAME=$1

atlas migrate diff "$MIGRATION_NAME" \
--dir "file://ent/migrate/migrations" \
--to "ent://ent/schema" \
--dev-url "docker://postgres/15/test?search_path=public"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[의견] 쉘 스크립트도 폴더 하나에 몰아두면 어떨까요?!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[대답] 좋아용!!! 안그래도 모아야할라나 고민이 있긴 했는데, 의견 같으면 그대로 모으는게 좋을 것 같네용 앞으로도 머가 또 생길 지 모르고 이미 3개나 되고

@@ -0,0 +1,2 @@
-- Create "companies" table
CREATE TABLE "companies" ("id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, "create_time" timestamptz NOT NULL, "update_time" timestamptz NOT NULL, "name" character varying NOT NULL, "logo_url" character varying NOT NULL, "blog_url" character varying NOT NULL, "rss_url" character varying NOT NULL, PRIMARY KEY ("id"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

굳..이 고민되는건 soft delete 정도?! 있고 괜찮은 것 같슴당

사담 - auto increment가 mysql에서 쓰는 이름이었다는게 충격 그잡채입니다

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

음 그러게 soft delete한번 고민해봐야겟네용

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

추가해보았어용~~ㅋㅋㅋㅋ
공식문서 보고 그대로 적용했고,
확인하려면 ent go mixin, hook, interceptor에 대해서 확인을 해봐용... 설명 필요하면 나중에 따로 시간잡고 한번 설명해드리것습니다

사담 - sqlc로 그냥 슥슥 할껄... 차피 쿼리 잘 짤줄 아는데,,,ㅋㅋㅋㅋㅋㅋㅋㅋ

@@ -0,0 +1,97 @@
package config
Copy link
Contributor

@iamjooon2 iamjooon2 Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 지금 config.go로도 괜찮은 것 같습니당

고랭 컨셉이 일단 짜고보는(?) 걸 지향하다보니
오히려 직관적이라는 생각이 드네용

config 개수가 늘어나거나(4~5개) 설정파일 뎁스가 늘어나면(config -> Aconfig -> BConfig 식으로 의존하게된다면) 그때 고민해봐도 좋을 것 같아요!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ㅋㅋㅋㅋㅋㅋㅋ good good .
사실 만들면서 처음엔 자프링하듯이 디비랑 서버 config분리해서 만들다가 이게 맞나...? 하고 그냥 롤백하고 하나로 합쳐서 작업했슴다.

일단 그냥 만들기 말은 편한데 계속 이게 맞나 고미이 드면서 아직 어렵네용

Copy link

report: Run #46

Tests 📝 Passed ✅ Failed ❌ Skipped ⏭️ Pending ⏳ Other ❓ Flaky 🍂 Duration ⏱️
1 1 0 0 0 0 0 1ms

🎉 All tests passed!

Github Test Reporter

Comment on lines +23 to +34
urlField := func(name string) ent.Field {
return field.String(name).
GoType(&url.URL{}).
ValueScanner(field.BinaryValueScanner[*url.URL]{})
}

return []ent.Field{
field.String("name"),
urlField("logo_url"),
urlField("blog_url"),
urlField("rss_url"),
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iamjooon2 아 추가로 고민되는것...

여기서 url필드들을 객체로 꺼낼때 url.URL로 파싱해서 가져오도록 했는데, 이때 validation등이 될 수 있도록..

사실상 우리는 logo url, blog url등은 바로 클라이언트에 내려주는 용도로 사용되는거고,,, URL을 이용한 동작들이 많지는 않을것같아서.. 그냥 Company Struct에도 String으로 가지고 있게 해주는게 좋으려나 싶네

어케 생각하나용???

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

entgo가 해주는 거라고 생각해서 db 스키마만 봤는데, schema 부분은 직접 만진 거였구만

난 큰 부분은 아닌 것 같아서 어느방향이든 괜춘!

Copy link

report: Run #47

Tests 📝 Passed ✅ Failed ❌ Skipped ⏭️ Pending ⏳ Other ❓ Flaky 🍂 Duration ⏱️
1 1 0 0 0 0 0 1ms

🎉 All tests passed!

Github Test Reporter

@pilyang
Copy link
Member Author

pilyang commented Jan 18, 2025

작업을 하다보니 go generate를 이용해서 (또는 atlas 등을 통해서) 생성된 코드와 파일은 레포에 올라가는게 좋을까 아닐까에 대한 고민을 하게되서... 일단 해당 내용에 대한 커뮤니티글

커밋한다(대다수), 안한다(소수이긴 하지만) 모든 의견이 있지만, 일단 내 생각으론 커밋을 하고, 커밋메시지에 chore(ent)이런것처럼 자동생성된 파일들은 따로 커밋해서 표기하는게 어떨까 하네용...

문제가 merge conflict가 생길 수 있다긴 한데,,, 이건 뭐 뒤에 머지시킬거에서 pull받고 해당 내용 포함해서 다시 generate하면서 해결하는거로 하면 될것같고
글 보다보니 커밋해서 올리고 CI workflow에서도 다시한번 generate해서 바뀐거 있나 체크 후 문제 없이 잘 되어있는지 확인한다는 사람도 있는데,, 이런건 나중에 여유나면 함 해봐도 재밌을것같네ㅋㅋ

@pilyang pilyang merged commit c4f8ab0 into main Jan 22, 2025
2 checks passed
@iamjooon2 iamjooon2 deleted the feat/connect-db branch January 22, 2025 06:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DB관련 설정
2 participants