liquibase---SpringBoot+Maven+liquibase

前言

liquibase是一個強大的數據庫版本控制工具,支持原聲sql,也有自己的xml格式

SpringBoot+Maven+liquibase

配置文件中

spring:
	liquibase:
	    change-log: classpath:conf/liquibase/master.xml
	    enabled: true
	    drop-first: false
	    contexts: dev,prod

代碼中

import liquibase.integration.spring.SpringLiquibase;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class LiquibaseConfig {

    @Value("${spring.liquibase.enabled}")
    private Boolean enable;

    @Value("${spring.liquibase.drop-first}")
    private Boolean deopFirst;

    @Value("${spring.liquibase.contexts}")
    private String contexts;

    @Bean(name = "liquibase")
    public SpringLiquibase springLiquibase(DataSource dataSource) {

        SpringLiquibase springLiquibase = new SpringLiquibase();

        springLiquibase.setDataSource(dataSource);
        springLiquibase.setChangeLog("classpath:/conf/liquibase/master.xml");
        springLiquibase.setShouldRun(enable);
        springLiquibase.setDropFirst(deopFirst);
        springLiquibase.setContexts(contexts);
        return springLiquibase;
    }
}

master.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.6.xsd">

    <property name="now" value="now()" dbms="h2"/>
    <property name="now" value="now()" dbms="mysql"/>

    <property name="floatType" value="float4" dbms="postgresql, h2"/>
    <property name="floatType" value="float" dbms="mysql, oracle, mssql, mariadb"/>

    <include file="classpath:conf/liquibase/changelog/20190626reportdata_addcloumn.xml" relativeToChangelogFile="false"/>
</databaseChangeLog>
後續加文件時,繼續追加就行了

20190626reportdata_addcloumn.xml

<?xml version="1.0" encoding="utf-8"?>
<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 http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.6.xsd
                        http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">

    <property name="autoIncrement" value="true"/>

    <changeSet id="20190626reportdata_addcloumn" author="zzh">
        <addColumn tableName="report_data">
            <column name="status" type="int" remarks="狀態" ></column>
        </addColumn>
        <addColumn tableName="report_data">
            <column name="create_user_id" type="int" remarks="創建人id" ></column>
        </addColumn>
        <addColumn tableName="report_data">
            <column name="check_user_id" type="int" remarks="確認人id" ></column>
        </addColumn>
        <addColumn tableName="report_data">
            <column name="check_time" type="datetime" remarks="確認時間" ></column>
        </addColumn>
    </changeSet>
</databaseChangeLog>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章