即使抛出错误,如何继续执行 Liquibase 脚本?

发布于 2025-01-16 22:04:56 字数 1172 浏览 4 评论 0原文

我正在使用 MySQL 数据库。我正在调用存储过程 3 次以插入 3 条记录。以下是相同的示例。

<?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"
    xmlns:pro="http://www.liquibase.org/xml/ns/pro"
   xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.10.xsd
   http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-3.10.xsd">
    <changeSet id="Demo" author="rambo" runOnChange="true">
        <sql splitStatements="true">            
            CALL insert_animal(NULL, 'Cat', 'Cat', NULL, 0, '', '');
            CALL insert_animal(NULL, 'Mouse', 'Mouse', NULL, 0, '', '');
            CALL insert_animal(NULL, 'Dog', 'Dog', NULL, 0, '', '');            
        </sql>
        <rollback/>
    </changeSet>
</databaseChangeLog>

现在,当我更新上面的脚本以添加第四只动物时,我会收到错误,基本上它表示存在名为 Cat 的记录,并且脚本执行已停止。

我想做的是,即使插入任何动物的 SP 调用由于重复而失败,我也想继续执行。

我是 liquibase 的新手,正在寻求一些建议。

I'm using MySQL database. I'm giving call to an stored procedure for 3 times to insert 3 records. following is the example for the same.

<?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"
    xmlns:pro="http://www.liquibase.org/xml/ns/pro"
   xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.10.xsd
   http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-3.10.xsd">
    <changeSet id="Demo" author="rambo" runOnChange="true">
        <sql splitStatements="true">            
            CALL insert_animal(NULL, 'Cat', 'Cat', NULL, 0, '', '');
            CALL insert_animal(NULL, 'Mouse', 'Mouse', NULL, 0, '', '');
            CALL insert_animal(NULL, 'Dog', 'Dog', NULL, 0, '', '');            
        </sql>
        <rollback/>
    </changeSet>
</databaseChangeLog>

Now when I update this above script for addition of 4th animal lets say, then I get error and basically it says that record exist with the name Cat and script execution is halted.

What I want to do is, I want to continue the execution even if the SP call to insert any animal fails because its duplicate.

I'm new to liquibase and seeking for some advise on this.

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

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

发布评论

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

评论(1

我们的影子 2025-01-23 22:04:56

您试图以错误的方式使用 liquibase。变更集的目标是仅写入一次,并且在将其应用到数据库后永远不会更改。如果您需要向现有变更集添加新内容 - 只需创建新变更集即可。此规则也不例外,特别是如果您已经提交了更改。
实际上,只有一种例外 - 如果您在自己的数据库上测试变更集并且它不满足要求。在这种情况下,您应该从数据库变更日志表中删除相应的行,手动将数据库架构返回到以前的状态,更新变更集并将其再次应用到数据库。
此外,您不应该使用 liquibase 向表中添加新数据。 Liquibase 的目的是改变模式状态,而不是数据库数据。如果你打破了这个规则,你会一路遇到很多问题,因为数据是一个经常变化的对象。应用 liquibase 更改后,使用纯 sql 进行数据迁移。

You trying to use liquibase wrong way. The changeset aimed be written only once and never changed after you apply it to database. If you need to add something new to existing changeset - just create new one. No exception from this rule, especially if you already commit the changes.
Actually, there is only one exception - if you test the changeset on your own database and it is not satisfy the requirements. In this case you should remove corresponding row from databasechangelog table, manually return database schema to previous state, update the changeset and apply it to database again.
In addition, you should NEVER use liquibase to add new data to tables. Liquibase is aimed to change schema state, not database data. If you break this rule, you will get a lot of problems all the way, because the data is a subject for frequently changes. Use plain sql for data migration after you apply liquibase changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文