Bugzilla – Attachment 84603 Details for
Bug 21610
Koha::Object->store needs to handle incorrect values
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 21610: Fix integers and dates values at Koha::Object->store level
Bug-21610-Fix-integers-and-dates-values-at-KohaObj.patch (text/plain), 3.28 KB, created by
Tomás Cohen Arazi (tcohen)
on 2019-02-01 11:02:50 UTC
(
hide
)
Description:
Bug 21610: Fix integers and dates values at Koha::Object->store level
Filename:
MIME Type:
Creator:
Tomás Cohen Arazi (tcohen)
Created:
2019-02-01 11:02:50 UTC
Size:
3.28 KB
patch
obsolete
>From 811430147e00492c108aeb466ad6afc9f5e3e356 Mon Sep 17 00:00:00 2001 >From: Jonathan Druart <jonathan.druart@bugs.koha-community.org> >Date: Thu, 18 Oct 2018 18:55:44 -0300 >Subject: [PATCH] Bug 21610: Fix integers and dates values at > Koha::Object->store level > >With strict SQL modes, we are facing several issues when objects are stored. > >The following errors can be raised by DBIx::Class when an object does not have >a valid (as defined at DB level) value: >- Incorrect integer value: '' for column COLUMN >- Incorrect decimal value: '' for column COLUMN >- Column 'COLUMN' cannot be null >- Field 'COLUMN' doesn't have a default value >- Incorrect date value: '' for column 'COLUMN' > >We already handled some of them for Koha::Patron. As we do not want to provide such >changes for all Koha::* modules we are going to fix this at Koha::Object->store level >in order to provide a global fix. > >This is related to bug 21599 (for item types) and bug 21596 (for patrons) > >Test plan: >- Apply first patch, run the tests >=> Patch fail because of the previous error in Koha::ItemType->store, to make them pass >you can replace > $self->notforloan(0) unless $self->notforloan; >with > $self->notforloan(undef) unless $self->notforloan; >- Apply second patch, run the tests >=> They fail because of dates and/or integers values >- Apply third patch, run the tests >=> They now pass > >Deep code review wanted! > >Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com> >--- > Koha/Object.pm | 36 ++++++++++++++++++++++++++++++++++++ > 1 file changed, 36 insertions(+) > >diff --git a/Koha/Object.pm b/Koha/Object.pm >index 98867d29d0..65790eb411 100644 >--- a/Koha/Object.pm >+++ b/Koha/Object.pm >@@ -121,6 +121,31 @@ Returns: > sub store { > my ($self) = @_; > >+ my $columns_info = Koha::Database->new->schema->resultset( $self->_type ) >+ ->result_source->{_columns}; >+ >+ # Handle not null and default values for integers and dates >+ foreach my $col ( keys %{$columns_info} ) { >+ # Integers >+ if ( _numeric_column_type( $columns_info->{$col}->{data_type} ) ) { >+ # Has been passed but not a number, usually an empty string >+ if ( defined $self->$col and not looks_like_number( $self->$col ) ) { >+ if ( $columns_info->{$col}->{is_nullable} ) { >+ # If nullable, default to null >+ $self->$col(undef); >+ } else { >+ # If cannot be null, get the default value >+ # What if cannot be null and does not have a default value? Possible? >+ $self->$col($columns_info->{$col}->{default_value}); >+ } >+ } >+ } >+ elsif ( _date_or_datetime_column_type( $columns_info->{$col}->{data_type} ) ) { >+ # Set to null if an empty string (or == 0 but should not happen) >+ $self->$col(undef) unless $self->$col; >+ } >+ } >+ > try { > return $self->_result()->update_or_insert() ? $self : undef; > } >@@ -282,6 +307,17 @@ sub TO_JSON { > return $unblessed; > } > >+sub _date_or_datetime_column_type { >+ my ($column_type) = @_; >+ >+ my @dt_types = ( >+ 'timestamp', >+ 'date', >+ 'datetime' >+ ); >+ >+ return ( grep { $column_type eq $_ } @dt_types) ? 1 : 0; >+} > sub _datetime_column_type { > my ($column_type) = @_; > >-- >2.20.1
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 21610
:
80880
|
80881
|
80882
|
80906
|
80926
|
80927
|
81016
|
81017
|
81018
|
81019
|
81020
|
81021
|
84601
|
84602
|
84603
|
84604
|
84605
|
84606
|
84642
|
84643
|
84644
|
84645
|
84646
|
84647