]> git.proxmox.com Git - pmg-api.git/commitdiff
PMG/DBTools.pm: new helper copy_table()
authorDietmar Maurer <dietmar@proxmox.com>
Tue, 18 Apr 2017 09:37:42 +0000 (11:37 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Tue, 18 Apr 2017 09:39:15 +0000 (11:39 +0200)
PMG/DBTools.pm

index 08be5b04385efba963a92eaf7885346929431371..ef6cce607d58359104531833a82c825b4cab839f 100644 (file)
@@ -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) = @_;