如何在 liquibase SQL 命令中传递变量?

问题描述 投票:0回答:3

我正在尝试在启动 docker 之前使用 liquibase 执行一个简单的 sql(posgresql DB) 命令。 这是我的 liquibase 脚本“email-reset.xml”,它在我使用变量时不起作用(硬编码值有效)

<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
                   xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
                                       http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd
                                       http://www.liquibase.org/xml/ns/dbchangelog-ext
                                       http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
    <changeSet id="email-reset" author="keanu">
        <comment>Update User Emails in Dev</comment>
        <sql>
            update cwd_user set email_address='${email.reset}';
        </sql>
    </changeSet>
</databaseChangeLog>

我有一个 liquibase.properties 文件,我在其中定义了

parameter.email.reset=${OVERWRITE_USER_EMAIL}

在我写的docker文件中

ENV OVERWRITE_USER_EMAIL false

我希望在我的 docker 文件中指定值时覆盖数据库中的电子邮件。

[email protected]

我如何实现这一目标?

postgresql docker liquibase confluence
3个回答
1
投票

如果正确理解您的问题,

  1. 使用 changelog 参数,您可以将值传递给 Liquibase 更新过程。
  2. 通过 changeLogPropertyDefined 前提条件,可以检查是否定义了更改日志参数。
  3. 如果您使用 更新更改,而不是普通 SQL,您可以在列的
    value
    属性中传递参数值
<changeSet id="email-reset" author="keanu">
   <update>
      <column name='email_address' value='${email.reset}'/>
   </update >
</changeSet>

希望能帮到你。


0
投票

首先,您不应在属性名称中添加

parameter
前缀。只需指定
email.reset=some_actual_value

其次,您不能使用示例中使用的“双重间接寻址”,即在属性文件中指定环境变量值。系统中没有任何内容可以执行该替换。

如果您想在 dockerfile 中添加一些内容来控制 email.reset 的值,最好不要使用属性文件,而是将该属性作为系统属性在命令行上传递,以便 liquibase run 命令看起来像像这样的:

./liquibase -Demail.reset=true update

如果 email.reset 的值无论如何都被硬编码到 dockerfile 中,则无需设置环境变量只是为了在单个位置使用它。

最后,使用 Liquibase 执行本质上的系统管理任务可能不是完成此任务的最佳方法。如果应用程序本身有一些东西可以在应用程序启动并运行后使用,那就更好了。 Liquibase 是一个用于管理架构更改的工具,而不是用于控制应用程序数据的工具。


0
投票

我通过将变量作为环境参数传递来修复了 docker-compose 中 Liquibase 的相同问题:

  liquibase:
    container_name: liquibase
    image: liquibase/liquibase
    command: liquibase --url="jdbc:postgresql://postgres:5432/postgres" --changeLogFile=changelog.xml --username=${USR} --password=${PSW} update
    volumes:
      - ./backend-liquibase/changelog.xml:/liquibase/changelog.xml
      - ./backend-liquibase/scripts:/liquibase/scripts
    environment:
      - POSTGRES_BACKEND_USER=${POSTGRES_BACKEND_USER}
      - POSTGRES_BACKEND_PASSWORD=${POSTGRES_BACKEND_PASSWORD}
    depends_on:
      - postgres
© www.soinside.com 2019 - 2024. All rights reserved.