liquibase

springboot集合liquibase

需求:集成增量脚本自动执行框架,flyaway和liquibaser,建议用liquibase,同时liquibaser也可以做数据库迁移

1.引入依赖

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

2.在application中配置liquibase,注意层级关系

spring:
    liquibase:
        # 是否启动
        enabled: true
        # 日志配置路径
        change-log: classpath:liquibase/master.xml
        # 用于跟踪更改历史记录的表的名称
        databaseChangeLogTable: databasechangelog
        # 用于跟踪并发 Liquibase 使用情况的表的名称
        databaseChangeLogLockTable: databasechangeloglock

3.配置liquibase启动bean

import liquibase.integration.spring.SpringLiquibase;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class LiquibaseConfiguration {
    
    @Bean
    @ConfigurationProperties(prefix = "spring.liquibase")
    public LiquibaseProperties getLiquibaseProperties() {
        return new LiquibaseProperties();
    }

    @Bean
    public SpringLiquibase liquibase(DataSource dataSource) {

        LiquibaseProperties liquibaseProperties = getLiquibaseProperties();
        SpringLiquibase liquibase = new SpringLiquibase();
        liquibase.setChangeLog(liquibaseProperties.getChangeLog());
        liquibase.setDataSource(dataSource);
        liquibase.setShouldRun(true);
        return liquibase;
    }
}

4.liquibase的文件配置

在这里插入图片描述

4.1 000_initial_schema.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.5.xsd">

    <!-- id="00000000000003" 如果已经执行过将不再执行  -->
    <changeSet id="00000000000003" author="zhiyi">
        <!--   执行这个sql文件     -->
        <sqlFile path="liquibase/data/init_schema.sql"/>
    </changeSet>

    <changeSet id="00000000000004" author="zhiyi">
        <!--preConditions:定义在执行这个变更集之前需要满足的条件。-->
        <!--onFail="MARK_RAN":如果条件不满足,Liquibase 将标记该变更集为已运行(即使它没有实际执行),这可以防止未来的不必要执行-->
        <preConditions onFail="MARK_RAN">
        <!--<not>:表示条件的否定-->
            <not>
                <!--当这个为否的时候执行<addColumn tableName="demo1">里面的-->
                <columnExists tableName="demo1" columnName="name"/>
            </not>
        </preConditions>
        <!--addColumn:表示要向指定表中添加一个新列-->
        <addColumn tableName="demo1">
            <column name="name" type="varchar(25)" remarks="名字"/>
        </addColumn>
        <!--dropColumn删除字段-->
        <!--<dropColumn tableName="demo1" columnName="name"/>-->
    </changeSet>

    <changeSet id="00000000000006" author="zhiyi">
        <preConditions onFail="MARK_RAN">
            <not>
                <columnExists tableName="demo1" columnName="name1"/>
            </not>
        </preConditions>
        <addColumn tableName="demo1">
            <column name="name1" type="varchar(26)" remarks="名字1"/>
        </addColumn>
    </changeSet>

</databaseChangeLog>

4.2 init_schema.sql

create table if not exists demo1
(
    id          varchar(64) not null comment '主键id'
        primary key,
    name        varchar(32) null comment '名称',
    content     varchar(64) null comment '内容'
)
    comment 'demo1';

4.3 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.5.xsd">

    <include file="liquibase/changelog/000_initial_schema.xml" />

</databaseChangeLog>
  1. 在启动类上贴@DependsOn(“liquibase”)提前提升liquibase运行的优先级

    @DependsOn("liquibase")//提升liquibase运行的优先级
    public class Application {
        public static ConfigurableApplicationContext ac;
    
        public static void main(String[] args) {
            SpringApplication app = new SpringApplication(Application.class);
            app.setBannerMode(Banner.Mode.OFF);
            Application.ac = app.run(args);
            System.out.println("启动成功");
        }
    }
    

6.第一次启动之后会在数据库里创建liquibase的记录表
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值