From 8761a994808c7b0ad922585fef18a34923f45213 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 8 Dec 2017 10:50:51 -0300 Subject: [PATCH] Bug 19096: Use raw SQL queries instead for the update DB entry I strongly thing we must not use C4 or Koha subroutine in the update DB process, only sql queries. Especially not DBIC Schema files as they will change. For instance the update DB 17.12.00.00X is using a specific Koha::Schema::RS::MSS representing the current schema, but in X months/years, this schema will change and the ->search we used may failed. --- Koha/Util/Dbrev.pm | 84 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 55 insertions(+), 29 deletions(-) diff --git a/Koha/Util/Dbrev.pm b/Koha/Util/Dbrev.pm index 7346558aa5..db5ada15cd 100644 --- a/Koha/Util/Dbrev.pm +++ b/Koha/Util/Dbrev.pm @@ -22,9 +22,8 @@ package Koha::Util::Dbrev; use Modern::Perl; -use Koha::BiblioFrameworks; +use C4::Context; use Koha::Caches; -use Koha::MarcSubfieldStructures; =head1 NAME @@ -54,19 +53,40 @@ The routines in this modules are called in OO style as class methods. sub sync_kohafield { my ( $class ) = @_; + my $dbh = C4::Context->dbh; + # Clear the destination frameworks first - Koha::MarcSubfieldStructures->search({ frameworkcode => { '>', q{} }, kohafield => { '>', q{} } })->update({ kohafield => undef }); + $dbh->do(q| + UPDATE marc_subfield_structure + SET kohafield = NULL + WHERE frameworkcode > '' + AND Kohafield > '' + |); # Now copy from Default - my $rs = Koha::MarcSubfieldStructures->search({ frameworkcode => q{}, kohafield => { '>', q{} } }); - while( my $rec = $rs->next ) { - Koha::MarcSubfieldStructures->search({ frameworkcode => { '>', q{} }, tagfield => $rec->tagfield, tagsubfield => $rec->tagsubfield })->update({ kohafield => $rec->kohafield }); + my $msss = $dbh->selectall_arrayref(q| + SELECT kohafield, tagfield, tagsubfield + FROM marc_subfield_structure + WHERE frameworkcode = '' + AND kohafield > '' + |, { Slice => {} }); + my $sth = $dbh->prepare(q| + UPDATE marc_subfield_structure + SET kohafield = ? + WHERE frameworkcode > '' + AND tagfield = ? + AND tagsubfield = ? + |); + for my $mss ( @$msss ) { + $sth->execute( $mss->{kohafield}, $mss->{tagfield}, $mss->{tagsubfield} ); } + my @frameworkcodes = $dbh->selectall_arrayref(q| # Clear the cache - my @fw = Koha::BiblioFrameworks->search({ frameworkcode => { '>', q{} } })->get_column('frameworkcode'); - foreach( @fw ) { - Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-$_" ); + SELECT frameworkcode FROM biblio_framework WHERE frameworkcode > '' + |); + for my $frameworkcode ( @frameworkcodes ) { + Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-$frameworkcode" ); } Koha::Caches->get_instance->clear_from_cache("default_value_for_mod_marc-"); } @@ -84,29 +104,35 @@ sub sync_kohafield { =cut sub get_kohafield_exceptions { - my ( $class, $params ) = @_; - - # Too bad that DBIx is not very helpful here - my $default = Koha::MarcSubfieldStructures->search({ - frameworkcode => q{}, - kohafield => { '>' => '' }, - }); - my $other = Koha::MarcSubfieldStructures->search({ - frameworkcode => { '!=' => q{} }, - }, { order_by => { -asc => [ qw/tagfield tagsubfield/ ] } }); - - # Compare - my @return; - while( my $rec = $other->next ) { - my @tags = ( $rec->tagfield, $rec->tagsubfield ); - my $defa = $default->find( q{}, @tags ); - if( $rec->kohafield ) { - push @return, [ $rec->frameworkcode, @tags, $rec->kohafield ] if !$defa || !$defa->kohafield || $defa->kohafield ne $rec->kohafield; + my ( $class ) = @_; + + my $dbh = C4::Context->dbh; + my $msss = $dbh->selectall_arrayref(q| + SELECT kohafield, tagfield, tagsubfield + FROM marc_subfield_structure + WHERE frameworkcode != '' + |, { Slice => {} }); + + + my $sth = $dbh->prepare(q| + SELECT kohafield + FROM marc_subfield_structure + WHERE frameworkcode = '' + AND tagfield = ? + AND tagsubfield = ? + |); + + my @exceptions; + for my $mss ( @$msss ) { + $sth->execute($mss->{tagfield}, $mss->{tagsubfield} ); + my ( $default_kohafield ) = $sth->fetchrow_array(); + if( $mss->{kohafield} ) { + push @exceptions, [ $mss->{frameworkcode}, $mss->{tagfield}, $mss->{tagsubfield}, $mss->{kohafield} ] if not $default_kohafield or $default_kohafield ne $mss->{kohafield}; } else { - push @return, [ $rec->frameworkcode, @tags, q{} ] if $defa && $defa->kohafield; + push @exceptions, [ $mss->{frameworkcode}, $mss->{tagfield}, $mss->{tagsubfield}, q{} ] if $default_kohafield; } } - return \@return; + return \@exceptions; } =head1 AUTHOR -- 2.11.0