๐ ์ค์ต ๋ด์ฉ
1. Spring Boot 3 + Spring Data JPA ํ๊ฒฝ์์ Liquibase ํ๊ฒฝ์ ์ค์ ํ๋ค.
2. Entity ํ์ผ์ ์์ฑํ ๋ค, ๋ก์ปฌ DB์ ์คํค๋ง ์ฐจ์ด์ ๋ํ ChangeLog ํ์ผ์ ์์ฑํ๋ค. (diff)
4. profile ์ค์ ์ ํตํด ๊ฐ๋ฐ ํ๊ฒฝ๋ณ๋ก ์ ์ฉํ๋ค.
๐ ์ค์ต 2. ๋ก์ปฌ DB์ Entity ๊ฐ์ ์คํค๋ง ์ฐจ์ด ChangeLog ๋ง๋ค๊ธฐ
์ด์ ์ค์ต์์๋ ํ๋ก์ ํธ์ ๊ธฐ๋ณธ์ ์ธ Liquibase๋ฅผ ์๋ฃํ๋ค.
์ด๋ฒ ์ค์ต์์๋ ํ๋ก์ ํธ์ Spring Data JPA ๊ธฐ๋ฐ์ ๊ฐ๋จํ ์ํฐํฐ๋ฅผ ์์ฑํ๊ณ , DB์ ์คํค๋ง ์ฐจ์ด์ ๋ํ ChangeLog ํ์ผ์ ๋ง๋ค ๊ฒ์ด๋ค.
1. ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ค์
application.properties์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ค์ ์ ๋ณด๋ฅผ ์ถ๊ฐํ๋ค.
๊ธฐ์กด ํ๋ก์ ํธ์ ๋ฐ์ดํฐ ๋ฒ ์ด์ค ์ค์ ์ด ๋์ด์๋ ๊ฒฝ์ฐ๋ 1๋ฒ์ ์๋ตํ๋ฉด ๋๋ค.
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.datasource.url=jdbc:mariadb://localhost:{db_port}/{database_name}
spring.datasource.username={user_name}
spring.datasource.password={password}
spring.jpa.hibernate.ddl-auto=none
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.show-sql=true
{user_name} = ๋ฐ์ดํฐ๋ฒ ์ด์ค id ์ ๋ณด ์ ๋ ฅ
{password} = ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋น๋ฐ๋ฒํธ ์ ๋ ฅ
{db_port} = ๋ฐ์ดํฐ๋ฒ ์ด์ค ํฌํธ ๋ฒํธ
{database_name} = ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ด๋ฆ
โ๏ธ ddl-auto ์ต์ ์ none์ผ๋ก ์ค์ ํ์.
liquibase๋ ddl-auto๋ฅผ ํตํด update ๋๋ create ํ๋ ์ ๋ณด์ ๋ํด์๋ ์์ง ๋ชปํ๋ค. ๋ฐ๋ผ์ liquibase๋ฅผ ํ์ฉํด์ ์ ๋๋ก ๋ DB Migration ์ด๋ ฅ ๊ด๋ฆฌ๋ฅผ ์๋ํ๊ณ ์๋ค๋ฉด, JPA๊ฐ ์ง์ํ๋ ddl-auto ๊ธฐ๋ฅ์ ์ฌ์ฉํ์ง ์๋ ๊ฒ์ด ์ข๋ค.
2. ์ํฐํฐ ์์ฑ/๋ณ๊ฒฝ
๊ฐ๋จํ ์ํฐํฐ๋ฅผ ์์ฑ ๋๋ ๋ณ๊ฒฝํ๋ค. ๊ธฐ์กด ํ๋ก์ ํธ์ ์ํฐํฐ๊ฐ ์๋ ๊ฒฝ์ฐ์๋ 2๋ฒ์ ์๋ตํ๋ฉด ๋๋ค.
@Entity
@Getter
@Table(name = "shiba_holic")
public class ShibaHolic {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(unique = true, length = 200, nullable = false)
private String name;
@Column(nullable = false)
private Integer age;
}
์์ ๋ฅผ ๋ณด์ฌ์ฃผ๊ธฐ ์ํด ๊ฐ๋จํ ์ํฐํฐ๋ฅผ ์์ฑํ๋ค. ๋ํ, ์ ์ฝ ์ฌํญ๊ณผ ์ต์ ๋ค์ ์ถ๊ฐํ์๋ค.
3. diff ๋ช ๋ น์ด ์ฌ์ฉํ์ฌ changeLog ํ์ผ ๋ง๋ค๊ธฐ
- cmd์์ ํ๋ก์ ํธ ์์น์ ๊ฐ๊ฑฐ๋ ์ธํ ๋ฆฌ์ ์ด ํฐ๋ฏธ๋์ ํ์ฉํด์ ํ๋ก์ ํธ ๊ฒฝ๋ก๋ก ์ด๋ํ๋ค.
- mvn liquibase:diff ๋ช ๋ น์ด๋ฅผ ์ ๋ ฅํ๋ค.
๋ช ๋ น์ด๋ฅผ ์ ๋ ฅํ๋ฉด ์๋จ ์ด๋ฏธ์ง์ฒ๋ผ ํฐ๋ฏธ๋์ Liquibase ์คํ ํ๋ฉด์ด ๋์ค๋ฉฐ, ์ค์ ๋ฐ ์งํ ๊ด๋ จ ์ ๋ณด ๋ฑ์ ์ถ๋ ฅํ๋ค.
4. changeLog.xml ํ์ผ ๋ด์ฉ ํ์ธํ๊ธฐ
์ค์ ์ ๋์ผํ๊ฒ ํ๋ค๋ฉด ํด๋น ์ด๋ฏธ์ง์ฒ๋ผ resoures/db/changelog/diff ๋๋ ํฐ๋ฆฌ์ ๋ณ๊ฒฝ๋ ๋ด์ฉ์ด ์์ฑ๋ ๋ ์ง_changelog.xml ํ์ผ์ด ์์ฑ๋์์ ๊ฒ์ด๋ค.
๊ฒฐ๊ณผ๋ฌผ์ ํ์ธํด ๋ณด์.
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="http://www.liquibase.org/xml/ns/pro" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-latest.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">
<changeSet author="MHK (generated)" id="1684573329477-1">
<createTable tableName="shiba_holic">
<column autoIncrement="true" name="id" type="BIGINT">
<constraints nullable="false" primaryKey="true" primaryKeyName="shiba_holicPK"/>
</column>
<column name="age" type="INT">
<constraints nullable="false"/>
</column>
<column name="name" type="VARCHAR(200)">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>
<changeSet author="MHK (generated)" id="1684573329477-2">
<addUniqueConstraint columnNames="name" constraintName="UC_SHIBA_HOLICNAME_COL" tableName="shiba_holic"/>
</changeSet>
</databaseChangeLog>
๋ด์ฉ์ ์ดํด๋ณด๋ฉด ๋ค์๊ณผ ๊ฐ์ ์ ๋ณด๋ฅผ ํ์ ํ ์ ์๋ค.
1. shiba_holic ํ ์ด๋ธ ์์ฑ(createTable)
2. column 3๊ฐ ์์ฑ(column)
3. ๊ฐ column์ ์ค์ ํ nullable = false(constraints), length= 200(VARCHAR(200)) ๋ฑ์ ์ ์ฝ ์กฐ๊ฑด๊ณผ ์ต์
4. name์ ์ ๋ํฌ ์ ์ฝ ์กฐ๊ฑด ์ถ๊ฐ (addUniqueConstraint)
์ฆ, ์ํฐํฐ ํ์ผ์ ์ค์ ํ ๋ด์ฉ์ด ๊ณ ์ค๋ํ liquibase ํ์์ ๋ง๊ฒ ์ถ๊ฐ๋์ด ์๋ค.
์ด๊ฒ์ผ๋ก ChangeLog ํ์ผ ์์ฑํ๊ธฐ๊น์ง ๋์ด ๋ฌ๋ค.
3ํธ์์๋ ์์ฑํ ChangeLog ํ์ผ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฉํด์ ์ค์ ํ ์ด๋ธ์ ์์ฑํ๊ณ , ๋ณ๊ฒฝ ์ด๋ ฅ์ ์ถ๊ฐํ๋ ๋ด์ฉ์ ๊ณต์ ํ๊ฒ ๋ค.
REFERENCE
https://docs.liquibase.com/commands/inspection/diff.html
diff
diff The diff command in Liquibase allows you to compare two databases of the same type, or different types, to one another. Uses The diff command is typically used at the completion of a project to verify all expected changes are in the changelog or to de
docs.liquibase.com
https://docs.liquibase.com/commands/inspection/diff-changelog.html
diff-changelog
diff-changelog The diff-changelog command displays the differences between two databases you are comparing. It also generates a changelog file containing deployable changesets to resolve most of these differences. Uses The diff-changelog command is typical
docs.liquibase.com
๋๊ธ