Merge lp:~percona-toolkit-dev/percona-toolkit/fix-bug-903379 into lp:percona-toolkit/2.1

Proposed by Brian Fraser
Status: Merged
Merged at revision: 272
Proposed branch: lp:~percona-toolkit-dev/percona-toolkit/fix-bug-903379
Merge into: lp:percona-toolkit/2.1
Diff against target: 105 lines (+64/-11)
2 files modified
bin/pt-archiver (+22/-10)
t/pt-archiver/file.t (+42/-1)
To merge this branch: bzr merge lp:~percona-toolkit-dev/percona-toolkit/fix-bug-903379
Reviewer Review Type Date Requested Status
Daniel Nichter Approve
Brian Fraser (community) Approve
Review via email: mp+107645@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Brian Fraser (fraserbn) :
review: Approve
Revision history for this message
Daniel Nichter (daniel-nichter) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bin/pt-archiver'
2--- bin/pt-archiver 2012-05-24 17:25:20 +0000
3+++ bin/pt-archiver 2012-05-28 14:46:19 +0000
4@@ -3962,18 +3962,30 @@
5 # Open the file and print the header to it.
6 if ( $archive_file ) {
7 my $need_hdr = $o->get('header') && !-f $archive_file;
8- my $charset = lc $o->get('charset');
9- if ( $charset ) {
10- $archive_fh = IO::File->new($archive_file, ">>:$charset")
11- or die "Cannot open $charset $archive_file: $OS_ERROR\n";
12- }
13- else {
14- $archive_fh = IO::File->new($archive_file, ">>")
15- or die "Cannot open $archive_file: $OS_ERROR\n";
16- }
17+ my $charset = $o->get('charset') || '';
18+ if ($charset eq 'utf8') {
19+ $charset = ":$charset";
20+ }
21+ elsif ($charset) {
22+ eval { require Encode }
23+ or (PTDEBUG &&
24+ _d("Couldn't load Encode: ", $EVAL_ERROR,
25+ "Going to try using the charset ",
26+ "passed in without checking it."));
27+ # No need to punish a user if they did their
28+ # homework and passed in an official charset,
29+ # rather than an alias.
30+ $charset = ":encoding("
31+ . (defined &Encode::resolve_alias
32+ ? Encode::resolve_alias($charset) || $charset
33+ : $charset)
34+ . ")";
35+ }
36+ $archive_fh = IO::File->new($archive_file, ">>$charset")
37+ or die "Cannot open $charset $archive_file: $OS_ERROR\n";
38 $archive_fh->autoflush(1) unless $o->get('buffer');
39 if ( $need_hdr ) {
40- print $archive_fh '', escape(\@sel_cols), "\n"
41+ print { $archive_fh } '', escape(\@sel_cols), "\n"
42 or die "Cannot write to $archive_file: $OS_ERROR\n";
43 }
44 }
45
46=== modified file 't/pt-archiver/file.t'
47--- t/pt-archiver/file.t 2011-07-12 22:56:55 +0000
48+++ t/pt-archiver/file.t 2012-05-28 14:46:19 +0000
49@@ -23,7 +23,7 @@
50 plan skip_all => 'Cannot connect to sandbox master';
51 }
52 else {
53- plan tests => 5;
54+ plan tests => 11;
55 }
56
57 my $output;
58@@ -72,6 +72,47 @@
59 `rm -f archive.test.table_1`;
60
61 # #############################################################################
62+# Bug #903379: --file & --charset could cause warnings and exceptions
63+# #############################################################################
64+
65+sub test_charset {
66+ my ($charset) = @_;
67+
68+ $sb->load_file('master', 't/pt-archiver/samples/table1.sql');
69+ local $@;
70+ eval {
71+ pt_archiver::main("-c", "b,c", qw(--where 1=1 --header),
72+ "--source", "D=test,t=table_1,F=$cnf",
73+ '--file', '/tmp/%Y-%m-%d-%D_%H:%i:%s.%t',
74+ '--no-check-charset',
75+ '--charset', $charset,
76+ );
77+ };
78+
79+ ok !$@, "--charset $charset works";
80+}
81+
82+for my $charset (qw(latin1 iso-8859-1 utf8 UTF-8 )) {
83+ test_charset($charset);
84+}
85+
86+my $warning;
87+local $SIG{__WARN__} = sub { $warning .= shift };
88+my $out = output( sub {
89+ $sb->load_file('master', 't/pt-archiver/samples/table1.sql');
90+ pt_archiver::main("-c", "b,c", qw(--where 1=1 --header),
91+ "--source", "D=test,t=table_1,F=$cnf",
92+ '--file', '/tmp/%Y-%m-%d-%D_%H:%i:%s.%t',
93+ '--no-check-charset',
94+ '--charset', "some_charset_that_doesn't_exist",
95+ );
96+ },
97+);
98+
99+like($out, qr/\QCannot open :encoding(some_chars/, "..but an unknown charset fails");
100+like($warning, qr/Cannot find encoding/, "..and throws a useful warning");
101+
102+# #############################################################################
103 # Done.
104 # #############################################################################
105 $sb->wipe_clean($dbh);

Subscribers

People subscribed via source and target branches