h2 使用liquibase的changelog表格创建不成功

        h2数据库是一款可以嵌入Java应用程序中使用的数据,可以跟随应用程序的生命周期结束而停止的数据库,因此它非常适合用于单元测试。使用h2作为单元测试的数据,能确保我们每次测试的数据都是干净的,同时也能不污染原来应用程序的数据。

由于程序在开发过程中我们使用了liquibase来作为mysql数据库的管理和迁移工具,因此相关配置如下:

changelog.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.1.xsd">
    <changeSet author="Cherie" id="project_init-tables">
        <sqlFile dbms="mysql"  path="./sql/init-tables.sql" relativeToChangelogFile="true" stripComments="true"/>
    </changeSet>
</databaseChangeLog>

liquibase的配置:

spring:
  liquibase:
    change-log: classpath:/db/liquibase/changelog.xml

datasource配置:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mgmt?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
    username: root
    password: root

pom.xml

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
        </dependency>
    </dependencies>

到此,程序使用liquibase管理mysql数据库是可以正常跑起来的,数据库的project表也是可以成功创建处理。

然而当我们仿照liquibase管理mysql的做法使用h2数据,参照网上的一些资料把datasource改为h2即可,配置如下:

datasource配置:

spring:
  datasource:
    url: jdbc:h2:mem:test;MODE=MYSQL
    driver-class-name: org.h2.Driver
  h2:
    console:
      enabled: true #enble web; url:http://localhost:18082/h2-console

pom.xml

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
    </dependencies>

changelog 和 liquibase配置不用修改

        修改完,跑了一下,程序没报错,但发现数据库表并没有创建出来。为了确定表是否真的创建,我们登陆一下h2的web客户端查看一下。

        要访问h2的web客户端,我们要加如下配置:

spring:
  h2:
    console:
      enabled: true #enble web; url:http://localhost:18082/h2-console

由于h2是嵌入到应用程序中的,因此我们访问h2的端口是通过应用程序的端口访问,我的程序端口是18082,所以访问连接是:http://localhost:18082/h2-console

打开之后,页面如下:

填写datasource配置的URL到JDBC URL然后登陆进去后,发现project表确实没有创建,但是DATABASECHANGLOG文件已经把changelog文件的写了进去,这就意味着changelog是有执行的,只是没有效果而已。

我们回头细看一下changelog文件,发现changelog文件都是针对mysql写的,指定的dbms也是mysql,并没有h2的,因此当我们使用h2数据库时,他并不会执行,因此我们只需要做如下改动:

1. changeSet 加入 logicalFilePath="path-independent" 属性配置

2. 增加h2的配置

修改后的changelog.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.1.xsd">
    <changeSet author="Cherie" id="project_init-tables" logicalFilePath="path-independent">
        <sqlFile dbms="mysql"  path="./sql/init-tables.sql" relativeToChangelogFile="true" stripComments="true"/>
        <sqlFile dbms="h2"  path="./sql/init-tables.sql" relativeToChangelogFile="true" stripComments="true"/>
    </changeSet>
</databaseChangeLog>

修改保存,重新运行程序,我们发现project成功创建出来,至此问题解决。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值