]> git.sesse.net Git - skvidarsync/blobdiff - bin/sync.pl
Make do with only one commit per sync.
[skvidarsync] / bin / sync.pl
index 0aea7b908a1f57d2e20441f9d66a2118fe93be62..8ca281d6c195591afc22e62b7c0043a3b7b675d0 100644 (file)
@@ -235,7 +235,6 @@ sub get_group_assignments {
 sub update_assignment_db {
        my ($dbh, $channel, $ts, $assignments) = @_;
 
-       local $dbh->{AutoCommit} = 0;
        my %db_assignments = ();
        my $q = $dbh->prepare('SELECT name,group_name FROM current_group_membership_history WHERE channel=? AND ts=?');
        $q->execute($channel, $ts);
@@ -256,7 +255,6 @@ sub update_assignment_db {
                        $q->execute($channel, $ts, $name, undef);
                }
        }
-       $dbh->commit;
 }
 
 sub get_spreadsheet_with_title {
@@ -483,6 +481,7 @@ sub db_connect {
        if (!defined($dbh)) {
                return undef;
        }
+       $dbh->{AutoCommit} = 0;
        $dbh->do('LISTEN skvupdate') or return undef;
        return $dbh;
 }
@@ -494,6 +493,9 @@ sub run {
        @log = ();
        skv_log("Siste sync startet: " . POSIX::ctime(time));
 
+       # For the logic on the “applied” table below.
+       $dbh->do('SET TRANSACTION ISOLATION LEVEL SERIALIZABLE');
+
        my $token = get_oauth_bearer_token($dbh, $ua);
 
        # Find the newest message, what it is linked to, and what was the one before it (for group diffing).
@@ -564,7 +566,9 @@ sub run {
        my %assignments = get_group_assignments($main_sheet_json);
        update_assignment_db($dbh, $config::invitation_channel, $invitation_ts, \%assignments);
 
+       $start = [Time::HiRes::gettimeofday];
        my %seen_names = find_where_each_name_is($main_sheet_json);
+       log_timing($start, "Making sort key reverse mapping");
 
        # Find duplicates.
        for my $name (sort keys %seen_names) {
@@ -718,8 +722,6 @@ sub run {
 
        # Find the list of names we already marked yellow.
        my %have_colors = ();
-       $dbh->{AutoCommit} = 0;
-       $dbh->do('SET TRANSACTION ISOLATION LEVEL SERIALIZABLE');
        $q = $dbh->prepare('SELECT name,color FROM applied WHERE channel=? AND ts=?');
        $q->execute($config::invitation_channel, $invitation_ts);
        while (my $ref = $q->fetchrow_hashref) {
@@ -827,9 +829,7 @@ if ($#ARGV >= 0 && $ARGV[0] eq '--daemon') {
                }
                if (scalar @ready > 0) {  
                        eval {
-                               $dbh->{AutoCommit} = 1;
                                run($dbh, $ua);
-                               $dbh->commit;
                        };
                        if ($@) {
                                warn "Died with: $@";
@@ -839,9 +839,7 @@ if ($#ARGV >= 0 && $ARGV[0] eq '--daemon') {
        }
 } elsif ($#ARGV >= 0 && $ARGV[0] eq '--benchmark') {
        for my $i (0..9) {
-               $dbh->{AutoCommit} = 1;
                run($dbh, $ua);
-               $dbh->commit;
        }
 } else {
        run($dbh, $ua);