在Ansible上使用PSQL脚本检测交易中的错误

发布于 2025-02-13 05:25:02 字数 422 浏览 0 评论 0原文

有以下脚本(

BEGIN;
SET client_min_messages = warning;

\COPY foo_table FROM 'foo.csv' csv header DELIMITER ';';
\COPY bar_table FROM 'bar.csv' csv header DELIMITER ';';

COMMIT;

我们

- name: 'Restore SQL dump(s) on database(s)'
  become: yes
  become_user: 'postgres'
  postgresql_db:
    name: 'db'
    target: 'script.sql'
    state: 'restore'

脚本

We have the following script (script.sql):

BEGIN;
SET client_min_messages = warning;

\COPY foo_table FROM 'foo.csv' csv header DELIMITER ';';
\COPY bar_table FROM 'bar.csv' csv header DELIMITER ';';

COMMIT;

We launch this script in an ansible playbook with

community.postgresql.postgresql_db

- name: 'Restore SQL dump(s) on database(s)'
  become: yes
  become_user: 'postgres'
  postgresql_db:
    name: 'db'
    target: 'script.sql'
    state: 'restore'

How can we detect if the transaction fail with a rollback to get an error on the deployment ?

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

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

发布评论

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

评论(1

默嘫て 2025-02-20 05:25:02

常见的方法是在psql命令行上使用-v on_error_stop = on,或者在sql脚本中使用 on_error_stop on 。

根据 documentation> documentation

on_error_stop

默认情况下,命令处理在错误后继续进行。当这个
变量设置为ON,处理将立即停止。在
交互式模式,PSQL将返回命令提示符;否则,
PSQL将退出,返回错误代码3,以区分此情况
致命错误条件,使用错误代码1进行了报告。
无论哪种情况,任何当前运行的脚本(顶级脚本,如果
它可能在调用中使用的任何其他脚本)将是
立即终止。如果最高命令字符串包含
多个SQL命令,处理将停止使用当前命令。

The common way is to use -v ON_ERROR_STOP=on on psql command line, or alternatively \set ON_ERROR_STOP on in the SQL script.

According to the documentation:

ON_ERROR_STOP

By default, command processing continues after an error. When this
variable is set to on, processing will instead stop immediately. In
interactive mode, psql will return to the command prompt; otherwise,
psql will exit, returning error code 3 to distinguish this case from
fatal error conditions, which are reported using error code 1. In
either case, any currently running scripts (the top-level script, if
any, and any other scripts which it may have in invoked) will be
terminated immediately. If the top-level command string contained
multiple SQL commands, processing will stop with the current command.

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