我正在努力使用runAlways="true"属性从.csv文件中重新加载数据。我的目标是重写(更新)从app_version.csv文件读取的应用程序版本,以防值发生变化。如果更改,我使用Liquibase API来运行liquibase.update(..),这应该会触发包含节的changeSet。
我的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>
...Application.properties:
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.DriverDB表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包含changeSet的.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();
}
}我的意图是: first changeSet (id="app_version")从.csv文件中加载数据,工作。在运行runLiquibaseUpdate方法后,据我所知(如果我错了,请纠正我),由于runAlways="true",它应该触发changeSet(id="update_app_version"),数据库中的值应该更改为.csv文件中的初始值,但它没有。
到目前为止,我还不能确定问题的原因,在方法完成后,日志没有显示任何内容。
如果我的理解是错误的,这是不可行的,我恳请如何实现这一点的建议。
发布于 2020-01-22 04:06:59
您在Application.properties中定义的changelog仅对prod上下文有效,但您已在无上下文模式下调用了liquibase:
spring.liquibase.contexts=prod因此,要么删除属性文件中的条目,要么将其提供给liquibase:
liquibase.update(new Contexts("prod), new LabelExpression());https://stackoverflow.com/questions/59845187
复制相似问题