Bug 21336: (follow-up) Handle strict SQL mode in _anonymize_column
authorMarcel de Rooy <m.de.rooy@rijksmuseum.nl>
Mon, 17 Dec 2018 08:15:29 +0000 (09:15 +0100)
committerNick Clemens <nick@bywatersolutions.com>
Wed, 17 Apr 2019 12:25:24 +0000 (12:25 +0000)
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 <m.de.rooy@rijksmuseum.nl>

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

Koha/Patron.pm

index eedac6b..c332628 100644 (file)
@@ -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