<commit_before>package NonameTV::DataStore::Updater;

use strict;

use Carp;
use NonameTV::Log qw/info progress error logdie/;

=head1 NAME

NonameTV::DataStore::Updater

=head1 DESCRIPTION

Update a batch

==head1 SYNOPSIS

To update a batch, i.e. modify some of the programs in a batch
without deleting all of them, do the following steps:

  StartBatchUpdate( $batch_id );
  DeleteProgramme( ... );
  AddProgramme( ... );
  EndBatchUpdate( $success, $message );

=head1 METHODS

=over 4

=cut

=item new

The constructor for the object. Called with a NonameTV::DataStore object
as a parameter. 

=cut

sub new
{
  my $class = ref( $_[0] ) || $_[0];
  
  my $self = { }; 
  bless $self, $class;
  
  $self->{ds} = $_[1];
  
  return $self;
}

sub DESTROY
{
  my $self = shift;
  
}

=item StartBatchUpdate

Called by an importer to signal the start of an update of a batch.
Takes a single parameter containing a string that uniquely identifies
a set of programmes.

Returns 1 on success and 0 on failure. Failure means that this batch does not
exist.

=cut

sub StartBatchUpdate
{
  my( $self, $batchname ) = @_;
  
  my $ds = $self->{ds};

#  print "SBU: $batchname\n";

  $ds->DoSql( "START TRANSACTION" );
  my $id = $ds->Lookup( 'batches', { name => $batchname }, 'id' );
  if( not defined( $id ) )
  {
    error( "No such batch $batchname" );
    return 0;
  }
  
  $ds->SetBatch( $id, $batchname );

  $self->{currbatch} = $id;
  $self->{currbatchname} = $batchname;
  $self->{batcherror} = 0;
  return 1;
}

=item EndBatchUpdate

Called by an importer to signal the end of a batch of updates.
Takes two parameters: 

An integer containing 1 if the batch was processed
successfully and 0 if the batch failed and the database should
be rolled back to the contents as they were before StartBatchUpdate was called.

A string containing a log-message to add to the batchrecord. If success==1,
then the log-message is appended to the field 'message'. If success==0, then
the log-message is appended to the field abort_message. The log-message 
can be undef.

=cut

sub EndBatchUpdate
{
  my( $self, $success, $log ) = @_;
  
#  print "EBU: $success\n";

  logdie( "You must call StartBatchUpdate before EndBatchUpdate" ) 
    unless exists $self->{currbatch};

  my $ds = $self->{ds};

  $log = "" if not defined $log;

  if( $success and not $self->{batcherror} )
  {
    $ds->Update( 'batches', { id => $self->{currbatch} }, 
                   { last_update => time(),
                     message => "concat( message, \"$log\" )" } );

    $ds->DoSql("Commit");
  }
  else
  {
    $ds->DoSql("Rollback");

    $ds->Update( 'batches', { id => $self->{currbatch} },
                 { abort_message => "concat( abort_message, \"$log\" )" } );

    error( $self->{currbatchname} . ": Rolling back changes" );
  }
<commit_msg>Fix bug in batch-log updates.<commit_after>package NonameTV::DataStore::Updater;

use strict;

use Carp;
use NonameTV::Log qw/info progress error logdie/;

=head1 NAME

NonameTV::DataStore::Updater

=head1 DESCRIPTION

Update a batch

==head1 SYNOPSIS

To update a batch, i.e. modify some of the programs in a batch
without deleting all of them, do the following steps:

  StartBatchUpdate( $batch_id );
  DeleteProgramme( ... );
  AddProgramme( ... );
  EndBatchUpdate( $success, $message );

=head1 METHODS

=over 4

=cut

=item new

The constructor for the object. Called with a NonameTV::DataStore object
as a parameter. 

=cut

sub new
{
  my $class = ref( $_[0] ) || $_[0];
  
  my $self = { }; 
  bless $self, $class;
  
  $self->{ds} = $_[1];
  
  return $self;
}

sub DESTROY
{
  my $self = shift;
  
}

=item StartBatchUpdate

Called by an importer to signal the start of an update of a batch.
Takes a single parameter containing a string that uniquely identifies
a set of programmes.

Returns 1 on success and 0 on failure. Failure means that this batch does not
exist.

=cut

sub StartBatchUpdate
{
  my( $self, $batchname ) = @_;
  
  my $ds = $self->{ds};

#  print "SBU: $batchname\n";

  $ds->DoSql( "START TRANSACTION" );
  my $data = $ds->Lookup( 'batches', { name => $batchname } );
  if( not defined( $data->{id} ) )
  {
    error( "No such batch $batchname" );
    return 0;
  }

  my $id = $data->{id};

  $ds->SetBatch( $id, $batchname );

  $self->{currbatch} = $id;
  $self->{currbatchname} = $batchname;
  $self->{batcherror} = 0;
  $self->{oldmessage} = $data->{message};
  $self->{oldabortmessage} = $data->{abort_message};

  return 1;
}

=item EndBatchUpdate

Called by an importer to signal the end of a batch of updates.
Takes two parameters: 

An integer containing 1 if the batch was processed
successfully and 0 if the batch failed and the database should
be rolled back to the contents as they were before StartBatchUpdate was called.

A string containing a log-message to add to the batchrecord. If success==1,
then the log-message is appended to the field 'message'. If success==0, then
the log-message is appended to the field abort_message. The log-message 
can be undef.

=cut

sub EndBatchUpdate
{
  my( $self, $success, $log ) = @_;
  
#  print "EBU: $success\n";

  logdie( "You must call StartBatchUpdate before EndBatchUpdate" ) 
    unless exists $self->{currbatch};

  my $ds = $self->{ds};

  $log = "" if not defined $log;

  if( $success and not $self->{batcherror} )
  {
    $ds->Update( 'batches', { id => $self->{currbatch} }, 
                   { last_update => time(),
                     message => $self->{oldmessage} .  "\n$log" } );

    $ds->DoSql("Commit");
  }
  else
  {
    $ds->DoSql("Rollback");

    $ds->Update( 'batches', { id => $self->{currbatch} },
                 { abort_message => $self->{oldabortmessage} . "\n$log" } );

    error( $self->{currbatchname} . ": Rolling back changes" );
  }

{'fraction_non_alphanumeric': 0.13674771512329711, 'fraction_numerical': 0.005863079841351957, 'mean_word_length': 3.2059463379260333, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7568220', 'n_tokens_mistral': 2048, 'n_tokens_neox': 1989, 'n_words': 668}