From af84af7cb7cd7ade4054ad155963db4d1ecd6272 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 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 Signed-off-by: Josef Moravec Signed-off-by: Martin Renvoize Signed-off-by: Nick Clemens --- Koha/Patron.pm | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index eedac6b07f..c3326285b9 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -1365,26 +1365,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); } =head2 Internal methods -- 2.39.5