728x90
반응형


DB 형상 관리 - Liquibase Best Practices

Liquibase Best Practices

  • Liquibase 공식문서에서 가장 효율적으로 잘 활용하는 방법에 대한 예시

디렉토리 구조

  • changelog 를 릴리즈 별로 구성.
  • DB Access 코드 즉, DAO, Repository 코드 근처에 구성해라.
com
  example
    db
      changelog
        db.changelog-root.xml
        db.changelog-1.0.xml
        db.changelog-1.1.xml
        db.changelog-2.0.xml
      DatabasePool.java
      AbstractDAO.java

Root ChangeLog 파일 사용

  • 모든 변경 파일은 changelog-root.xml 파일에 전달 된다.
  • Liquibase가 Flyway 와 차별화 될 수 있는 점으로 순차적 실행을 보장하는 Flyway와 다르게 include 속성을 통해 변경 사항을 구성할 수 있다.

changelog-root.xml

<databaseChangeLog
    xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
    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-latest.xsd
        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">

	<include file="com/example/news/news.changelog.sql"/>
	<include file="com/example/directory/directory.changelog.sql"/>

</databaseChangeLog>
  • changelog-root.xml 의 경우 편하게 마크업 언어로 구성하고, changelog의 경우 SQL문 으로 구성하는 방법. 상당히 괜찮아 보임.

하나의 변경 셋에는 하나의 변경 사항 (단일 원칙)

  • 하나의 ChangeSet에는 하나의 Changelog만을 기록할 것.
  • 단, 여러개의 변경사항을 트랜잭션으로 코드로 묶어두는 것은 예외의 경우.
  • 각 단일 트랜잭션의 원자성을 유지하도록 할 것.
  • 단일 ChangeSet에서 여러 개의. 독립적인 변경 사항을 사용하면 일부는 성공하고 일부는 실패된 상황으로 배포가 될 위험이 있음.

일관된 ChangeSet ID

  • 고유하며 일관된 방법을 사용할 것.
  • 1 부터 시작되는 숫자 시퀀스 사용 권장.

문서화의 중요성

  • ChangeSet에 <comments> 태그를 사용하여, 변경에 대한 이유를 문서화.

롤백 전략

  • 특정 ChangeLog를 이용해 롤백 전략을 구성해둘 것.

개발자 WorkFlow

  1. ChangeLog를 기록하며 개발 진행
  2. ChangeLog DB 반영
  3. 검증
  4. Source Control (Git, SVN) 반영

Orcale의 Liquibase Best Practices (Oracle)

방법 - 1 : 각 팀이 고유한 changeLog 파일 사용

  • 각 팀은 고유한 changeLog 파일을 가지공 있으며, 해당 파일에 자신들의 변경 사항을 기록
  • ex) Team A : teamA-changelog.xml, Team B: teamB-changelog.xml

예시

teamA-changelog.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">

    <changeSet id="1" author="teamA">
        <createTable tableName="teamA_table">
            <column name="id" type="int">
                <constraints primaryKey="true"/>
            </column>
        </createTable>
    </changeSet>

</databaseChangeLog>

teamB-changelog.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">

    <changeSet id="1" author="teamB">
        <createTable tableName="teamB_table">
            <column name="id" type="int">
                <constraints primaryKey="true"/>
            </column>
        </createTable>
    </changeSet>

</databaseChangeLog>

방법 - 2 : include 태그를 사용하여 팀별 changeLog 파일을 포함

  • include 태그를 사용하여 중앙 master changeLog 파일에 팀별 changeLog 파일을 포함
  • 각 팀의 변경 사항이 중앙 master changeLog 파일에 통합되어 관리.
  • 팀 모두 동일한 DB 접근 URL 및 계정을 사용해야 함. (databasechnagelog) 테이블 통합 및 추적하기 위함.
  • 여러 데이터베이스에 대해 변경 권한이 있는 계정을 사용해야 함.
  • SQL 스크립트는 데이터베이스 이름. 스키마 이름. 테이블 명으로 정규화 되어 있어야 함.

예시

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">

    <include file="teamA-changelog.xml"/>
    <include file="teamB-changelog.xml"/>

</databaseChangeLog>

application.properties

spring.liquibase.change-log=classpath:db/changelog/db.changelog-master.xml
728x90
반응형
MyeongDev