database 如何在 liquibase 中标记变更集以回滚
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11131978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to tag a changeset in liquibase to rollback
提问by Manu
I have configured the maven pluggin for liquibase as specified in maven configuration.
Now created a changeset like :-
我已经配置的行家撑着为liquibase中规定行家配置。
现在创建了一个变更集,如:-
<changeSet id="changeRollback" author="nvoxland">
<createTable tableName="changeRollback1">
<column name="id" type="int"/>
</createTable>
<rollback>
<dropTable tableName="changeRollback1"/>
</rollback>
</changeSet>
Created the sql to update DB using command line :- mvn liquibase:updateSQL
使用命令行创建 sql 以更新数据库:- mvn liquibase:updateSQL
But just want to know how to rollback using a "rollbackTag" parameter. i.e. If run the command "mvn liquibase:rollbackSQL", what should be the value of "rollbackTag" parameter.
但只想知道如何使用“rollbackTag”参数回滚。即如果运行命令“ mvn liquibase:rollbackSQL”,“rollbackTag”参数的值应该是多少。
And is it possible to rollback using the changeset id ?
是否可以使用变更集 id 回滚?
回答by Mark O'Connor
Rollback tags are designed to checkpoint your database's configuration.
回滚标签旨在检查您的数据库配置。
The following commands will roll the database configuration back by 3 changesets and create a tag called "checkpoint":
以下命令将数据库配置回滚 3 个变更集并创建一个名为“检查点”的标签:
mvn liquibase:rollback -Dliquibase.rollbackCount=3
mvn liquibase:tag -Dliquibase.tag=checkpoint
You can now update the database, and at any stage rollback to that point using the rollback tag:
您现在可以更新数据库,并在任何阶段使用回滚标记回滚到该点:
mvn liquibase:rollback -Dliquibase.rollbackTag=checkpoint
or alternatively generate the rollback SQL:
或者生成回滚 SQL:
mvn liquibase:rollbackSQL -Dliquibase.rollbackTag=checkpoint
Revised example
修改示例
I initially found it difficult to figure out how to configure the liquibase Maven plugin. Just in case it helps here's the example I've used.
我最初发现很难弄清楚如何配置 liquibase Maven 插件。以防万一它有帮助,这是我使用的示例。
The liquibase update is configured to run automatically, followed by tagging the database at the current Maven revision number.
liquibase 更新配置为自动运行,然后在当前 Maven 修订号处标记数据库。
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.myspotontheweb.db</groupId>
<artifactId>liquibase-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<!-- Liquibase settings -->
<liquibase.url>jdbc:h2:target/db1/liquibaseTest;AUTO_SERVER=TRUE</liquibase.url>
<liquibase.driver>org.h2.Driver</liquibase.driver>
<liquibase.username>user</liquibase.username>
<liquibase.password>pass</liquibase.password>
<liquibase.changeLogFile>com/myspotontheweb/db/changelog/db-changelog-master.xml</liquibase.changeLogFile>
<liquibase.promptOnNonLocalDatabase>false</liquibase.promptOnNonLocalDatabase>
</properties>
<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.162</version>
</dependency>
</dependencies>
<profiles>
<profile>
<id>dbupdate</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>2.0.2</version>
<executions>
<execution>
<phase>process-resources</phase>
<configuration>
<tag>${project.version}</tag>
</configuration>
<goals>
<goal>update</goal>
<goal>tag</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Liquibase is now configured as part of the standard life-cycle so can be run as follows:
Liquibase 现在被配置为标准生命周期的一部分,因此可以按如下方式运行:
mvn clean compile
回答by kothvandir
I personally prefer to put the tag as part of the changeset files, so if you have to rollback or delete all the records in the DATABASECHANGELOGyou won't loose your tagging records.
我个人更喜欢将标记作为变更集文件的一部分,因此如果您必须回滚或删除其中的所有记录,DATABASECHANGELOG您将不会丢失标记记录。
<databaseChangeLog>
<changeSet id="001_create_tables" .../>
<changeSet id="002_alter_tables" .../>
<changeSet id="003_load_user_data" .../>
<!-- Also include the tagging itself as a changeSet... -->
<changeSet author="userId" id="tag_version_0_1_0">
<tagDatabase tag="version_0.1.0" />
</changeSet>
<!-- version 0.1.0 ends here -->
</databaseChangeLog>

