[java] Guice-Persist를 사용하여 데이터베이스 스키마 버전 관리하는 방법은?

다음은 Guice-Persist와 Liquibase를 함께 사용하여 데이터베이스 스키마 버전 관리를 하는 방법에 대한 예시입니다.

  1. 의존성 설정
    // build.gradle
    
    dependencies {
        // Guice-Persist
        implementation 'com.google.inject.extensions:guice-persist:4.2.0'
    
        // Hibernate
        implementation 'org.hibernate:hibernate-core:5.4.32.Final'
    
        // Liquibase
        implementation 'org.liquibase:liquibase-core:4.4.3'
        implementation 'org.liquibase.ext:liquibase-hibernate5:4.4.3'
    }
    
  2. Guice-Persist 모듈 설정
    // MyModule.java
    
    import com.google.inject.AbstractModule;
    import com.google.inject.persist.jpa.JpaPersistModule;
    
    public class MyModule extends AbstractModule {
        @Override
        protected void configure() {
            install(new JpaPersistModule("myPersistenceUnit"));
            bind(StartupService.class).asEagerSingleton();
        }
    }
    
  3. Persistence Unit 설정
    // persistence.xml
    
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" version="2.2">
    
        <persistence-unit name="myPersistenceUnit" transaction-type="RESOURCE_LOCAL">
            <properties>
                <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/mydatabase"/>
                <property name="javax.persistence.jdbc.user" value="username"/>
                <property name="javax.persistence.jdbc.password" value="password"/>
                <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            </properties>
        </persistence-unit>
    
    </persistence>
    
  4. Liquibase 설정
    <!-- liquibase.properties -->
    
    url=jdbc:mysql://localhost:3306/mydatabase
    username=username
    password=password
    driver=com.mysql.jdbc.Driver
    changeLogFile=src/main/resources/db/changelog.xml
    
    <!-- changelog.xml -->
    
    <?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"
                       xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
                       http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
    
        <changeSet id="1" author="me">
            <createTable tableName="my_table">
                <column name="id" type="bigint" autoIncrement="true">
                    <constraints primaryKey="true" nullable="false"/>
                </column>
                <column name="name" type="varchar(255)"/>
            </createTable>
        </changeSet>
    
    </databaseChangeLog>
    

위의 예시에서는 Guice-Persist를 사용하여 JPA를 구성하고, Liquibase를 통해 스키마 변경 내역을 관리하고 있습니다. Liquibase의 설정은 liquibase.propertieschangelog.xml 파일을 통해 정의되며, changeSet 요소를 사용하여 실행할 스키마 변경 작업을 정의할 수 있습니다.

이러한 설정을 통해 Guice-Persist와 Liquibase를 함께 사용하여 데이터베이스 스키마 버전 관리를 할 수 있습니다. 자세한 내용은 Guice-Persist와 Liquibase의 공식 문서를 참고하시기 바랍니다.