![](/img/trans.png)
[英]liquibase failOnError:false runAlways:false not working
[英]Liquibase changeSet with runAlways not executed by liquibase.update
我正在努力使用runAlways="true"
属性从 .csv 文件重新加载数据。 我的目标是重写(更新)从app_version.csv
文件读取的应用程序版本,以防值更改。 如果更改,我使用 Liquibase API 运行liquibase.update(..)
这应该触发具有部分的liquibase.update(..)
。
我的 pom.xml:
...
<!-- Persistance DEPS -->
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
<dependency>
<groupId>org.liquibase.ext</groupId>
<artifactId>liquibase-hibernate5</artifactId>
<version>3.6</version>
</dependency>
...
应用程序属性:
spring.liquibase.change-log=classpath:db/changelog/db.changelog-master.xml
spring.liquibase.contexts=prod
spring.liquibase.enabled=true
# H2
spring.datasource.url=jdbc:h2:file:~/author-db/testdb
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
DB 表global_settings
(目前在文件中使用 H2,pk="id"):
ID | APP_KEY | APP_VALUE | CREATED_BY | CREATED_DATE | LAST_MODIFIED_BY | LAST_MODIFIED_DATE
1 app-version 4.3.3-SNAPSHOT system 2020-01-21 16:11:10.009 system null
包含更改集的 .xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<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.5.xsd">
<changeSet id="app_version" author="author">
<loadData file="db/app_version.csv" separator=";"
tableName="global_settings">
<column name="id" type="numeric"/>
<column name="created_date" type="timestamp"/>
<column name="last_modified_date" type="timestamp"/>
</loadData>
</changeSet>
<changeSet id="update_app_version" author="author" runAlways="true">
<loadUpdateData file="db/app_version.csv" separator=";" tableName="global_settings"
primaryKey="id">
<column name="id" type="numeric"/>
<column name="created_date" type="timestamp"/>
<column name="last_modified_date" type="timestamp"/>
</loadUpdateData>
</changeSet>
</databaseChangeLog>
app_version.csv
文件的内容:
id;app_key;app_value;created_by;last_modified_by
1;app-version;@project.version@;system;system
用于运行的 Java 代码:
inside main spring boot class
...
Optional<GlobalSettings> appSettings = globalSettingsRepository.findOneByAppKey("app-version"); --> value = 4.3.3-SNAPSHOT
appSettings.get().setValue("4.3.2"); --> value = 4.3.2
globalSettingsRepository.save(appSettings.get());
runLiquibaseUpdate(); --> value = 4.3.2
--------------
public void runLiquibaseUpdate() {
Database database;
Connection connection;
Liquibase liquibase;
try {
connection = DriverManager.getConnection(env.getProperty("spring.datasource.url"),
env.getProperty("spring.datasource.username"),
env.getProperty("spring.datasource.password"));
database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
DatabaseChangeLog changeLog = new DatabaseChangeLog(env.getProperty("spring.liquibase.change-log"));
liquibase = new Liquibase(changeLog, new FileSystemResourceAccessor(), database);
liquibase.update(new Contexts(), new LabelExpression());
} catch (SQLException | LiquibaseException e) {
e.printStackTrace();
}
}
我的意图是:第一个 changeSet (id="app_version") 从 .csv 文件加载数据,有效。 runLiquibaseUpdate
方法运行后,根据我的理解(如果我错了请纠正我),它应该触发runAlways="true"
(id="update_app_version") 由于runAlways="true"
并且数据库中的值应该更改为初始值在 .csv 文件中找到值,但没有。
到目前为止,我还无法查明问题的原因,方法完成后日志没有显示任何内容。
如果我的理解是错误的并且这是不可行的,我恳请您提供有关如何实现这一目标的建议。
您在Application.properties
定义的更改日志仅对prod
上下文有效,但您已在无上下文模式下调用 liquibase:
spring.liquibase.contexts=prod
因此,要么删除属性文件中的条目,要么将其提供给 liquibase:
liquibase.update(new Contexts("prod), new LabelExpression());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.