gpt4 book ai didi

java - Liquibase ChangeSet 与 runAlways 不由 liquibase.update 执行

转载 作者:行者123 更新时间:2023-12-01 18:45:43 26 4
gpt4 key购买 nike

我正在努力使用 runAlways="true" 从 .csv 文件重新加载数据属性。我的目标是重写(更新)从 app_version.csv 读取的应用程序版本文件以防值发生变化。如果更改,我使用 Liquibase API 运行 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

数据库表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方法运行后,根据我的理解(如果我错了,请纠正我),它应该触发changeSet(id="update_app_version"),因为runAlways="true"并且数据库中的值应更改为 .csv 文件中找到的初始值,但事实并非如此。

到目前为止,我还无法查明问题的原因,方法完成后日志没有显示任何内容。

如果我的理解是错误的并且这是不可行的,我恳请您提供有关如何实现这一目标的建议。

最佳答案

您在 Application.properties 中定义的变更日志仅对 prod 上下文有效,但您在无上下文模式下调用了 liquibase:

spring.liquibase.contexts=prod

因此,要么删除属性文件中的条目,要么将其提供给 liquibase:

 liquibase.update(new Contexts("prod), new LabelExpression());

关于java - Liquibase ChangeSet 与 runAlways 不由 liquibase.update 执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59845187/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com