Liquibase Changeet:如何根据另一个表构建子句

发布于 2025-01-24 22:22:48 字数 531 浏览 3 评论 0原文

基于问题 liquibase更改,我想根据来自其他表格的列的值选择列的默认值。

例如,我有两个表格, order order_history 。请注意,我们也将所有订单都保持在 order order_history 中。订单完成后,将其从订单表中删除。

order 具有“状态”列,但我们在 order_history 上错过了它,您可以称其为不良设计。 我现在如何将“状态”添加到 order_history 现在,然后从相应的顺序复制值 在升级时在顺序表中用于现有数据?有没有办法在Liquibase中做到这一点?

Building upon the question How to build a WHERE-clause in a LiquiBase changeset, I would like to choose the default value of a column based on the value of a column from a different table.

For instance, I have two tables, order and order_history. Pls note that we maintain all our orders in both order in order_history as well. Once an order is completed, it is removed from order table.

order has a column 'status' but we missed it on order_history, you can call it a bad design.
How do I add 'status' to order_history now, and copy the value from the corresponding order
in order table for existing data at the time of upgrade? Is there a way to do this in liquibase?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云柯 2025-01-31 22:22:48

如果orderorder_history与外键连接,则可以执行以下操作:

<changeSet id="foo" author="bar">
    <preConditions onFail="MARK_RAN">
        <and>
            <columnExists tableName="order" columnName="status"/>
            <columnExists tableName="order_history" columnName="status"/>
        </and>
    </preConditions>
    <comment>Update order_history.status with values from order.status, where order.id = order_history.order_id</comment>
    <update tableName="order_history">
        <column name="status" valueComputed="SELECT o.status FROM order o WHERE o.id = order_id"/>
    </update>
</changeSet>

如果这些表未连接,则可以使用defaultValueComputed 添加新列时:

    <addColumn tableName="order_history">
        <column name="status" type="varchar(255)" defaultValueComputed="some SQL query here"/>
    </addColumn>

If order and order_history are connected with the foreign key, then you can just do the following:

<changeSet id="foo" author="bar">
    <preConditions onFail="MARK_RAN">
        <and>
            <columnExists tableName="order" columnName="status"/>
            <columnExists tableName="order_history" columnName="status"/>
        </and>
    </preConditions>
    <comment>Update order_history.status with values from order.status, where order.id = order_history.order_id</comment>
    <update tableName="order_history">
        <column name="status" valueComputed="SELECT o.status FROM order o WHERE o.id = order_id"/>
    </update>
</changeSet>

If these tables are not connected, then you may use defaultValueComputed when you're adding a new column:

    <addColumn tableName="order_history">
        <column name="status" type="varchar(255)" defaultValueComputed="some SQL query here"/>
    </addColumn>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文