mirror of
https://github.com/percona/percona-toolkit.git
synced 2025-09-19 02:05:23 +00:00
PT-141 added --output-format to pt-archiver
This commit is contained in:
@@ -5893,6 +5893,8 @@ my $get_sth;
|
|||||||
my ( $OUT_OF_RETRIES, $ROLLED_BACK, $ALL_IS_WELL ) = ( 0, -1, 1 );
|
my ( $OUT_OF_RETRIES, $ROLLED_BACK, $ALL_IS_WELL ) = ( 0, -1, 1 );
|
||||||
my ( $src, $dst );
|
my ( $src, $dst );
|
||||||
my $pxc_version = '0';
|
my $pxc_version = '0';
|
||||||
|
my $fields_separated_by = "\t";
|
||||||
|
my $optionally_enclosed_by;
|
||||||
|
|
||||||
# Holds the arguments for the $sth's bind variables, so it can be re-tried
|
# Holds the arguments for the $sth's bind variables, so it can be re-tried
|
||||||
# easily.
|
# easily.
|
||||||
@@ -6522,12 +6524,19 @@ sub main {
|
|||||||
|
|
||||||
# Open the file and print the header to it.
|
# Open the file and print the header to it.
|
||||||
if ( $archive_file ) {
|
if ( $archive_file ) {
|
||||||
|
if ($o->got('output-format') && $o->get('output-format') ne 'dump' && $o->get('output-format') ne 'csv') {
|
||||||
|
warn "Invalid output format:". $o->get('format');
|
||||||
|
warn "Using default 'dump' format";
|
||||||
|
} elsif ($o->get('output-format') || '' eq 'csv') {
|
||||||
|
$fields_separated_by = ", ";
|
||||||
|
$optionally_enclosed_by = '"';
|
||||||
|
}
|
||||||
my $need_hdr = $o->get('header') && !-f $archive_file;
|
my $need_hdr = $o->get('header') && !-f $archive_file;
|
||||||
$archive_fh = IO::File->new($archive_file, ">>$charset")
|
$archive_fh = IO::File->new($archive_file, ">>$charset")
|
||||||
or die "Cannot open $charset $archive_file: $OS_ERROR\n";
|
or die "Cannot open $charset $archive_file: $OS_ERROR\n";
|
||||||
$archive_fh->autoflush(1) unless $o->get('buffer');
|
$archive_fh->autoflush(1) unless $o->get('buffer');
|
||||||
if ( $need_hdr ) {
|
if ( $need_hdr ) {
|
||||||
print { $archive_fh } '', escape(\@sel_cols), "\n"
|
print { $archive_fh } '', escape(\@sel_cols, $fields_separated_by, $optionally_enclosed_by), "\n"
|
||||||
or die "Cannot write to $archive_file: $OS_ERROR\n";
|
or die "Cannot write to $archive_file: $OS_ERROR\n";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6572,7 +6581,7 @@ sub main {
|
|||||||
# problem, hopefully the data has at least made it to the file.
|
# problem, hopefully the data has at least made it to the file.
|
||||||
my $escaped_row;
|
my $escaped_row;
|
||||||
if ( $archive_fh || $bulkins_file ) {
|
if ( $archive_fh || $bulkins_file ) {
|
||||||
$escaped_row = escape([@{$row}[@sel_slice]]);
|
$escaped_row = escape([@{$row}[@sel_slice]], $fields_separated_by, $optionally_enclosed_by);
|
||||||
}
|
}
|
||||||
if ( $archive_fh ) {
|
if ( $archive_fh ) {
|
||||||
trace('print_file', sub {
|
trace('print_file', sub {
|
||||||
@@ -7036,7 +7045,8 @@ sub escape {
|
|||||||
return join($fields_separated_by, map {
|
return join($fields_separated_by, map {
|
||||||
s/([\t\n\\])/\\$1/g if defined $_; # Escape tabs etc
|
s/([\t\n\\])/\\$1/g if defined $_; # Escape tabs etc
|
||||||
$_ = defined $_ ? $_ : '\N'; # NULL = \N
|
$_ = defined $_ ? $_ : '\N'; # NULL = \N
|
||||||
$_ =~ s/[^\\]"/\\"/g if $_ & ~$_ && $optionally_enclosed_by eq '"';
|
# var & ~var will return 0 only for numbers
|
||||||
|
$_ =~ s/([^\\])"/$1\\"/g if ($_ & ~$_ && $optionally_enclosed_by eq '"');
|
||||||
$_ = $optionally_enclosed_by && $_ & ~$_ ? $optionally_enclosed_by."$_".$optionally_enclosed_by : $_;
|
$_ = $optionally_enclosed_by && $_ & ~$_ ? $optionally_enclosed_by."$_".$optionally_enclosed_by : $_;
|
||||||
} @$row);
|
} @$row);
|
||||||
|
|
||||||
@@ -7660,6 +7670,17 @@ Runs OPTIMIZE TABLE after finishing. See L<"--analyze"> for the option syntax
|
|||||||
and L<http://dev.mysql.com/doc/en/optimize-table.html> for details on OPTIMIZE
|
and L<http://dev.mysql.com/doc/en/optimize-table.html> for details on OPTIMIZE
|
||||||
TABLE.
|
TABLE.
|
||||||
|
|
||||||
|
=item --output-format
|
||||||
|
|
||||||
|
type: string
|
||||||
|
|
||||||
|
Used with L<"--file"> to specify the output format.
|
||||||
|
|
||||||
|
Valid formats are:
|
||||||
|
dump: MySQL dump format using tabs as field separator (default)
|
||||||
|
csv : Dump rows using ',' as separator and optionally enclosing fields by '"'.
|
||||||
|
This format is equivalent to FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'.
|
||||||
|
|
||||||
=item --password
|
=item --password
|
||||||
|
|
||||||
short form: -p; type: string
|
short form: -p; type: string
|
||||||
|
@@ -115,6 +115,25 @@ like(
|
|||||||
"..but an unknown charset fails"
|
"..but an unknown charset fails"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
local $SIG{__WARN__} = undef;
|
||||||
|
|
||||||
|
$sb->load_file('master', 't/pt-archiver/samples/table2.sql');
|
||||||
|
`rm -f archive.test.table_2`;
|
||||||
|
$output = output(
|
||||||
|
sub { pt_archiver::main(qw(--where 1=1 --output-format=csv), "--source", "D=test,t=table_2,F=$cnf", "--file", 'archive.%D.%t') },
|
||||||
|
);
|
||||||
|
$output = `cat archive.test.table_2`;
|
||||||
|
is($output, <<EOF
|
||||||
|
1, 2, 3, "4"
|
||||||
|
2, "\\N", 3, "4"
|
||||||
|
3, 2, 3, "\\\t"
|
||||||
|
4, 2, 3, "\\\n"
|
||||||
|
5, 2, 3, "Zapp \\"Brannigan"
|
||||||
|
EOF
|
||||||
|
, '--output-format=csv');
|
||||||
|
`rm -f archive.test.table_2`;
|
||||||
|
|
||||||
|
|
||||||
# #############################################################################
|
# #############################################################################
|
||||||
# Done.
|
# Done.
|
||||||
# #############################################################################
|
# #############################################################################
|
||||||
|
19
t/pt-archiver/samples/table2.sql
Normal file
19
t/pt-archiver/samples/table2.sql
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
CREATE SCHEMA IF NOT EXISTS test;
|
||||||
|
use test;
|
||||||
|
drop table if exists table_2;
|
||||||
|
|
||||||
|
create table table_2(
|
||||||
|
a int not null primary key,
|
||||||
|
b int,
|
||||||
|
c int not null,
|
||||||
|
d varchar(50),
|
||||||
|
key(b)
|
||||||
|
) engine=innodb;
|
||||||
|
|
||||||
|
insert into table_2 values
|
||||||
|
(1, 2, 3, 4),
|
||||||
|
(2, null, 3, 4),
|
||||||
|
(3, 2, 3, "\t"),
|
||||||
|
(4, 2, 3, "\n"),
|
||||||
|
(5, 2, 3, "Zapp \"Brannigan");
|
||||||
|
|
Reference in New Issue
Block a user