首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >liquibase.update未执行带有runAlways的Liquibase changeSet

liquibase.update未执行带有runAlways的Liquibase changeSet
EN

Stack Overflow用户
提问于 2020-01-22 00:17:01
回答 1查看 922关注 0票数 0

我正在努力使用runAlways="true"属性从.csv文件中重新加载数据。我的目标是重写(更新)从app_version.csv文件读取的应用程序版本,以防值发生变化。如果更改,我使用Liquibase API来运行liquibase.update(..),这应该会触发包含节的changeSet。

我的pom.xml:

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

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

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

代码语言:javascript
复制
<?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文件的内容:

代码语言:javascript
复制
id;app_key;app_value;created_by;last_modified_by
1;app-version;@project.version@;system;system

用于运行的Java代码:

代码语言:javascript
复制
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文件中的初始值,但它没有。

到目前为止,我还不能确定问题的原因,在方法完成后,日志没有显示任何内容。

如果我的理解是错误的,这是不可行的,我恳请如何实现这一点的建议。

EN

回答 1

Stack Overflow用户

发布于 2020-01-22 04:06:59

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

代码语言:javascript
复制
spring.liquibase.contexts=prod

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

代码语言:javascript
复制
 liquibase.update(new Contexts("prod), new LabelExpression());
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59845187

复制
相关文章

相似问题

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