From a78306e63fab948a3c030e7f45a96f364f8e514e 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 Import Records and Import Record Matches To test: 1 - Apply patches 2 - Update database 3 - Generate schema files (dbic) 4 - prove -v t/db_dependent/Koha/Import/Records.t 5 - prove -v t/db_dependent/Koha/Import/Record/Matches.t Signed-off-by: Andrew Fuerste-Henry Signed-off-by: Katrin Fischer JD Amended patch: remove unused unset_chosen method Signed-off-by: Jonathan Druart --- Koha/Import/Record.pm | 44 ++++++++++++++ Koha/Import/Record/Match.pm | 60 +++++++++++++++++++ Koha/Import/Record/Matches.pm | 56 +++++++++++++++++ Koha/Import/Records.pm | 56 +++++++++++++++++ Koha/Schema/Result/ImportRecord.pm | 7 +++ Koha/Schema/Result/ImportRecordMatch.pm | 7 ++- .../data/mysql/atomicupdate/bug_26326.perl | 12 ++++ installer/data/mysql/kohastructure.sql | 1 + t/db_dependent/Koha/Import/Record/Matches.t | 51 ++++++++++++++++ t/db_dependent/Koha/Import/Records.t | 46 ++++++++++++++ 10 files changed, 339 insertions(+), 1 deletion(-) create mode 100644 Koha/Import/Record.pm create mode 100644 Koha/Import/Record/Match.pm create mode 100644 Koha/Import/Record/Matches.pm create mode 100644 Koha/Import/Records.pm create mode 100644 installer/data/mysql/atomicupdate/bug_26326.perl create mode 100755 t/db_dependent/Koha/Import/Record/Matches.t create mode 100755 t/db_dependent/Koha/Import/Records.t diff --git a/Koha/Import/Record.pm b/Koha/Import/Record.pm new file mode 100644 index 0000000000..189d878dd8 --- /dev/null +++ b/Koha/Import/Record.pm @@ -0,0 +1,44 @@ +package Koha::Import::Record; + +# 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 - Koha Import Record Object class + +=head1 API + +=head2 Class methods + +=head2 Internal methods + +=head3 _type + +=cut + +sub _type { + return 'ImportRecord'; +} + +1; diff --git a/Koha/Import/Record/Match.pm b/Koha/Import/Record/Match.pm new file mode 100644 index 0000000000..3389fe2db7 --- /dev/null +++ b/Koha/Import/Record/Match.pm @@ -0,0 +1,60 @@ +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 Koha::Import::Record; + +use base qw(Koha::Object); + +=head1 NAME + +Koha::Import::Record::Match - Koha Import Record Match Object class + +=head1 API + +=head2 Class methods + +=head3 import_record + +my $import_record = $match->import_record; + +Return the import record of this match + +=cut + +sub import_record { + my ( $self ) = @_; + my $record_rs = $self->_result->import_record; + return Koha::Import::Record->_new_from_dbic( $record_rs ); +} + + +=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..5e6e5aa147 --- /dev/null +++ b/Koha/Import/Record/Matches.pm @@ -0,0 +1,56 @@ +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'; +} + +=head3 object_class + +Koha::Object class + +=cut + +sub object_class { + return 'Koha::Import::Record::Match'; +} + +1; diff --git a/Koha/Import/Records.pm b/Koha/Import/Records.pm new file mode 100644 index 0000000000..75d378d7e9 --- /dev/null +++ b/Koha/Import/Records.pm @@ -0,0 +1,56 @@ +package Koha::Import::Records; + +# 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; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::Import::Records - Koha Import Record Object set class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub _type { + return 'ImportRecord'; +} + +=head3 object_class + +Koha::Object class + +=cut + +sub object_class { + return 'Koha::Import::Record'; +} + +1; diff --git a/Koha/Schema/Result/ImportRecord.pm b/Koha/Schema/Result/ImportRecord.pm index 8499e4bb86..b32c9fea6a 100644 --- a/Koha/Schema/Result/ImportRecord.pm +++ b/Koha/Schema/Result/ImportRecord.pm @@ -265,6 +265,13 @@ __PACKAGE__->has_many( # Created by DBIx::Class::Schema::Loader v0.07046 @ 2019-04-18 10:50:48 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:bqIAQzhgioWtBWU8zFdtjw +sub koha_object_class { + 'Koha::Import::Record'; +} +sub koha_objects_class { + 'Koha::Import::Records'; +} + # You can replace this text with custom content, and it will be preserved on regeneration 1; diff --git a/Koha/Schema/Result/ImportRecordMatch.pm b/Koha/Schema/Result/ImportRecordMatch.pm index 2350d376a6..ae09a53e7f 100644 --- a/Koha/Schema/Result/ImportRecordMatch.pm +++ b/Koha/Schema/Result/ImportRecordMatch.pm @@ -78,6 +78,11 @@ __PACKAGE__->belongs_to( # Created by DBIx::Class::Schema::Loader v0.07049 @ 2021-01-21 13:39:29 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:kWU/SGWvZBBvVwEvDysrtA +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..b6a9c380fb --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_26326.perl @@ -0,0 +1,12 @@ +$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, 26326, "Add primary key to import_record_matches"); +} diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index 91990eb47a..64a06a084b 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -2902,6 +2902,7 @@ CREATE TABLE `import_record_matches` ( `import_record_id` int(11) NOT NULL COMMENT 'the id given to the imported bib record (import_records.import_record_id)', `candidate_match_id` int(11) NOT NULL COMMENT 'the biblio the imported record matches (biblio.biblionumber)', `score` int(11) NOT NULL DEFAULT 0 COMMENT 'the match score', + PRIMARY KEY (`import_record_id`,`candidate_match_id`), KEY `record_score` (`import_record_id`,`score`), CONSTRAINT `import_record_matches_ibfk_1` FOREIGN KEY (`import_record_id`) REFERENCES `import_records` (`import_record_id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; diff --git a/t/db_dependent/Koha/Import/Record/Matches.t b/t/db_dependent/Koha/Import/Record/Matches.t new file mode 100755 index 0000000000..f37653b363 --- /dev/null +++ b/t/db_dependent/Koha/Import/Record/Matches.t @@ -0,0 +1,51 @@ +#!/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 => 5; + +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', value => { import_record_id => $match_1->{import_record_id} } }); + +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' ); + +my $retrieved_match_2 = Koha::Import::Record::Matches->search({ import_record_id => $match_2->{import_record_id}, candidate_match_id => $match_2->{candidate_match_id} })->next; +my $import_record = $retrieved_match_2->import_record; +is( ref($import_record), 'Koha::Import::Record', "import_record has the correct class"); +is( $import_record->import_record_id, $retrieved_match_2->import_record_id, "We have the right record"); + +$schema->storage->txn_rollback; diff --git a/t/db_dependent/Koha/Import/Records.t b/t/db_dependent/Koha/Import/Records.t new file mode 100755 index 0000000000..0c9974ebc8 --- /dev/null +++ b/t/db_dependent/Koha/Import/Records.t @@ -0,0 +1,46 @@ +#!/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::Records; +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_records = Koha::Import::Records->search->count; + +my $record_1 = $builder->build({ source => 'ImportRecord' }); +my $record_2 = $builder->build({ source => 'ImportRecord' }); + +is( Koha::Import::Records->search->count, $nb_of_records + 2, 'The 2 records should have been added' ); + +my $retrieved_record_1 = Koha::Import::Records->search({ import_record_id => $record_1->{import_record_id}})->next; +is_deeply( $retrieved_record_1->unblessed, $record_1, 'Find a record by import record id should return the correct record' ); + +$retrieved_record_1->delete; +is( Koha::Import::Records->search->count, $nb_of_records + 1, 'Delete should have deleted the record' ); + +$schema->storage->txn_rollback; -- 2.39.5