首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java中的Liquibase回滚似乎不起作用

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

Stack Overflow用户
提问于 2014-03-09 01:49:30
回答 1查看 3.7K关注 0票数 2

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

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

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

我的变更文件:

代码语言:javascript
复制
<?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>

我的单元测试:

代码语言:javascript
复制
@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进行选择和插入。

下面是日志的样子(只是粘贴重要的部分)

代码语言:javascript
复制
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

..。运行改变集..。

代码语言:javascript
复制
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

在每个测试之间,我尝试运行一个回滚,以便使用一个新加载的数据库,日志如下所示:

代码语言:javascript
复制
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在changelogs运行后立即创建一个标记:

代码语言:javascript
复制
liquibase.tag(rollbackTag); 

..。但我也得到了同样的结果。什么都没回过头。

EN

回答 1

Stack Overflow用户

发布于 2014-03-10 13:49:51

Liquibase不会自动为insert语句生成rollback命令,所以除非您将这些命令放在手动中,否则Liquibase不会做任何事情。有关这方面的文档,请参见http://www.liquibase.org/documentation/rollback.html。

许多重构,如“创建表”、“重命名列”和“添加列”可以自动创建rollback语句。如果更改日志只包含适合此类别的语句,则将自动生成回滚命令。 其他重构(如“drop”和“insert data”)没有能够自动生成的相应回滚命令。在这些情况下,以及在要覆盖默认生成的回滚命令的情况下,可以通过changeSet标记中的标记指定回滚命令。如果不希望执行任何操作来撤消回滚模式中的更改,请使用空标记。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22277453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档