๐ ์ค์ต ๋ด์ฉ
1. Spring Boot 3 + Spring Data JPA ํ๊ฒฝ์์ Liquibase ํ๊ฒฝ์ ์ค์ ํ๋ค.
4. profile ์ค์ ์ ํตํด ๊ฐ๋ฐ ํ๊ฒฝ๋ณ๋ก ์ ์ฉํ๋ค.
๐ ์ค์ต 4. profile ์ค์ ์ ํตํด ๊ฐ๋ฐ ํ๊ฒฝ๋ณ๋ก ์ ์ฉํด๋ณด๊ธฐ
์ด์ ์ค์ต์ ํตํด changeLog ํ์ผ์ ์ ์ฉํ๊ณ DB์ ๊ด๋ จ ๋ก๊ทธ๋ฅผ ๋จ๊ธด ๊ฒ์ ํ์ธํ๋ค.
์ด๋ฒ ์ค์ต์์๋ Profile(dev, prod) ๋ณ๋ก ๋ค๋ฅด๊ฒ ์ค์ ์ ์ ์ฉํ๊ณ ์คํํ๋ ๋ฐฉ๋ฒ์ ๋ํด ํ์ธํด ๋ณผ ๊ฒ์ด๋ค.
`dev ํ๊ฒฝ`์์๋ ์ํฐํฐ > ๊ฐ๋ฐ DB๋ก ๋ง์ด๊ทธ๋ ์ด์ ํ๋๋ก ์ค์ ํ๊ณ ,
`prod ํ๊ฒฝ`์์๋ ๊ฐ๋ฐDB > ์ด์ DB๋ก ๋ง์ด๊ทธ๋ ์ด์ ํ๋๋ก ์ค์ ํ๋ค.
1. pom.xml ์ค์ ํ๊ธฐ
pom.xml์์ profile์ ๋ฐ๋ผ ๋ค๋ฅด๊ฒ ์ค์ ํ์ผ์ ๋ถ๋ฆฌํด์ผ ํ๋ค. pom.xml ํ์ผ์ ํ๋จ ์ฝ๋๋ฅผ ์ถ๊ฐํ๋ค.
dev์ ๊ฒฝ์ฐ๋ ์ํฐํฐ์์ DB๋ก ๋ง์ด๊ทธ๋ ์ด์ ํ๋ ๊ณผ์ ์ด๋ฏ๋ก ๊ด๋ จํ ์์กด์ฑ์ด ์ถ๊ฐ๋์ด ์๊ณ , prod๋ DB ๊ฐ ๋ง์ด๊ทธ๋ ์ด์ ์ด๋ฏ๋ก ๋ณ๋์ ์์กด์ฑ์ ์ถ๊ฐํ์ง ์๋๋ค.
<profiles>
<profile>
<id>dev</id>
<build>
<plugins>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<configuration>
<diffChangeLogFile>src/main/resources/db/changelog/diff/dev/${maven.build.timestamp}_changelog.xml</diffChangeLogFile>
<verbose>true</verbose>
<propertyFile>src/main/resources/liquibase-dev.properties</propertyFile>
</configuration>
<dependencies>
<dependency>
<groupId>org.liquibase.ext</groupId>
<artifactId>liquibase-hibernate6</artifactId>
<version>4.20.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>3.0.6</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>prod</id>
<build>
<plugins>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<configuration>
<diffChangeLogFile>src/main/resources/db/changelog/diff/prod/${maven.build.timestamp}_changelog.xml</diffChangeLogFile>
<verbose>true</verbose>
<propertyFile>src/main/resources/liquibase-prod.properties</propertyFile>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
ํด๋น ์ค์ ์์ ์ค์ํ ๋ถ๋ถ์ <diffChangeLogFile> ๊ฒฝ๋ก์ <propertyFile> ๊ฒฝ๋ก์ด๋ค.
changeLog ํ์ผ์ด ์์ฑ๋๋ ์์น์ ์ค์ ํ์ผ์ด ์๋ ๊ฒฝ๋ก์ด๋ฏ๋ก ๊ฐ๋ฐํ๋ ํ๋ก์ ํธ์ ๋ง๊ฒ ์์ฑํด์ค๋ค.
2. properties ์ค์ ํ์ผ ์์ฑํ๊ธฐ
dev, prod ํ๊ฒฝ๋ง๋ค ๋ง์ด๊ทธ๋ ์ด์ ํ๋ ํ๊ฒฝ์ด ๋ค๋ฅด๊ธฐ ๋๋ฌธ์ ๋ค๋ฅด๊ฒ ์์ฑํด์ฃผ์ด์ผ ํ๋ค.
liquibase-dev.properties
contexts=dev
logLevel=info
changeLogFile=src/main/resources/db/changelog/db.changelog-master.xml
referenceUrl=hibernate:spring:com.shibaholic.blogliquibase.model?dialect=org.hibernate.dialect.MariaDBDialect
url=jdbc:mariadb://localhost:3306/shibaholic
username=
password=
driver=org.mariadb.jdbc.Driver
liquibase-prod.properties
contexts=prod
logLevel=info
changeLogFile=src/main/resources/db/changelog/db.changelog-master.xml
referenceUrl=jdbc:mariadb://localhost:3306/shibaholic
referenceUsername=
referencePassword=
referenceDriver=org.mariadb.jdbc.Driver
url=jdbc:mariadb://localhost:3306/shibaholic2
username=
password=
driver=org.mariadb.jdbc.Driver
์์ ์์ฑ๋ ์ค์ ํ์ผ์ ๋ณด๊ณ reference ์ ๋๊ฐ์ฌ ๋ถ์ ์ค์ ๊ณผ ์ผ๋ฐ ์ค์ ์ ์ด๋ค ์ฐจ์ด๊ฐ ์๋ ๊ฑด์ง ๊ถ๊ธํ ์ ์๋ค.
๊ฐ๋จํ๊ฒ ์ผ๋ฐ ์ค์ (url, username, password, driver)์ด ๊ธฐ์ค์ด๋ผ๊ณ ์๊ฐํ๋ฉด ๋๋ค. DB ์ฐจ์ด ์ ๋ณด(changeLog)๋ฅผ ์ถ๋ ฅํ๋ diff ๋ช ๋ น์ ์์๋ก ๋ ๋ค๋ฉด, ๋ค์๊ณผ ๊ฐ๋ค.
diff = ์ผ๋ฐ ์ค์ ์ ๊ธฐ์ค์ผ๋ก reference ์ค์ ๋ DB(ํน์ ์ํฐํฐ)์ ๋น๊ตํด์ ์๋ ๋ด์ฉ(์คํค๋ง, ์ ์ฝ ๋ฑ)์ ์ถ์ถํ๋ค.
๊ทธ๋์ ์ผ๋ฐ ์ค์ ์ dev ์์๋ ๊ฐ๋ฐ DB ์ฐ๊ฒฐ ์ ๋ณด๋ฅผ ์ ๋ ฅํ๊ณ , prod ์์๋ ์ด์ DB ์ฐ๊ฒฐ ์ ๋ณด๋ฅผ ์ ๋ ฅํ๋ค.
3. db.changelog-master.xml ์ค์ ํ์ผ ์์ ํ๊ธฐ
master ์ค์ ํ์ผ์ ํ๊ฒฝ๋ง๋ค ๋ณ๋์ ๋ง์คํฐ ํ์ผ์ ๋๋ ๋ฐฉ๋ฒ๊ณผ ํ๋์ ํ์ผ์์ context๋ก ๊ตฌ๋ถํ๋ ๋ฐฉ๋ฒ์ด ์๋ค.
ํ์๋ ํ๋์ ํ์ผ์์ ํ๊ฒฝ๋ณ๋ก context๋ก ๊ตฌ๋ถํ๋ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๋ค.
(2๋ฒ์์ ์ค์ ํ์ผ์ ์ดํด๋ณด๋ฉด ๊ฐ ์ค์ ํ์ผ์ contexts= dev, prod๊ฐ ์์ฑ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.)
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd" >
<includeAll path="/db/changelog/diff/dev/" context="dev"></includeAll>
<includeAll path="/db/changelog/diff/prod/" context="prod"></includeAll>
</databaseChangeLog>
context๋ ์ด๋ค changeset์ ์คํํ ์ง ์ฌ๋ถ๋ฅผ ์ ์ดํ๋ โโํํ์์ด๋ค.
4. ๋ง์ด๊ทธ๋ ์ด์ ํ๊ธฐ
ํ๊ฒฝ๋ณ๋ก ์ค์ ์ด ์ ๋์๋์ง ํ์ธํด๋ณด๊ธฐ ์ํด ์๋ 4๊ฐ์ ๋ช ๋ น์ด๋ฅผ ์ฐจ๋ก๋ก ์ ๋ ฅํด๋ณธ๋ค.
update ๋ช ๋ น์ด๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ์ง์ ์คํํด์ ํ ์คํธํด๋ ์๊ด ์๋ค. (profile ์ค์ ํ์)
1. mvn compile liquibase:diff -Pdev
2. mvn compile liquibase:update -Pdev
3. mvn compile liquibase:diff -Pprod
4. mvn compile liquibase:update -Pprod
1,2๋ ์ํฐํฐ์์ ๊ฐ๋ฐ DB๋ก ๋ง์ด๊ทธ๋ ์ด์ ํ๋ ๊ณผ์ ์ด๋ฉฐ,
3,4๋ ๊ฐ๋ฐ DB์์ ์ด์ DB๋ก ๋ง์ด๊ทธ๋ ์ด์ ํ๋ ๊ณผ์ ์ด๋ค.
๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ๊ทผํด์ ํ์ธํด๋ณด๋ฉด dev์ prod ํ๊ฒฝ์์ ์ฑ๊ณต์ ์ผ๋ก ํ ์ด๋ธ์ด ์์ฑ๋์๊ณ ์ ์ฝ์กฐ๊ฑด์ด ์ถ๊ฐ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
์ด๊ฒ์ผ๋ก liquibase ๋ง์ด๊ทธ๋ ์ด์ ์ค์ต ๊ณผ์ ์ ๋ง๋ฌด๋ฆฌ ํ๋ค.
ํ๋ก์ ํธ์ ์ ์ฉํ๊ฒ ํ์ฉํด๋ณด๋ฉด ์ข๊ฒ ๋ค.
REFERENCE
'Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Spring RedisSerializer ์ข ๋ฅ์ ํน์ง (0) | 2023.07.15 |
---|---|
Spring Boot 3 ํ๊ฒฝ์์ KeyCloak ์ ์ฉํ๊ธฐ (0) | 2023.06.25 |
๋์ ๋ฆฌ์คํธ ๋ฐ์ธ๋ฉ ๋ฌธ์ ํด๊ฒฐํ๊ธฐ (java.lang.IndexOutOfBoundsException: Index: 256, Size: 256) (0) | 2023.06.06 |
Spring Boot 3 + JPA๋ฅผ ํ์ฉํ Liquibase ์ค์ตํ๊ธฐ (3) (0) | 2023.05.29 |
Spring Boot 3 + JPA๋ฅผ ํ์ฉํ Liquibase ์ค์ตํ๊ธฐ (2) (0) | 2023.05.20 |
๋๊ธ