杂散换行会破坏转储/恢复时的源代码

发布于 2024-10-01 17:37:57 字数 1037 浏览 9 评论 0原文

简而言之:转储/恢复过程使我的函数的源代码看起来很丑!天知道为什么,但是某些东西在我格式精美的源代码中添加了额外的换行符,这让我非常生气(并且使阅读我的代码变得更加困难)。只是恢复数据库后发生的情况的一个小说明:

CREATE OR REPLACE FUNCTION f_tr_std()
  RETURNS trigger AS
$BODY$







begin







  /* Standard trigger function */







  if ( tg_when <> 'BEFORE' ) then







    raise exception 'This must be a "before"-trigger only: "%"', tg_name;







  end if;















  if ( tg_level <> 'ROW' ) then







    raise exception 'This must be a row-level trigger: "%"', tg_name;







  end if;















end;







$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION f_tr_std() OWNER TO postgres;

页眉和页脚是由 pgAdmin 生成的。剩下的就是我自己的代码了。

PG版本:9.0.1 操作系统:Windows XP

我用于转储的bat文件内容:

@echo off
set curr_dir=%CD%
pg_dump --blobs --format=c --compress=9 --verbose --host=localhost --port=5432 -U postgres rc2_dev > "%curr_dir%\dump.bak"
pause

我认为用于恢复的bat文件内容无关紧要,因为转储源内部已经损坏。

我完全不知道是什么导致了如此奇怪的行为!任何帮助将不胜感激。

To be concise: dump/restore process makes my functions’ source code look ugly! God knows why, but something adds extra line breaks to my beautifully formatted source code in a way that makes me really mad (and makes it harder to read my code). Just a small illustration of what happens after I restore my database:

CREATE OR REPLACE FUNCTION f_tr_std()
  RETURNS trigger AS
$BODY$







begin







  /* Standard trigger function */







  if ( tg_when <> 'BEFORE' ) then







    raise exception 'This must be a "before"-trigger only: "%"', tg_name;







  end if;















  if ( tg_level <> 'ROW' ) then







    raise exception 'This must be a row-level trigger: "%"', tg_name;







  end if;















end;







$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION f_tr_std() OWNER TO postgres;

The header and the footer were generated by pgAdmin. The rest is my own code.

PG version: 9.0.1
OS : Windows XP

Contents of bat file I use for dump:

@echo off
set curr_dir=%CD%
pg_dump --blobs --format=c --compress=9 --verbose --host=localhost --port=5432 -U postgres rc2_dev > "%curr_dir%\dump.bak"
pause

Contents of bat file for restore is irrelevant, I reckon, because inside the dump source is damaged already.

I have absolutely no idea what causes such a weird behavior!!! Any help would be much appreciated.

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

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

发布评论

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

评论(2

云淡风轻 2024-10-08 17:37:57

就我而言,问题在于使用 >重定向输出。如果我宁愿使用 -f 让 pg_dump 直接写入文件,那么我会得到格式良好的输出。

所以你的例子将变成:
pg_dump --blobs --host=localhost --port=5432 -U postgres -f "%curr_dir%\dump.bak" rc2_dev

In my case the issue was with using > to redirect the output. If I rather use -f to let pg_dump write straight to the file then I get nicely formatted output.

So your example will become:
pg_dump --blobs --host=localhost --port=5432 -U postgres -f "%curr_dir%\dump.bak" rc2_dev

兔姬 2024-10-08 17:37:57

我敢打赌,问题不在于转储/恢复,而在于 PostgreSQL 和其他 Windows 程序之间的行尾处理。请记住,Windows 使用 CRLF 表示 EOL,即两个字符宽,而 UNIX 使用 CR,Mac 使用 LF。这并不是第一次工具链中其他地方的行尾被不恰当地破坏。

首先要做的是检查数据库中的源代码。对于上面的函数,这将是一个很好的起点:

SELECT pro_src FROM pg_proc WHERE proname = 'f_tr_std'; 

只有两种可能性。 EOL 要么被损坏,要么没有。如果它们被损坏,请检查工具链的其余部分。如果不是,请检查转储和恢复之间使用的每个程序。

I am willing to bet that the problem is not with dump/restore but with end of line handling between PostgreSQL and other Windows programs. Remember that Windows uses CRLF for EOL's which is two characters wide, while UNIX uses CR and Mac uses LF. This wouldn't be the first time end of lines were inappropriately mangled elsewhere in the tool chain.

The first thing to do is check the source code in your database. For the function above, this would be a good place to start:

SELECT pro_src FROM pg_proc WHERE proname = 'f_tr_std'; 

There are only two possibilities. Either the EOL's are mangled there or they aren't. If they are mangled check the rest of your toolchain. If they are not, check every program you are using between dump and restore.

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