java - 执行更新命令时 Liquibase 更改集显示为空

标签 java mysql junit liquibase changeset

在解释我的问题之前,我必须声明我是 Liquibase 的新手。

我正在尝试在我的测试类中运行 liquibase 更新方法。我的代码读取变更集文件,获取锁,从变更日志中读取并释放锁,但不执行变更集本身。这是我的结构:

测试类:

public class LiquibaseTestDbUtil {

    private static final String LIQUIBASE_CHANGELOG_FILE = "liquibase/liquibase-changelog-test.xml";
    private static final String UNIT_NAME = "com.ihsinformatics.tbreach.api.test";
    // Get entity manager
    EntityManager entityManager = Persistence.createEntityManagerFactory(
            UNIT_NAME).createEntityManager();

    @Before
    public void setupDatabase() throws Exception {
        Connection connection = ((SessionFactoryImpl) entityManager.unwrap(
                Session.class).getSessionFactory()).getConnectionProvider()
                .getConnection();
        Database database = DatabaseFactory.getInstance()
                .findCorrectDatabaseImplementation(
                        new JdbcConnection(connection));
        // Get liquibase instance / execute update / drop
        URL file = ClassLoaderUtil.getResource(LIQUIBASE_CHANGELOG_FILE,
                LiquibaseTestDbUtil.class);
        DatabaseChangeLog dbChangeLog = new DatabaseChangeLog(file.getPath());
        Liquibase liquibase = new Liquibase(dbChangeLog,
                new ClassLoaderResourceAccessor(), database);
        liquibase.update(new Contexts("test"));
        // liquibase.dropAll();
    }

    @Test
    public void someTestCase() throws Exception {
    }
}

更改日志文件:

<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.1.xsd">

    <property name="schemaName" value="tbreach_api_test" dbms="mysql,oracle,postgresql" />

    <changeSet author="owais.hussain" context="test" id="20180216-1" runOnChange="true">
        <createTable tableName="data_log" schemaName="${schemaName}">
            <column autoIncrement="true" name="log_id" type="INT">
                <constraints primaryKey="true" />
            </column>
            <column name="log_type" type="CHAR(6)">
                <constraints nullable="false" />
            </column>
            <column name="entity_name" type="VARCHAR(45)">
                <constraints nullable="false" />
            </column>
            <column name="record" type="TEXT" />
            <column name="description" type="TEXT" />
            <column name="date_created" type="datetime(6)">
                <constraints nullable="false" />
            </column>
            <column name="created_by" type="INT" />
            <column name="created_at" type="INT" />
            <column name="uuid" type="CHAR(38)">
                <constraints nullable="false" unique="true" />
            </column>
        </createTable>
    </changeSet>

    <!-- Test Data -->
    <changeSet author="owais.hussain" id="2018-02-19" runOnChange="true">
        <sqlFile dbms="mysql" path="src/test/resources/test-data.sql" />
    </changeSet>

</databaseChangeLog>

我的 persistence.xml 文件保存在 src/test/resources/META-INF/目录中,其中包含以下详细信息:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://java.sun.com/xml/ns/persistence
        http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

    <persistence-unit name="com.ihsinformatics.tbreach.api.test" transaction-type="RESOURCE_LOCAL">
        <description>
            Persistence unit for the JPA tutorial of the Hibernate Getting Started Guide
        </description>
        <provider>org.hibernate.ejb.HibernatePersistence</provider>

        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/tbreach_api_test" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="mypassword" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>

    </persistence-unit>

</persistence>

我的 pom.xml 中有以下依赖项

  • junit 4.10
  • liquibase-core 3.5.4
  • mysql-connector-java 5.1.45
  • 其他依赖项,例如 log4j

当我在 Eclipse 中将 LiquibaseTestDbUtil 作为 Junit 执行时,我得到以下日志:

log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
INFO 2/19/18 2:40 PM: liquibase: Successfully acquired change log lock
INFO 2/19/18 2:40 PM: liquibase: Reading from tbreach_api_test.DATABASECHANGELOG
INFO 2/19/18 2:40 PM: liquibase: Successfully released change log lock

但是我的数据库(tbreach_api_test)看起来是空的,只有2个liquibase表,其中databasechangelog是空的。

我进入 Debug模式,发现 Liquibase 类中的 ChangeLog 对象已初始化,但为空。

我错过了什么?

  • 请注意,我已经注释掉了 liquibase.dropAll() 方法,因此数据库更改应该持续存在(据我所知)。

最佳答案

尝试使用替代的 liquibase 构造函数

Liquibase liquibase = new Liquibase(
    "your file path", 
    new FileSystemResourceAccessor(), 
    database
);

关于java - 执行更新命令时 Liquibase 更改集显示为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48863426/

相关文章:

java - 如何使 JDialog 处于非 Activity 状态

Java ArrayList 和对象问题。数值重置

php - 在 laravel php 中注册新用户时更新多个用户详细信息

mysql - 获取顶级用户排名位置取决于连接查询的最大计数

java - 通过捕获失败步骤的失败原因,继续在 Serenity Jbehave BDD 中执行后续步骤

java - 无法理解策略模式

java - Android - 无法修复折叠工具栏布局中的工具栏

mysql - sql按逻辑排序

Java JUnit 测试不适用于 @Before 注释

java - IntelliJ IDEA 中的 JUnit 测试问题 - 找不到类