From 64cef18acfd222cd648fe443821a26c669ee6348 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Mon, 17 Dec 2018 09:15:29 +0100 Subject: [PATCH] Bug 21336: (follow-up) Handle strict SQL mode in _anonymize_column Content-Type: text/plain; charset=utf-8 When a field is not nullable we should pass empty string, zero or today to a char, numeric or date column. The mandatory parameter does not refer to a database constraint but a Koha preference. Only for strings we generate a random value. Test plan: Enable strict_sql_modes. Run t/db_dependent/Koha/Patrons.t again. Signed-off-by: Marcel de Rooy --- Koha/Patron.pm | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index c2ba1be..f6b5fe2 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -1193,26 +1193,29 @@ sub anonymize { @columns = grep { !/borrowernumber|branchcode|categorycode|^date|password|flags|updated_on|lastseen|lang|login_attempts|flgAnonymized/ } @columns; push @columns, 'dateofbirth'; # add this date back in foreach my $col (@columns) { - if( $mandatory->{lc $col} ) { - my $str = $self->_anonymize_column($col); - $self->$col($str); - } else { - $self->$col(undef); - } + $self->_anonymize_column($col, $mandatory->{lc $col} ); } $self->flgAnonymized(1)->store; } sub _anonymize_column { - my ( $self, $col ) = @_; - my $type = $self->_result->result_source->column_info($col)->{data_type}; + my ( $self, $col, $mandatory ) = @_; + my $col_info = $self->_result->result_source->column_info($col); + my $type = $col_info->{data_type}; + my $nullable = $col_info->{is_nullable}; + my $val; if( $type =~ /char|text/ ) { - return Koha::Token->new->generate({ pattern => '\w{10}' }); + $val = $mandatory + ? Koha::Token->new->generate({ pattern => '\w{10}' }) + : $nullable + ? undef + : q{}; } elsif( $type =~ /integer|int$|float|dec|double/ ) { - return 0; + $val = $nullable ? undef : 0; } elsif( $type =~ /date|time/ ) { - return dt_from_string; + $val = $nullable ? undef : dt_from_string; } + $self->$col($val); } =head3 can_see_patron_infos -- 2.1.4