@@ -, +, @@ --- Koha/Util/Dbrev.pm | 145 ------------------------ installer/data/mysql/atomicupdate/bug19096.perl | 96 ++++++++++++++-- t/db_dependent/Koha/MarcSubfieldStructures.t | 54 +-------- 3 files changed, 86 insertions(+), 209 deletions(-) delete mode 100644 Koha/Util/Dbrev.pm --- a/Koha/Util/Dbrev.pm +++ a/Koha/Util/Dbrev.pm @@ -1,145 +0,0 @@ -package Koha::Util::Dbrev; - -# Module contains subroutines used in database revisions (call in OO style) -# -# Copyright 2017 Koha Development Team -# -# This file is part of Koha. -# -# Koha is free software; you can redistribute it and/or modify it under the -# terms of the GNU General Public License as published by the Free Software -# Foundation; either version 3 of the License, or (at your option) any later -# version. -# -# Koha is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR -# A PARTICULAR PURPOSE. See the GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License along -# with Koha; if not, write to the Free Software Foundation, Inc., -# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -# - -use Modern::Perl; - -use C4::Context; -use Koha::Caches; - -=head1 NAME - -Koha::Util::Dbrev - utility class with routines used in db revisions - -=head1 SYNOPSIS - - use Koha::Util::Dbrev; - - Koha::Util::Dbrev->sync_kohafield; - -=head1 DESCRIPTION - -The routines in this modules are called in OO style as class methods. - -=head1 METHODS - -=head2 sync_kohafield - - Synchronizes the used kohafields in the Default framework to all other - frameworks. - Used in the dbrev for bug 19096 (Koha to MARC mappings (Part 2): Make Default authoritative. Tested in t/db_dependent/Koha/MarcSubfieldStructures.t. - If needed somehow later on, we should move it to Koha::MarcSubfieldStructures - -=cut - -sub sync_kohafield { - my ( $class ) = @_; - - my $dbh = C4::Context->dbh; - - # Clear the destination frameworks first - $dbh->do(q| - UPDATE marc_subfield_structure - SET kohafield = NULL - WHERE frameworkcode > '' - AND Kohafield > '' - |); - - # Now copy from Default - 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 - 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-"); -} - -=head2 get_kohafield_exceptions - - Get all kohafield exceptions (deviations from the Default framework) - - Returns an arrayref of [ framework, field, subfield, kohafield ] - where kohafield is the original (deviating) value. - - Used in the dbrev for bug 19096 (Koha to MARC mappings (Part 2): Make Default authoritative. Tested in t/db_dependent/Koha/MarcSubfieldStructures.t. - If needed somehow later on, we should move it to Koha::MarcSubfieldStructures - -=cut - -sub get_kohafield_exceptions { - 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 @exceptions, [ $mss->{frameworkcode}, $mss->{tagfield}, $mss->{tagsubfield}, q{} ] if $default_kohafield; - } - } - return \@exceptions; -} - -=head1 AUTHOR - - Marcel de Rooy, Rijksmuseum Amsterdam, The Netherlands - Koha Development Team - -=cut - -1; --- a/installer/data/mysql/atomicupdate/bug19096.perl +++ a/installer/data/mysql/atomicupdate/bug19096.perl @@ -1,20 +1,94 @@ $DBversion = 'XXX'; # will be replaced by the RM if( CheckVersion( $DBversion ) ) { - require Koha::Util::Dbrev; - - my $diff = Koha::Util::Dbrev->get_kohafield_exceptions; - if( @$diff ) { - print "WARNING: The Default framework is now considered as authoritative for Koha to MARC mappings. We have found that your additional frameworks contained ".@$diff." mapping(s) that deviate from the standard mappings. Please look at the following list and consider if you need to add them again in Default (possibly as a second mapping).\n"; - foreach( @$diff ) { - print "Field ". $_->[1].$_->[2]. " in framework ". $_->[0]. ": "; - if( $_->[3] ) { - print "Mapping to ". $_->[3]. " has been adjusted.\n"; - } else { + + my $dbh = C4::Context->dbh; + + my $msss = $dbh->selectall_arrayref(q| + SELECT kohafield, tagfield, tagsubfield, frameworkcode + 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, { frameworkcode => $mss->{frameworkcode}, tagfield => $mss->{tagfield}, tagsubfield => $mss->{tagsubfield}, kohafield => $mss->{kohafield} } if not $default_kohafield or $default_kohafield ne $mss->{kohafield}; + } else { + push @exceptions, { frameworkcode => $mss->{frameworkcode}, tagfield => $mss->{tagfield}, tagsubfield => $mss->{tagsubfield}, kohafield => q{} } if $default_kohafield; + } + } + + if (@exceptions) { + print +"WARNING: The Default framework is now considered as authoritative for Koha to MARC mappings. We have found that your additional frameworks contained " + . scalar(@exceptions) + . " mapping(s) that deviate from the standard mappings. Please look at the following list and consider if you need to add them again in Default (possibly as a second mapping).\n"; + for my $exception (@exceptions) { + print "Field " + . $exception->{tagfield} . '.' + . $exception->{tagsubfield} + . " in framework " + . $exception->{frameworkcode} . ': '; + if ( $exception->{kohafield} ) { + print "Mapping to " + . $exception->{kohafield} + . " has been adjusted.\n"; + } + else { print "Mapping has been reset.\n"; } } - Koha::Util::Dbrev->sync_kohafield; + + # Sync kohafield + + # Clear the destination frameworks first + $dbh->do(q| + UPDATE marc_subfield_structure + SET kohafield = NULL + WHERE frameworkcode > '' + AND Kohafield > '' + |); + + # Now copy from Default + 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} ); + } + + # Clear the cache + my @frameworkcodes = $dbh->selectall_arrayref(q| + 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-"); } + SetVersion( $DBversion ); print "Upgrade to $DBversion done (Bug 19096 - Make Default authoritative for Koha to MARC mappings)\n"; } --- a/t/db_dependent/Koha/MarcSubfieldStructures.t +++ a/t/db_dependent/Koha/MarcSubfieldStructures.t @@ -19,12 +19,10 @@ use Modern::Perl; -use Test::More tests => 2; +use Test::More tests => 1; -use Koha::MarcSubfieldStructure; use Koha::MarcSubfieldStructures; use Koha::Database; -use Koha::Util::Dbrev; use t::lib::TestBuilder; @@ -57,54 +55,4 @@ subtest 'Trivial tests' => sub { is( Koha::MarcSubfieldStructures->search->count, $nb_of_fields + 1, 'Delete should have deleted the field' ); }; -subtest 'get_kohafield_exceptions / sync_kohafield' => sub { - plan tests => 14; - - # start from scratch again, add a new framework - Koha::MarcSubfieldStructures->delete; - my $builder = t::lib::TestBuilder->new; - my $fwc = $builder->build({ source => 'BiblioFramework' })->{frameworkcode}; - - # 100a mapped in Default, unmapped in other - Koha::MarcSubfieldStructure->new({ frameworkcode => q{}, tagfield => '100', tagsubfield => 'a', kohafield => 'biblio.author' })->store; - Koha::MarcSubfieldStructure->new({ frameworkcode => $fwc, tagfield => '100', tagsubfield => 'a', kohafield => '' })->store; - my $res = Koha::Util::Dbrev->get_kohafield_exceptions; - is( @$res, 1, 'Found one exception' ); - is( $res->[0]->[1] . $res->[0]->[2], '100a', '100a was reported' ); - Koha::Util::Dbrev->sync_kohafield; - $res = Koha::Util::Dbrev->get_kohafield_exceptions; - is( @$res, 0, 'Found no exceptions after syncing' ); - is( Koha::MarcSubfieldStructures->find($fwc, '100', 'a')->kohafield, - 'biblio.author', '100a in added framework adjusted' ); - - # 245a unmapped in Default, mapped in other - # 300a not in Default, mapped in other - Koha::MarcSubfieldStructure->new({ frameworkcode => q{}, tagfield => '245', tagsubfield => 'a', kohafield => q{} })->store; - Koha::MarcSubfieldStructure->new({ frameworkcode => $fwc, tagfield => '245', tagsubfield => 'a', kohafield => 'biblio.title' })->store; - Koha::MarcSubfieldStructure->new({ frameworkcode => $fwc, tagfield => '300', tagsubfield => 'a', kohafield => 'biblioitems.pages' })->store; - $res = Koha::Util::Dbrev->get_kohafield_exceptions; - is( @$res, 2, 'Found two exceptions' ); - is( $res->[0]->[1] . $res->[0]->[2], '245a', '245a was reported' ); - is( $res->[1]->[1] . $res->[1]->[2], '300a', '300a was reported' ); - Koha::Util::Dbrev->sync_kohafield; - $res = Koha::Util::Dbrev->get_kohafield_exceptions; - is( @$res, 0, 'Found no exceptions after syncing again' ); - is( Koha::MarcSubfieldStructures->find($fwc, '245', 'a')->kohafield, - undef, '245a in added framework cleared' ); - is( Koha::MarcSubfieldStructures->find($fwc, '300', 'a')->kohafield, - undef, '300a in added framework cleared' ); - - # 300a mapped in Default to another field than in other framework - Koha::MarcSubfieldStructure->new({ frameworkcode => q{}, tagfield => '300', tagsubfield => 'a', kohafield => 'something_else' })->store; - Koha::MarcSubfieldStructures->find($fwc, '300', 'a')->update({ kohafield => 'biblioitems.pages' }); - $res = Koha::Util::Dbrev->get_kohafield_exceptions; - is( @$res, 1, 'Found one exception' ); - is( $res->[0]->[1] . $res->[0]->[2], '300a', '300a was reported again' ); - Koha::Util::Dbrev->sync_kohafield; - $res = Koha::Util::Dbrev->get_kohafield_exceptions; - is( @$res, 0, 'Found no exceptions after third syncing' ); - is( Koha::MarcSubfieldStructures->find($fwc, '300', 'a')->kohafield, - 'something_else', '300a in added framework adjusted' ); -}; - $schema->storage->txn_rollback; --