1. 의존성 추가

implementation 'org.liquibase:liquibase-core:4.8.0'

liquibase 관련 의존성을 추가해준다.


2. DB change log 작성

<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.1.xsd>">

    <include file="classpath:/db/changelog/db.changelog-1.0.xml" />
    <include file="classpath:/db/changelog/db.changelog-1.1.xml" />

</databaseChangeLog>
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="<http://www.liquibase.org/xml/ns/dbchangelog>" xmlns:xsi="<http://www.w3.org/2001/XMLSchema-instance>" xmlns:pro="<http://www.liquibase.org/xml/ns/pro>" xsi:schemaLocation="<http://www.liquibase.org/xml/ns/dbchangelog>
  <http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd>
  <http://www.liquibase.org/xml/ns/pro> <http://www.liquibase.org/xml/ns/pro/liquibase-pro-3.8.xsd>">
  <changeSet id="202010211812" author="Julius Krah">
    <createTable tableName="house">
      <column name="id" type="bigint">
        <constraints primaryKey="true" primaryKeyName="house_id_pk" />
      </column>
      <column name="owner" type="varchar(250)">
        <constraints unique="true" uniqueConstraintName="house_owner_unq" />
      </column>
      <column name="fully_paid" type="boolean" defaultValueBoolean="false"></column>
    </createTable>
    <createTable tableName="item">
      <column name="id" type="bigint">
        <constraints primaryKey="true" primaryKeyName="item_id_pk" />
      </column>
      <column name="name" type="varchar(250)" />
      <column name="house_id" type="bigint">
        <constraints nullable="false" notNullConstraintName="item_house_id_nn" />
      </column>
    </createTable>
    <addAutoIncrement tableName="house" columnName="id" columnDataType="bigint" startWith="1" incrementBy="1" />
    <addAutoIncrement tableName="item" columnName="id" columnDataType="bigint" startWith="1" incrementBy="1" />
    <createSequence sequenceName="hibernate_sequence" incrementBy="1" startValue="1" />
    <addForeignKeyConstraint baseTableName="item" baseColumnNames="house_id" constraintName="item_house_id_fk" referencedTableName="house" referencedColumnNames="id" />
  </changeSet>
</databaseChangeLog>

위와 같은 방식으로 DB의 변경 사항을 기록할 수 있다.


3. yaml에 change log 경로 설정

liquibase:
    change-log: db/test-changelog-master.xml

위와 같은 방식으로 change log를 설정하고 나면 DB 변경이 잘 되어있는걸 확인할 수 있다.


참고

liquibase 연동하기