]> git.sesse.net Git - wloh/blob - sync.pl
Remove password that leaked out into the git repository. (It has also been changed...
[wloh] / sync.pl
1 #! /usr/bin/perl
2 use DBI;
3 use strict;
4 use warnings;
5 no warnings qw(once);
6 use Encode;
7 use Time::HiRes;
8 use lib qw( include );
9 require 'config.pm';
10
11 sub copy_escape {
12         my $x = shift;
13         if (!defined($x)) {
14                 return '\N';
15         }
16         $x =~ s/([\\\r\n\t])/\\$1/g;
17         return $x;
18 }
19
20 sub sync_table {
21         my ($mdbh, $sdbh, $table) = @_;
22         my $start = [ Time::HiRes::gettimeofday ];
23         my $data = $mdbh->selectall_arrayref("SELECT * FROM $table");
24         my $elapsed = Time::HiRes::tv_interval($start);
25         #printf "MySQL $table time used: $elapsed\n";
26
27         $start = [ Time::HiRes::gettimeofday ];
28         $sdbh->do("DELETE FROM $table");
29         $sdbh->do("COPY $table FROM STDIN");
30         
31         for my $row (@$data) {
32                 $sdbh->pg_putcopydata(join("\t", map { copy_escape($_) } (@$row)) . "\n");
33         }
34         $sdbh->pg_putcopyend();
35         $elapsed = Time::HiRes::tv_interval($start);
36         #printf "PostgreSQL $table time used: $elapsed\n";
37 }
38
39 my $mdbh = DBI->connect($config::master_connstr, $config::master_username, $config::master_password)
40         or die "PostgreSQL connect: " . $DBI::errstr;
41 $mdbh->{AutoCommit} = 0;
42 $mdbh->{RaiseError} = 1;
43
44 my $sdbh = DBI->connect($config::local_connstr, $config::local_username, $config::local_password)
45         or die "PostgreSQL connect: " . $DBI::errstr;
46 $sdbh->{AutoCommit} = 0;
47 $sdbh->{RaiseError} = 1;
48 $sdbh->do("SET client_encoding TO 'iso8859-1';");
49
50 # For our own enjoyment.
51 sync_table($mdbh, $sdbh, "Fotballbrukere");
52 sync_table($mdbh, $sdbh, "Fotballdeltagere");
53 sync_table($mdbh, $sdbh, "Fotballresultater");
54 sync_table($mdbh, $sdbh, "Fotballresultater_2123");
55 sync_table($mdbh, $sdbh, "Fotballserier");
56
57 # Just for backup.
58 sync_table($mdbh, $sdbh, 'Fotballjuks');
59 sync_table($mdbh, $sdbh, 'FotballKaffeklubb');
60 sync_table($mdbh, $sdbh, 'Fotballkommentarer');
61 sync_table($mdbh, $sdbh, 'Fotballkommentartema');
62 sync_table($mdbh, $sdbh, 'FotballLogin');
63 sync_table($mdbh, $sdbh, 'Fotballspraak');
64 sync_table($mdbh, $sdbh, 'Fotballtoppti');
65 sync_table($mdbh, $sdbh, 'Fotballvariabler');
66
67 $sdbh->do('DELETE FROM last_sync');
68 $sdbh->do('INSERT INTO last_sync VALUES ( CURRENT_TIMESTAMP );');
69
70 $mdbh->rollback();
71 $sdbh->commit();