简体   繁体   English

liquibase.update 不执行带有 runAlways 的 Liquibase changeSet

[英]Liquibase changeSet with runAlways not executed by liquibase.update

I am struggling to reload data from a .csv file using runAlways="true" attribute.我正在努力使用runAlways="true"属性从 .csv 文件重新加载数据。 My goal is to rewrite(update) application version read from app_version.csv file in case the value changed.我的目标是重写(更新)从app_version.csv文件读取的应用程序版本,以防值更改。 If changed, I use Liquibase API to run liquibase.update(..) which should trigger the changeSet that has section.如果更改,我使用 Liquibase API 运行liquibase.update(..)这应该触发具有部分的liquibase.update(..)

My pom.xml:我的 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.Driver

DB table global_settings (using H2 in file at the moment, pk="id"): 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

The .xml file containing the 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>

Contents of the app_version.csv file: app_version.csv文件的内容:

id;app_key;app_value;created_by;last_modified_by
1;app-version;@project.version@;system;system

Java code used to run:用于运行的 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();
    }
  }

My intention was this: first changeSet (id="app_version") loads the data from .csv file, works.我的意图是:第一个 changeSet (id="app_version") 从 .csv 文件加载数据,有效。 After runLiquibaseUpdate method is run, per my understanding (please correct me if I'm wrong), it should trigger the changeSet(id="update_app_version") due to runAlways="true" and the value in the database should change to the initial value found in .csv file, but it doesn't. runLiquibaseUpdate方法运行后,根据我的理解(如果我错了请纠正我),它应该触发runAlways="true" (id="update_app_version") 由于runAlways="true"并且数据库中的值应该更改为初始值在 .csv 文件中找到值,但没有。

So far, I have not been able to pinpoint the cause of the problem, logs are not showing anything after method is finished.到目前为止,我还无法查明问题的原因,方法完成后日志没有显示任何内容。

If my understanding is wrong and this is not doable, I kindly ask for an advice on how to achieve this.如果我的理解是错误的并且这是不可行的,我恳请您提供有关如何实现这一目标的建议。

The changelog you have defined in your Application.properties is only valid for the prod context but you have called liquibase in no-context mode:您在Application.properties定义的更改日志仅对prod上下文有效,但您已在无上下文模式下调用 liquibase:

spring.liquibase.contexts=prod

So either remove the entry in the properties file or provide it to liquibase:因此,要么删除属性文件中的条目,要么将其提供给 liquibase:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM