Bugzilla – Attachment 54162 Details for
Bug 17080
Koha::Object->new should handle default values for NOT NULL columns
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 17080: Handle default values for NOT NULL columns from Koha::Object->new
Bug-17080-Handle-default-values-for-NOT-NULL-colum.patch (text/plain), 3.66 KB, created by
Jonathan Druart
on 2016-08-08 13:59:28 UTC
(
hide
)
Description:
Bug 17080: Handle default values for NOT NULL columns from Koha::Object->new
Filename:
MIME Type:
Creator:
Jonathan Druart
Created:
2016-08-08 13:59:28 UTC
Size:
3.66 KB
patch
obsolete
>From 6faea990d6e682430ae8e86214896e82eb2db6f5 Mon Sep 17 00:00:00 2001 >From: Jonathan Druart <jonathan.druart@bugs.koha-community.org> >Date: Mon, 8 Aug 2016 14:13:40 +0100 >Subject: [PATCH] Bug 17080: Handle default values for NOT NULL columns from > Koha::Object->new > >Recently we face the same issue on different modules after we moved them to the >Koha namespace using Koha::Object of using DBIx::Class directly. > >1/ Koha::Patron::Modification on bug 16960 comment 14 and 15 >2/ Koha::Patron::Category from bug 17069 >3/ C4::Members::AddMember (which does not use Koha::Object) on bug 16917 > >If a DB column is defined as NOT NULL and has a default value, the DBIx::Class >$rs->update_or_insert method won't use the default value if the column name >has been passed to the constructor. > >We do that almost everywhere as we retrieve the data from the HTML forms without >checking/cleaning them. > >There are several ways to fix that: >1/ Continue to fix them case by case (what we did for the recent issues) >2/ Try to fix them globally (existing ones and the next ones) > >This patch propose a global solution to avoid future issues of this kind. > >The idea is not to pass the undefined values which cannot be nullable to the >DBIx::Class constructor. >--- > Koha/Object.pm | 13 +++++++++++-- > t/db_dependent/Koha/Objects.t | 15 ++++++++++++++- > 2 files changed, 25 insertions(+), 3 deletions(-) > >diff --git a/Koha/Object.pm b/Koha/Object.pm >index 9d23fb4..50c82a1 100644 >--- a/Koha/Object.pm >+++ b/Koha/Object.pm >@@ -56,8 +56,17 @@ sub new { > my $self = {}; > > if ($attributes) { >- $self->{_result} = >- Koha::Database->new()->schema()->resultset( $class->_type() ) >+ my $schema = Koha::Database->new->schema; >+ >+ # Remove the arguments which exist, are not defined but NOT NULL to use the default value >+ my $columns_info = $schema->resultset( $class->_type )->result_source->columns_info; >+ for my $column_name ( keys %$attributes ) { >+ my $c_info = $columns_info->{$column_name}; >+ next if $c_info->{is_nullable}; >+ next if not exists $attributes->{$column_name} or defined $attributes->{$column_name}; >+ delete $attributes->{$column_name}; >+ } >+ $self->{_result} = $schema->resultset( $class->_type() ) > ->new($attributes); > } > >diff --git a/t/db_dependent/Koha/Objects.t b/t/db_dependent/Koha/Objects.t >index b92239a..00e6b9c 100644 >--- a/t/db_dependent/Koha/Objects.t >+++ b/t/db_dependent/Koha/Objects.t >@@ -19,9 +19,11 @@ > > use Modern::Perl; > >-use Test::More tests => 2; >+use Test::More tests => 3; > > use Koha::Authority::Types; >+use Koha::Patron::Category; >+use Koha::Patron::Categories; > use Koha::Patrons; > use Koha::Database; > >@@ -36,5 +38,16 @@ my @columns = Koha::Patrons->columns; > my $borrowernumber_exists = grep { /^borrowernumber$/ } @columns; > is( $borrowernumber_exists, 1, 'Koha::Objects->columns should return the table columns' ); > >+subtest 'new' => sub { >+ plan tests => 2; >+ my $a_cat_code = 'A_CAT_CODE'; >+ my $patron_category = Koha::Patron::Category->new( { categorycode => $a_cat_code } )->store; >+ is( Koha::Patron::Categories->find($a_cat_code)->category_type, 'A', 'Koha::Object->new should set the default value' ); >+ Koha::Patron::Categories->find($a_cat_code)->delete; >+ $patron_category = Koha::Patron::Category->new( { categorycode => $a_cat_code, category_type => undef } )->store; >+ is( Koha::Patron::Categories->find($a_cat_code)->category_type, 'A', 'Koha::Object->new should set the default value even if the argument exists but is not defined' ); >+ Koha::Patron::Categories->find($a_cat_code)->delete; >+}; >+ > $schema->storage->txn_rollback; > 1; >-- >2.8.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 17080
:
54162
|
54163
|
54164
|
54165
|
54166
|
54465
|
54466
|
54467
|
54468
|
54469
|
56464
|
56465
|
56466
|
56467
|
56468
|
56728