Open dongwooklee96 opened 3 years ago
해당 이슈는 다음과 같이 해결할 수 있다.
구현체로 Hibernate를 사용하기 때문에 spring.jpa.hibernate.ddl-auto
옵션을 통해서 보다 상세한 데이터베이스 초기화 전략을 설정할 수 있다.
다음 목록중 하나를 spring.jpa.hibernate.ddl-auto
옵션의 값으로 지정할 수 있다.
none
: 아무것도 실행하지 않는다 (대부분의 DB에서 기본값이다)create-drop
: SessionFactory
가 시작될 때 drop
및 생성을 실행하고, SessionFactory
가 종료될 때 drop
을 실행한다 (in-memory DB의 경우 기본값이다)create
: SessionFactory
가 시작될 때 데이터베이스 drop을 실행하고 생성된 DDL을 실행한다update
: 변경된 스키마를 적용한다validate
: 변경된 스키마가 있다면 변경점을 출력하고 애플리케이션을 종료한다위 초기화 전략을 기반으로 문제점을 해결 할 수 있을것 같다.
참고 자료 : https://pravusid.kr/java/2018/10/10/spring-database-initialization.html
@KIMHWANSEUNG
그렇다면, 배포 환경에 따라서 spring.jpa.hibernate.ddl-auto
설정 값이 달라지도록 설정하면 좋을 것 같습니다.
DEV
환경에서는 spring.jpa.hibernate.ddl-auto
값이 create-drop
으로 구성하고none
설정이 적용되고, 직접 DDL
문을 실행하는 방법
이에 대한 방법을 찾고 기여해주시면 좋겠습니다.@dongwooklee96
해결방법은 spring.profiles.active
를 활용하면 됩니다.
application.yml에 dev
와 prod
환경을 각각 코드로 써놓고 ---
로 나누며 직접적으로 프로젝트에 적용할 환경을 코드에서spring.profiles.active= {$적용할 환경}
으로 선언하여 설정하면 됩니다.
예시로 아래와 같이 코드를 쓰면 될것 같습니다 !
spring:
profiles:
active: dev
---
spring:
profiles: dev
datasource:
hikari:
maximum-pool-size: 4
url: jdbc:postgresql://localhost:5432/postgres
username: postgres
password: pass
platform: postgres
jpa:
hibernate:
ddl-auto: update
properties:
hibernate:
default_schema: db
jwt:
secret: "12345678901234567890123456789010"
---
spring:
profiles: PROD
datasource:
hikari:
maximum-pool-size: 4
url: jdbc:postgresql://localhost:5432/postgres
username: postgres
password: pass
platform: postgres
jpa:
hibernate:
ddl-auto: none
properties:
hibernate:
default_schema: db
jwt:
secret: "12345678901234567890123456789010"
예시로 아래와 같이 코드를 쓰면 될것 같습니다 !
spring: profiles: active: dev ---
@KIMHWANSEUNG 환경변수를 위에 주입할 수 있는지 조사해주세요
https://www.inflearn.com/questions/17359