]> git.sesse.net Git - wloh/commitdiff
Add a script to sync all data from the master WLoH database.
authorSteinar H. Gunderson <Steinar H. Gunderson sesse@debian.org>
Sat, 17 Mar 2012 12:09:30 +0000 (13:09 +0100)
committerSteinar H. Gunderson <Steinar H. Gunderson sesse@debian.org>
Sat, 17 Mar 2012 12:09:30 +0000 (13:09 +0100)
sync.pl [new file with mode: 0755]

diff --git a/sync.pl b/sync.pl
new file mode 100755 (executable)
index 0000000..5996e20
--- /dev/null
+++ b/sync.pl
@@ -0,0 +1,45 @@
+#! /usr/bin/perl
+use DBI;
+use strict;
+use warnings;
+no warnings qw(once);
+use Encode;
+use Time::HiRes;
+require './config.pm';
+
+sub sync_table {
+       my ($mdbh, $sdbh, $table) = @_;
+       my $start = [ Time::HiRes::gettimeofday ];
+       my $data = $mdbh->selectall_arrayref("SELECT * FROM $table");
+       my $elapsed = Time::HiRes::tv_interval($start);
+       #printf "MySQL $table time used: $elapsed\n";
+
+       $start = [ Time::HiRes::gettimeofday ];
+       $sdbh->do("DELETE FROM $table");
+       $sdbh->do("COPY $table FROM STDIN");
+       
+       for my $row (@$data) {
+               $sdbh->pg_putcopydata(join("\t", map { Encode::decode('iso8859-1', $_ // '\N') } (@$row)) . "\n");
+       }
+       $sdbh->pg_putcopyend();
+       $elapsed = Time::HiRes::tv_interval($start);
+       #printf "PostgreSQL $table time used: $elapsed\n";
+}
+
+my $mdbh = DBI->connect($config::master_connstr, $config::master_username, $config::master_password)
+       or die "PostgreSQL connect: " . $DBI::errstr;
+$mdbh->{AutoCommit} = 0;
+$mdbh->{RaiseError} = 1;
+
+my $sdbh = DBI->connect($config::local_connstr, $config::local_username, $config::local_password)
+       or die "PostgreSQL connect: " . $DBI::errstr;
+$sdbh->{AutoCommit} = 0;
+$sdbh->{RaiseError} = 1;
+
+sync_table($mdbh, $sdbh, "Fotballbrukere");
+sync_table($mdbh, $sdbh, "Fotballdeltagere");
+sync_table($mdbh, $sdbh, "Fotballresultater");
+sync_table($mdbh, $sdbh, "Fotballserier");
+
+$mdbh->rollback();
+$sdbh->commit();