๐ ์ค์ต ๋ด์ฉ
1. Spring Boot 3 + Spring Data JPA ํ๊ฒฝ์์ Liquibase ํ๊ฒฝ์ ์ค์ ํ๋ค.
4. profile ์ค์ ์ ํตํด ๊ฐ๋ฐ ํ๊ฒฝ๋ณ๋ก ์ ์ฉํ๋ค.
๐ ์ค์ต 1. Liquibase ํ๊ฒฝ ์ค์ ํ๊ธฐ
๊ธฐ๋ณธ ์ค์ต ํ๊ฒฝ์ ๋ค์๊ณผ ๊ฐ๋ค.
1. Spring Boot 3.0.6
2. Maven 3.8.4
3. Java 17
4. MariaDB 10.6
1. Liquibase Dependency ์ถ๊ฐ
Liquibase๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์ liquibase-core ์์กด์ฑ์ ์ถ๊ฐํ๋ค.
์คํ๋ง์์ ๊ธฐ๋ณธ์ ์ผ๋ก liquibase ๋ฒ์ ๊ด๋ฆฌ๋ฅผ ํด์ฃผ๋ฏ๋ก <version>์ ์ง์ ์ ๋ ฅํ ํ์๋ ์๋ค.
ํน์ ๋ฒ์ ๊ด๋ฆฌ๊ฐ ํ์ํ๋ค๋ฉด ๋ฐ๋ก ๋ฒ์ ์ ์ ๋ ฅํด๋ ์๊ด์๋ค.
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
2. Maven Plugin ์ถ๊ฐ
liquibase-maven-plugin๋ ์คํ๋ง์์ ๋ฒ์ ๊ด๋ฆฌ๋ฅผ ํด์ฃผ๊ธฐ ๋๋ฌธ์ <version>์ ์ ๋ ฅํ ํ์๋ ์๋ค.
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<configuration>
<diffChangeLogFile>src/main/resources/db/changelog/diff/${maven.build.timestamp}_changelog.xml</diffChangeLogFile>
<verbose>true</verbose>
<logging>info</logging>
<propertyFile>src/main/resources/liquibase.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>
<configuration>์์ liquibase ๋ฐ์ดํฐ๋ฒ ์ด์ค ํ๊ฒฝ ๋ฐ ๋์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ํ๊ฒฝ์ ์ค์ ํ๋ค.
<propertyFile>๋ ๋ณ๋์ ์ค์ ํ์ผ ๊ฒฝ๋ก๋ฅผ ์ค์ ํ๋ ๊ฒ์ด๋ค.
์ค์ ํ์ผ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ๋์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ ๋ณด๋ฅผ ์ ๋ ฅํ๋ค.
์ฐธ๊ณ ๋ก, ๊ผญ ์ค์ ํ์ผ์ ์ฌ์ฉํ ํ์๋ ์๋ค. ์ต์ ์ ์ง์ ์ ๋ ฅํ๋ ๊ฒ๋ ๊ฐ๋ฅํ๋ค. ์์์ฒ๋ผ ์ผ๋ถ ์ต์ ๋ง ์ฌ์ฉํด๋ ๋๋ค.
<diffChangeLogFile>์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ํฐํฐ๋ฅผ ๋น๊ตํ ๊ฒฐ๊ณผ๋ฌผ์ ์ด๋์๋ค ์ ์ฅํ ์ง ์ค์ ํ๋ ๊ฒ์ด๋ค.
๊ฒฝ๋ก ์ค์ ${maven.build.timestamp}๋ diff ํ์ผ์ ์์ฑํ๋ ์๊ฐ์ผ๋ก ์ด๋ฆ์ ์ค์ ํ๊ฒ ๋ค๋ ์๋ฏธ์ด๋ค.
๋์ผํ ์ด๋ฆ์ ์ฌ์ฉํ ๊ฒฝ์ฐ, diff๋ฅผ ์ฌ๋ฌ ๋ฒ ํ๋ ๊ฒฝ์ฐ ๋์ผํ ๋ด์ฉ์ด ๊ฐ์ ํ์ผ์ ๊ณ์ํด์ ์ถ๊ฐ๋ ์ ์์์ ์ฃผ์ํ์.
์๋ฅผ ๋ค์ด, diff ๋ช ๋ น์ ํตํด ํ ์ด๋ธ 2๊ฐ๋ฅผ ์์ฑํ๋ changeLog ํ์ผ์ ์์ฑํ์ ๋, update๋ฅผ ํ์ง ์๊ณ ๋ค์ diff ๋ช ๋ น์ ์ํํ๋ ๊ฒฝ์ฐ ํ ์ด๋ธ 2๊ฐ๋ฅผ ์์ฑํ๋ผ๋ ๋ด์ฉ์ด ๊ฐ์ ํ์ผ์ ๋ฐ๋ณตํด์ ๋ค์ด๊ฐ๋ค๋ ์๋ฏธ์ด๋ค.
<dependencies>์๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค ํ๊ฒฝ์ ๋ํ ์์กด์ฑ์ ์ถ๊ฐํ๋ค.
๊ธฐ์กด ๊ฐ๋ฐ ํ๊ฒฝ์ด ์๋๋ฐ 'DB to DB'๊ฐ ์๋ ์์์ฒ๋ผ 'DB to Entity'์ liquibase๋ฅผ ์ฌ์ฉํ๋ ค๋ ๊ฒฝ์ฐ์๋ Entity ํ์ผ์์ ์ฌ์ฉ ์ค์ธ ์ธ๋ถ ์์กด์ฑ์ ๋ชจ๋ ์ถ๊ฐํด์ผ ํ๋ค๋ ์ ์ ์ฃผ์ํ์.
์๋ฅผ ๋ค์ด, Entity ํ์ผ์ @NotNull ๋ฑ์ ์ฌ์ฉํ๋ค๋ฉด, ๊ด๋ จ ์์กด์ฑ์ธ javax-validation์ ์ถ๊ฐํด์ผ ํ๋ค๋ ์๋ฏธ์ด๋ค.
'DB to Entity'๋ฅผ ํ ์คํธ ํ๋ ๊ฒฝ์ฐ๋ ๋ฐ๋์ liquibase-hibernate๋ฅผ ์ถ๊ฐํ๋ค.
(๋ง์ฝ, 'DB to DB'๋ก ์ฌ์ฉํ๋ ๊ฒฝ์ฐ์๋ ํด๋น ์์กด์ฑ์ ํ์ ์๋ค.)
์ฐธ๊ณ ๋ก, ๋ฌด์กฐ๊ฑด liquibase-hibernate6์ ์ฌ์ฉํด์๋ ์๋๋ค.
์ต์ JPA ๋ฒ์ ์์๋ Hibernate 6์ ์ฌ์ฉํ๊ณ ์๊ธฐ ๋๋ฌธ์, ๋ฒ์ ์ ๋ง๊ฒ ์ฌ์ฉํ๋ ๊ฒ์ด๋ค.
๋ง์ฝ ํ๊ฒฝ์ด Hibernate 5๋ผ๋ฉด ๊ทธ์ ๋ง์ถฐ์ liquibase-hibernate5๋ก ์ฌ์ฉํด์ผ ํ๋ค.
3. liquibase.properties ํ์ผ ์ค์ ํ๊ธฐ
ํด๋น ์ค์ ์ MariaDB ํ๊ฒฝ์ ๋ง๊ฒ ์ค์ ๋์๋ค.
๋ค๋ฅธ DB๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ์๋ url, username, password, driver, referenceUrl ์ค dialect ๋ถ๋ถ์ ์๋ง๊ฒ ์ค์ ํ๋ฉด ๋๋ค.
changeLogFile=src/main/resources/db/changelog/db.changelog-master.xml
url=jdbc:mariadb://{db ip}:{db port}/{database name}
username={db username}
password={db password}
driver=org.mariadb.jdbc.Driver
referenceUrl=hibernate:spring:com.shibaholic.blogliquibase.model?dialect=org.hibernate.dialect.MariaDBDialect
changeLogFile์ db.changelog-master.xml ํ์ผ์ด ์ด๋ ์๋์ง ๊ฒฝ๋ก๋ฅผ ํ์ธํ๋ ๊ฒ์ด๋ค.
referenceUrl์์ hibernate:spring: ๋ค com.shibaholic.blogliquibase.model ์ ์ํฐํฐ ํจํค์ง ๊ฒฝ๋ก์ด๋ค.
๋ณธ์ธ ํ๋ก์ ํธ์ ์ค์ ํ ์ํฐํฐ ํจํค์ง ๊ฒฝ๋ก๋ฅผ ์ ๋ ฅํ๋ฉด ๋๋ค.
4. db.changelog-master.xml ํ์ผ ์์ฑํ๊ธฐ
<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/"></includeAll>
</databaseChangeLog>
db.changelog-master.xml ํ์ผ์์ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ก ๋ง์ด๊ทธ๋ ์ด์ ํ ๋์ ํ์ผ๋ค์ ๊ฒฝ๋ก๋ฅผ ์ค์ ํ๋ค.
<includeAll>๋ก ํน์ ๋๋ ํฐ๋ฆฌ๋ฅผ ์ค์ ํ์ฌ ํด๋น ํ์ผ์ ์๋ xml ํ์ผ ์ ์ฒด๋ฅผ ๋ง์ด๊ทธ๋ ์ด์ ํ ์๋ ์๊ณ ,
<include>๋ก ํ์ผ ๊ฐ๋ณ ์ค์ ๋ ๊ฐ๋ฅํ๋ค.
5. application.properties ํ์ผ ์ค์ ํ๊ธฐ
application properties์์๋ db.change-master.xml ํ์ผ ๊ฒฝ๋ก๋ง ์ค์ ํด์ฃผ๋ฉด ๋๋ค.
spring.liquibase.enabled=true
spring.liquibase.change-log=classpath:/db/changelog/db.changelog-master.xml
์ด๊ฒ์ผ๋ก ๊ธฐ๋ณธ์ ์ธ Liquid ์ค์ ์ด ๋์ด ๋ฌ๋ค.
2ํธ์์ Entity ํ์ผ์ ์์ฑํ ๋ค, ๋ก์ปฌ DB์ ์คํค๋ง ์ฐจ์ด์ ๋ํ ChangeLog ํ์ผ์ ์์ฑํ๊ณ diffํ๋ ๋ฐฉ๋ฒ์ ๋ํด ๊ณต์ ํ๊ฒ ๋ค.
REFERENCE
https://docs.liquibase.com/concepts/connections/creating-config-properties.html
Create and Configure a liquibase.properties File
Create and Configure a liquibase.properties File You can use a defaults file called liquibase.properties to store information that Liquibase needs to connect to a particular database, along with other properties that rarely change. This lets you avoid ente
docs.liquibase.com
https://www.baeldung.com/liquibase-refactor-schema-of-java-app
https://stackoverflow.com/questions/75093751/liquibase-generate-diff-with-springboot-3
Liquibase generate diff with Springboot 3
I'm trying to generate liquibase-scripts with liquibase 4.18.0 and Spring Boot 3.0.1. And i'm getting this error: [ERROR] Failed to execute goal org.liquibase:liquibase-maven-plugin:4.18.0:diff (de...
stackoverflow.com
https://docs.liquibase.com/change-types/includeall.html
includeAll
includeAll Use the includeAll tag to specify a directory that contains multiple changelog files. It is used within your root changelog file to call on the directory and include all XML, YAML, and JSON files as changelog files, and all SQL files as individu
docs.liquibase.com
'Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Spring Boot 3 + JPA๋ฅผ ํ์ฉํ Liquibase ์ค์ตํ๊ธฐ (3) (0) | 2023.05.29 |
---|---|
Spring Boot 3 + JPA๋ฅผ ํ์ฉํ Liquibase ์ค์ตํ๊ธฐ (2) (0) | 2023.05.20 |
Spring DB Migration Tool Flyway, Liquibase ํน์ง, ์ ์ฌ์ , ์ฐจ์ด์ (0) | 2023.05.13 |
ObjectMapper ์ฌ์ฉํ ๋ Validator ํ์ฉํ๊ธฐ (0) | 2023.05.01 |
[Spring] Redis ์ธ์ ๋ก๊ทธ์์, ๋ง๋ฃ์ ๊ด๋ จ ์ฒ๋ฆฌ ์์ ํ๊ธฐ (0) | 2023.04.16 |
๋๊ธ