From 26e32ee05f47916ad7f06d029ee1289fd1f33817 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Mon, 31 Aug 2020 16:47:51 +0000 Subject: [PATCH] Bug 26326: Add Koha Objects for Record Import Matches To test: 1 - Apply patches 2 - Update database 3 - Generate schema files (dbic) 4 - prove -v t/db_dependent/Koha/Import/Record/Matches.t --- Koha/Import/Record/Match.pm | 44 +++++++++++++++++++++ Koha/Import/Record/Matches.pm | 50 ++++++++++++++++++++++++ Koha/Schema/Result/ImportRecordMatch.pm | 7 +++- installer/data/mysql/atomicupdate/bug_26326.perl | 11 ++++++ installer/data/mysql/kohastructure.sql | 1 + t/db_dependent/Koha/Import/Record/Matches.t | 47 ++++++++++++++++++++++ 6 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 Koha/Import/Record/Match.pm create mode 100644 Koha/Import/Record/Matches.pm create mode 100644 installer/data/mysql/atomicupdate/bug_26326.perl create mode 100644 t/db_dependent/Koha/Import/Record/Matches.t diff --git a/Koha/Import/Record/Match.pm b/Koha/Import/Record/Match.pm new file mode 100644 index 0000000000..188b262623 --- /dev/null +++ b/Koha/Import/Record/Match.pm @@ -0,0 +1,44 @@ +package Koha::Import::Record::Match; + +# 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, see . + +use Modern::Perl; + +use Carp; + +use Koha::Database; + +use base qw(Koha::Object); + +=head1 NAME + +Koha::Import::Record::Match - Koha Import Record Match Object class + +=head1 API + +=head2 Class methods + +=head2 Internal methods + +=head3 _type + +=cut + +sub _type { + return 'ImportRecordMatch'; +} + +1; diff --git a/Koha/Import/Record/Matches.pm b/Koha/Import/Record/Matches.pm new file mode 100644 index 0000000000..fa60d9547c --- /dev/null +++ b/Koha/Import/Record/Matches.pm @@ -0,0 +1,50 @@ +package Koha::Import::Record::Matches; + +# 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, see . + +use Modern::Perl; + +use Carp; + +use Koha::Database; + +use Koha::Import::Record::Match; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::Import::Record::Matches - Koha Import Record Matches Object set class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub _type { + return 'ImportRecordMatch'; +} + +sub object_class { + return 'Koha::Import::Record::Match'; +} + +1; diff --git a/Koha/Schema/Result/ImportRecordMatch.pm b/Koha/Schema/Result/ImportRecordMatch.pm index 0ea5d56914..1330ce42eb 100644 --- a/Koha/Schema/Result/ImportRecordMatch.pm +++ b/Koha/Schema/Result/ImportRecordMatch.pm @@ -72,6 +72,11 @@ __PACKAGE__->belongs_to( # Created by DBIx::Class::Schema::Loader v0.07039 @ 2014-07-11 09:26:55 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:hJW36EeP+H8+0/Ij3iuIFw +sub koha_object_class { + 'Koha::Import::Record::Match'; +} +sub koha_objects_class { + 'Koha::Import::Record::Matches'; +} -# You can replace this text with custom code or comments, and it will be preserved on regeneration 1; diff --git a/installer/data/mysql/atomicupdate/bug_26326.perl b/installer/data/mysql/atomicupdate/bug_26326.perl new file mode 100644 index 0000000000..5a846352e1 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_26326.perl @@ -0,0 +1,11 @@ +$DBversion = 'XXX'; +if( CheckVersion( $DBversion ) ) { + unless( + primary_key_exists('import_record_matches','import_record_id') && + primary_key_exists('import_record_matches','candidate_match_id') + ){ + $dbh->do( "ALTER TABLE import_record_matches DROP PRIMARY KEY" ); + $dbh->do( "ALTER TABLE import_record_matches ADD PRIMARY KEY (import_record_id,candidate_match_id)" ); + } + NewVersion( $DBversion, 22785, "Add primary key to import_record_matches"); +} diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index cc7c133653..b891612d4d 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -758,6 +758,7 @@ CREATE TABLE `import_record_matches` ( -- matches found when importing a batch o `import_record_id` int(11) NOT NULL, -- the id given to the imported bib record (import_records.import_record_id) `candidate_match_id` int(11) NOT NULL, -- the biblio the imported record matches (biblio.biblionumber) `score` int(11) NOT NULL default 0, -- the match score + PRIMARY KEY (`import_record_id`,`candidate_match_id`), CONSTRAINT `import_record_matches_ibfk_1` FOREIGN KEY (`import_record_id`) REFERENCES `import_records` (`import_record_id`) ON DELETE CASCADE ON UPDATE CASCADE, KEY `record_score` (`import_record_id`, `score`) diff --git a/t/db_dependent/Koha/Import/Record/Matches.t b/t/db_dependent/Koha/Import/Record/Matches.t new file mode 100644 index 0000000000..2536e02e7e --- /dev/null +++ b/t/db_dependent/Koha/Import/Record/Matches.t @@ -0,0 +1,47 @@ +#!/usr/bin/perl + +# Copyright 2020 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, see . + +use Modern::Perl; + +use Test::More tests => 3; + +use Koha::Import::Record::Matches; +use Koha::Database; + +use t::lib::TestBuilder; + +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; + +my $builder = t::lib::TestBuilder->new; +my $nb_of_matches = Koha::Import::Record::Matches->search->count; + +my $match_1 = $builder->build({ source => 'ImportRecordMatch' }); +my $match_2 = $builder->build({ source => 'ImportRecordMatch' }); + +is( Koha::Import::Record::Matches->search->count, $nb_of_matches + 2, 'The 2 matches should have been added' ); + +my $retrieved_match_1 = Koha::Import::Record::Matches->search({ import_record_id => $match_1->{import_record_id}, candidate_match_id => $match_1->{candidate_match_id} })->next; +is_deeply( $retrieved_match_1->unblessed, $match_1, 'Find a match by import record id and candidate should return the correct match' ); + +$retrieved_match_1->delete; +is( Koha::Import::Record::Matches->search->count, $nb_of_matches + 1, 'Delete should have deleted the match' ); + +$schema->storage->txn_rollback; + -- 2.11.0