-$dbh->do('TRUNCATE ratings');
-$dbh->do('COPY ratings ( id, rating, rating_stddev ) FROM STDIN');
-$dbh->pg_putcopydata(join("\n", @ratings));
-$dbh->pg_putcopyend();
+my $dbh = DBI->connect($config::local_connstr, $config::local_username, $config::local_password)
+ or die "connect: " . $DBI::errstr;
+$dbh->{AutoCommit} = 0;
+$dbh->{RaiseError} = 1;
+
+$dbh->do('SET client_min_messages TO WARNING');
+
+my $last_season = find_last_season($dbh);
+my @games = ();
+my %ids = ();
+fetch_games($dbh, $last_season, \@games, \%ids);
+my $tmpnam = output_to_file(\@games, \%ids);
+
+my @ratings = ();
+my @covariances = ();
+my @aux_params = ();
+train_model($tmpnam, \@ratings, \@covariances, \@aux_params);
+unlink($tmpnam);