繁体   English   中英

Java中的Liquibase回滚似乎不起作用

[英]Liquibase Rollback in Java Doesn't Seem To Be Working

我正在使用Liquibase作为Java集成测试的一部分。 在集成测试阶段,我使用Liquibase在@BeforeClass函数中创建并填充表。 一旦应用我的变更集,它们就会被标记。 在@After函数中,我回滚到特定标签。

然后,我可以运行单元测试以从数据库以及INSERT中进行选择。 但是,我随后尝试在每次测试后回滚更改,并发现Liquibase没有回滚我的更改。

我正在对嵌入式Derby数据库使用Liquibase 3.1.1。

我的变更集文件:

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

<include file="create-table-event-type.xml" relativeToChangelogFile="true"/>
<include file="load-table-event-type.xml" relativeToChangelogFile="true"/>

<include file="create-table-event.xml" relativeToChangelogFile="true"/>
<include file="load-table-event.xml" relativeToChangelogFile="true"/>

<changeSet id="tag-integration-test-database" author="Ivan Suftin" context="integration-test">
    <tagDatabase tag="integration-tests-tag" />
</changeSet>

</databaseChangeLog>

我的单元测试:

@Category(IntegrationTest.class)
public class EventDAOTest {

private static Connection conn;
private static SqlSessionFactory sqlSessionFactory;
private static Liquibase liquibase;
private EventDAO instance = null;
private static final String rollbackTag = "integration-tests-tag";
private static final Contexts contexts = new Contexts("integration-test");

public EventDAOTest() {
}

@BeforeClass
public static void setUpClass() throws ClassNotFoundException, SQLException, DatabaseException, LiquibaseException, InstantiationException, IllegalAccessException, IOException {
    String port = System.getProperty("db.twitter.integration-test.port");
    String driver = System.getProperty("db.twitter.integration-test.driver");
    String dbType = System.getProperty("db.twitter.integration-test.dbtype");
    String schema = System.getProperty("db.twitter.integration-test.schema");
    if (StringUtils.isBlank(port)) {
        throw new NullPointerException("System property \"db.twitter.integration-test.port\" not found");
    }
    if (StringUtils.isBlank(driver)) {
        throw new NullPointerException("System property \"db.twitter.integration-test.driver\" not found");
    }
    if (StringUtils.isBlank(dbType)) {
        throw new NullPointerException("System property \"db.twitter.integration-test.dbType\" not found");
    }
    if (StringUtils.isBlank(schema)) {
        throw new NullPointerException("System property \"db.twitter.integration-test.schema\" not found");
    }

    Class.forName(driver).newInstance();

    conn = DriverManager.getConnection("jdbc:" + dbType + "://localhost:" + port + "/" + schema + ";create=true", "test", "test");

    Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(conn));
    liquibase = new Liquibase("src/main/resources/liquibase/changelogs/create-table-parent-changeLog.xml", new FileSystemResourceAccessor(), database);

    liquibase.update(contexts);

    try (InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml")) {
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, "integration-test");
    }
}


@AfterClass
public static void tearDownClass() throws SQLException {
    conn.close();
}

@Before
public void beforeTest() {
    instance = new EventDAO(sqlSessionFactory);
}

@After
public void afterTest() throws DatabaseException, LiquibaseException {
    liquibase.rollback(rollbackTag, contexts);
}

[...]

剩下的测试只是我使用MyBatis进行SELECT和INSERT的测试。

这是日志的样子(仅粘贴重要部分)

INFO 3/8/14 7:23 PM:liquibase: Successfully acquired change log lock
INFO 3/8/14 7:23 PM:liquibase: Creating database history table with name: TEST.DATABASECHANGELOG
INFO 3/8/14 7:23 PM:liquibase: Reading from TEST.DATABASECHANGELOG

[...运行变更集...]

INFO 3/8/14 7:23 PM:liquibase: src/main/resources/liquibase/changelogs/create-table-parent-changeLog.xml: tag-integration-test-database::Ivan Suftin: Reading from TEST.DATABASECHANGELOG
INFO 3/8/14 7:23 PM:liquibase: src/main/resources/liquibase/changelogs/create-table-parent-changeLog.xml: tag-integration-test-database::Ivan Suftin: Tag 'integration-tests-tag' applied to database
INFO 3/8/14 7:23 PM:liquibase: src/main/resources/liquibase/changelogs/create-table-parent-changeLog.xml: tag-integration-test-database::Ivan Suftin: ChangeSet src/main/resources/liquibase/changelogs/create-table-parent-changeLog.xml::tag-integration-test-database::Ivan Suftin ran successfully in 11ms
INFO 3/8/14 7:23 PM:liquibase: src/main/resources/liquibase/changelogs/create-table-parent-changeLog.xml: tag-integration-test-database::Ivan Suftin: Reading from TEST.DATABASECHANGELOG
INFO 3/8/14 7:23 PM:liquibase: Successfully released change log lock

在每次测试之间,我尝试运行回滚,以便获得一个新加载的数据库,并在此处显示日志:

INFO 3/8/14 7:23 PM:liquibase: Successfully acquired change log lock
INFO 3/8/14 7:23 PM:liquibase: Reading from TEST.DATABASECHANGELOG
INFO 3/8/14 7:23 PM:liquibase: Reading from TEST.DATABASECHANGELOG
INFO 3/8/14 7:23 PM:liquibase: Successfully released change log lock
-- *********************************************************************
-- Rollback to 'integration-tests-tag' Script
-- *********************************************************************
-- Change Log: src/main/resources/liquibase/changelogs/create-table-parent-changeLog.xml
-- Ran at: 3/8/14 7:23 PM
-- Against: test@jdbc:derby://localhost:59527/twitter;create=true
-- Liquibase version: 3.1.1
-- *********************************************************************

-- Lock Database
-- Rolling Back ChangeSet: src/main/resources/liquibase/changelogs/create-table-parent-changeLog.xml::tag-integration-test-database::Ivan Suftin
DELETE FROM TEST.DATABASECHANGELOG  WHERE ID='tag-integration-test-database' AND AUTHOR='Ivan Suftin' AND FILENAME='src/main/resources/liquibase/changelogs/create-table-parent-changeLog.xml';

-- Release Database Lock

我最初测试的表有两行。 我有两个插入的测试。 他们一个接一个地跑。 在第一个测试中,我对一行运行INSERT,然后立即运行SELECT来计算行数。 结果显示3行,这是正确的。

下一个测试还对一行运行INSERT,然后再次运行SELECT来测试行数,它应该为3行,但现在我得到4行。 这告诉我@After函数中的回滚实际上什么也没做。

我在这里做错什么了吗?

我还尝试过通过运行以下命令在运行更改日志后立即使用Java API创建标签:

liquibase.tag(rollbackTag); 

...但是我得到相同的结果。 没有任何回滚。

Liquibase不会自动为插入语句生成回滚命令,因此,除非您手动将其放入,否则Liquibase不会做任何事情。 有关该文档,请参见http://www.liquibase.org/documentation/rollback.html

许多重构(例如“创建表”,“重命名列”和“添加列”)可以自动创建回滚语句。 如果更改日志仅包含适合该类别的语句,则将自动生成回滚命令。

其他重构(例如“删除表”和“插入数据”)没有可以自动生成的相应回滚命令。 在这些情况下,以及要覆盖默认生成的回滚命令的情况下,可以通过changeSet标记内的标记指定回滚命令。 如果您不想做任何操作来撤消回滚模式的更改,请使用一个空标签。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM