From da686811a288d5c19d20d1ff825264070e23b119 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Tue, 18 Apr 2017 11:37:42 +0200 Subject: [PATCH] PMG/DBTools.pm: new helper copy_table() --- PMG/DBTools.pm | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/PMG/DBTools.pm b/PMG/DBTools.pm index 08be5b0..ef6cce6 100644 --- a/PMG/DBTools.pm +++ b/PMG/DBTools.pm @@ -1114,6 +1114,43 @@ sub init_masterdb { die $err if $err; } +sub copy_table { + my ($ldb, $rdb, $table) = @_; + + $table = lc($table); + + my $sth = $ldb->column_info(undef, undef, $table, undef); + my $attrs = $sth->fetchall_arrayref({}); + + my @col_arr; + foreach my $ref (@$attrs) { + push @col_arr, $ref->{COLUMN_NAME}; + } + + $sth->finish(); + + my $cols = join(', ', @col_arr); + $cols || die "unable to fetch column definitions of table '$table' : ERROR"; + + $rdb->do("COPY $table ($cols) TO STDOUT"); + + my $data = ''; + + eval { + $ldb->do("COPY $table ($cols) FROM stdin"); + + while ($rdb->pg_getcopydata($data) >= 0) { + $ldb->pg_putcopydata($data); + } + + $ldb->pg_putcopyend(); + }; + if (my $err = $@) { + $ldb->pg_putcopyend(); + die $err; + } +} + sub update_master_clusterinfo { my ($clientcid) = @_; -- 2.39.5