Setting maven liquibase plugin of Spring-boot

Liquibase Migration

Laravel 的 Migration 是一個很棒的工具,
可以簡單的下個指令, 輕鬆的建立 tables
php artisan migrate

在 Spring-boot 也提供一套類似的工具, 使用起來大同小異, 只是設定上有點小麻煩。

dependencies


<dependency>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-core</artifactId>
</dependency>

build


<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
    
    <testResources>
        <testResource>
            <directory>src/test/resources</directory>
        </testResource>
    </testResources>

    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.2</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>

        <!--用來快速產生 migration-->
        <plugin>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-maven-plugin</artifactId>
            <version>3.5.3</version>
            <configuration>
                <url>jdbc:mysql://localhost:3306/liquibase_demo</url>
                <changeLogFile>src/main/resources/config/liquibase/master.xml</changeLogFile>
                <driver>com.mysql.jdbc.Driver</driver>
                <username>username</username>
                <password></password>
            </configuration>
        </plugin>
    </plugins>
</build>

application.yml



# database
database:
    host: localhost
    name: liquibase_demo
    username: root
    password:
spring:
    profiles:
        active: dev
    application:
        name: urad-facebook-data
    datasource:
        username: ${database.username}
        password: ${database.password}
        url: jdbc:mysql://${database.host}:3306/${database.name}?characterEncoding=utf-8&useUnicode=true&useSSL=false&rewriteBatchedStatements=TRUE

# liquibase configuration
liquibase:
    enabled: true
    change-log: classpath:config/liquibase/master.xml
    url: ${spring.datasource.url}
    user: ${spring.datasource.username}
    password: ${spring.datasource.password}

Changle Log

Change Log 的路徑, Srping-boot 預設為 db/changelog/db.changelog-master.yaml,
可以參考 文件, 我為了方便管理設定為 src/main/resources/config/liquibase/changelog/master.xml

Create User Table

<?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: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/dbchangelog 
                                         http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">
    <changeSet author="jerry (generated)" id="1511234719817-1">
        <createTable tableName="user">
            <column autoIncrement="true" name="id" type="INT UNSIGNED">
                <constraints primaryKey="true"/>
            </column>
            <column name="name" type="CHAR(32)"/>
            <column name="age" type="INT"/>
        </createTable>
    </changeSet>
</databaseChangeLog>

將 change log 添加到 master.xml

<?xml version="1.0" encoding="utf-8"?>
<databaseChangeLog
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
    xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog 
                       http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">

    <include file="classpath:config/liquibase/changelog/00000000000000_initial_user_schema.xml" relativeToChangelogFile="false"/>
</databaseChangeLog>

Liquibase plugin 基本指令

liquibase:update: 將新的 change log 更新到目前的資料庫.

mvn liquibase:update

liquibase:generateChangeLog: 依據現有的資料庫的狀態, 產生 liquibase 的 Change log.

mvn liquibase:generateChangeLog

liquibase:dbDoc: 依據現有的資料庫的狀態, 產生 db doc 文件, 類似 Java doc, 參考 範例,
預設產出的文件路徑 target/liquibase/dbDoc/index.html

mvn liquibase:dbDoc

liquibase 文件



沒有留言:

張貼留言