X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=sync.pl;h=113df7d93ba04ae3b0cc07ce0696016c8e145bab;hb=HEAD;hp=5996e203228ed0a33e8ba05a19d33ef65e954315;hpb=cc6c9e5fa838d8c093dd51bef25efc18422f7e79;p=wloh diff --git a/sync.pl b/sync.pl index 5996e20..113df7d 100755 --- a/sync.pl +++ b/sync.pl @@ -5,7 +5,17 @@ use warnings; no warnings qw(once); use Encode; use Time::HiRes; -require './config.pm'; +use lib qw( include ); +require 'config.pm'; + +sub copy_escape { + my $x = shift; + if (!defined($x)) { + return '\N'; + } + $x =~ s/([\\\r\n\t])/\\$1/g; + return $x; +} sub sync_table { my ($mdbh, $sdbh, $table) = @_; @@ -19,7 +29,7 @@ sub sync_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_putcopydata(join("\t", map { copy_escape($_) } (@$row)) . "\n"); } $sdbh->pg_putcopyend(); $elapsed = Time::HiRes::tv_interval($start); @@ -35,11 +45,27 @@ my $sdbh = DBI->connect($config::local_connstr, $config::local_username, $config or die "PostgreSQL connect: " . $DBI::errstr; $sdbh->{AutoCommit} = 0; $sdbh->{RaiseError} = 1; +$sdbh->do("SET client_encoding TO 'iso8859-1';"); +# For our own enjoyment. sync_table($mdbh, $sdbh, "Fotballbrukere"); sync_table($mdbh, $sdbh, "Fotballdeltagere"); sync_table($mdbh, $sdbh, "Fotballresultater"); +sync_table($mdbh, $sdbh, "Fotballresultater_2123"); sync_table($mdbh, $sdbh, "Fotballserier"); +# Just for backup. +sync_table($mdbh, $sdbh, 'Fotballjuks'); +sync_table($mdbh, $sdbh, 'FotballKaffeklubb'); +sync_table($mdbh, $sdbh, 'Fotballkommentarer'); +sync_table($mdbh, $sdbh, 'Fotballkommentartema'); +sync_table($mdbh, $sdbh, 'FotballLogin'); +sync_table($mdbh, $sdbh, 'Fotballspraak'); +sync_table($mdbh, $sdbh, 'Fotballtoppti'); +sync_table($mdbh, $sdbh, 'Fotballvariabler'); + +$sdbh->do('DELETE FROM last_sync'); +$sdbh->do('INSERT INTO last_sync VALUES ( CURRENT_TIMESTAMP );'); + $mdbh->rollback(); $sdbh->commit();