From 5b2893ac76f8f5c73c3a77070228a36e1ff443b4 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 --- Koha/Patron.pm | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 3ad10a8efc..1313d5117d 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -1364,26 +1364,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.11.0