]> git.sesse.net Git - wloh/blob - sync.pl
Fix include paths for static scripts.
[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 = Encode::decode('iso8859-1', $x);
17         $x =~ s/\\/\\\\/g;
18         $x =~ s/\r/\\r/g;
19         $x =~ s/\n/\\n/g;
20         $x =~ s/\t/\\t/g;
21         return $x;
22 }
23
24 sub sync_table {
25         my ($mdbh, $sdbh, $table) = @_;
26         my $start = [ Time::HiRes::gettimeofday ];
27         my $data = $mdbh->selectall_arrayref("SELECT * FROM $table");
28         my $elapsed = Time::HiRes::tv_interval($start);
29         #printf "MySQL $table time used: $elapsed\n";
30
31         $start = [ Time::HiRes::gettimeofday ];
32         $sdbh->do("DELETE FROM $table");
33         $sdbh->do("COPY $table FROM STDIN");
34         
35         for my $row (@$data) {
36                 $sdbh->pg_putcopydata(join("\t", map { copy_escape($_) } (@$row)) . "\n");
37         }
38         $sdbh->pg_putcopyend();
39         $elapsed = Time::HiRes::tv_interval($start);
40         #printf "PostgreSQL $table time used: $elapsed\n";
41 }
42
43 my $mdbh = DBI->connect($config::master_connstr, $config::master_username, $config::master_password)
44         or die "PostgreSQL connect: " . $DBI::errstr;
45 $mdbh->{AutoCommit} = 0;
46 $mdbh->{RaiseError} = 1;
47
48 my $sdbh = DBI->connect($config::local_connstr, $config::local_username, $config::local_password)
49         or die "PostgreSQL connect: " . $DBI::errstr;
50 $sdbh->{AutoCommit} = 0;
51 $sdbh->{RaiseError} = 1;
52
53 # For our own enjoyment.
54 sync_table($mdbh, $sdbh, "Fotballbrukere");
55 sync_table($mdbh, $sdbh, "Fotballdeltagere");
56 sync_table($mdbh, $sdbh, "Fotballresultater");
57 sync_table($mdbh, $sdbh, "Fotballresultater_2123");
58 sync_table($mdbh, $sdbh, "Fotballserier");
59
60 # Just for backup.
61 sync_table($mdbh, $sdbh, 'Fotballjuks');
62 sync_table($mdbh, $sdbh, 'FotballKaffeklubb');
63 sync_table($mdbh, $sdbh, 'Fotballkommentarer');
64 sync_table($mdbh, $sdbh, 'Fotballkommentartema');
65 sync_table($mdbh, $sdbh, 'FotballLogin');
66 sync_table($mdbh, $sdbh, 'Fotballspraak');
67 sync_table($mdbh, $sdbh, 'Fotballtoppti');
68 sync_table($mdbh, $sdbh, 'Fotballvariabler');
69
70 $sdbh->do('DELETE FROM last_sync');
71 $sdbh->do('INSERT INTO last_sync VALUES ( CURRENT_TIMESTAMP );');
72
73 $mdbh->rollback();
74 $sdbh->commit();