From 68217297db9ad8f22ed6692258f7fc8cc7efba0e Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Mon, 12 Dec 2022 13:59:56 +0000 Subject: [PATCH] Bug 32437: Add Objects for ImportAuths This patch: 1 - Adds an atomic update to add a primary key to import_auths table 2 - Adds objects for Koha::Import::Records::Auths 3 - Adds tests for import auth and biblio objects Signed-off-by: Andrew Fuerste-Henry --- Koha/Import/Record/Auth.pm | 44 ++++++++++++++++ Koha/Import/Record/Auths.pm | 52 +++++++++++++++++++ Koha/Schema/Result/ImportAuth.pm | 23 ++++++-- Koha/Schema/Result/ImportRecord.pm | 12 ++--- .../data/mysql/atomicupdate/bug_32437.pl | 19 +++++++ installer/data/mysql/kohastructure.sql | 1 + t/db_dependent/Koha/Import/Record/Auths.t | 46 ++++++++++++++++ t/db_dependent/Koha/Import/Record/Biblios.t | 46 ++++++++++++++++ 8 files changed, 234 insertions(+), 9 deletions(-) create mode 100644 Koha/Import/Record/Auth.pm create mode 100644 Koha/Import/Record/Auths.pm create mode 100755 installer/data/mysql/atomicupdate/bug_32437.pl create mode 100755 t/db_dependent/Koha/Import/Record/Auths.t create mode 100755 t/db_dependent/Koha/Import/Record/Biblios.t diff --git a/Koha/Import/Record/Auth.pm b/Koha/Import/Record/Auth.pm new file mode 100644 index 0000000000..beaab30eaf --- /dev/null +++ b/Koha/Import/Record/Auth.pm @@ -0,0 +1,44 @@ +package Koha::Import::Record::Auth; + +# 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::Auth - Koha Import Record Authority Object class + +=head1 API + +=head2 Internal methods + +=head3 _type + +Returns name of corresponding DBIC resultset + +=cut + +sub _type { + return 'ImportAuth'; +} + +1; diff --git a/Koha/Import/Record/Auths.pm b/Koha/Import/Record/Auths.pm new file mode 100644 index 0000000000..2a8f1b2970 --- /dev/null +++ b/Koha/Import/Record/Auths.pm @@ -0,0 +1,52 @@ +package Koha::Import::Record::Auths; + +# 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 Koha::Import::Record::Auth; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::Import::Record::Auths - Koha Import Record Auths Object set class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub _type { + return 'ImportAuth'; +} + +=head3 object_class + +Koha::Object class + +=cut + +sub object_class { + return 'Koha::Import::Record::Auth'; +} + +1; diff --git a/Koha/Schema/Result/ImportAuth.pm b/Koha/Schema/Result/ImportAuth.pm index a8fc448da1..c0f96dec23 100644 --- a/Koha/Schema/Result/ImportAuth.pm +++ b/Koha/Schema/Result/ImportAuth.pm @@ -67,6 +67,18 @@ __PACKAGE__->add_columns( { data_type => "varchar", is_nullable => 1, size => 25 }, ); +=head1 PRIMARY KEY + +=over 4 + +=item * L + +=back + +=cut + +__PACKAGE__->set_primary_key("import_record_id"); + =head1 RELATIONS =head2 import_record @@ -85,9 +97,14 @@ __PACKAGE__->belongs_to( ); -# Created by DBIx::Class::Schema::Loader v0.07025 @ 2013-10-14 20:56:21 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:bm1ctlmYDh4qWUU95SLqBw +# Created by DBIx::Class::Schema::Loader v0.07049 @ 2022-12-12 12:33:40 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:3XmbG33SxiC0v4jj6V4bfQ +sub koha_object_class { + 'Koha::Import::Record::Auth'; +} +sub koha_objects_class { + 'Koha::Import::Record::Auths'; +} -# You can replace this text with custom content, and it will be preserved on regeneration 1; diff --git a/Koha/Schema/Result/ImportRecord.pm b/Koha/Schema/Result/ImportRecord.pm index 64e9274e07..9bf124a2a2 100644 --- a/Koha/Schema/Result/ImportRecord.pm +++ b/Koha/Schema/Result/ImportRecord.pm @@ -186,16 +186,16 @@ __PACKAGE__->set_primary_key("import_record_id"); =head1 RELATIONS -=head2 import_auths +=head2 import_auth -Type: has_many +Type: might_have Related object: L =cut -__PACKAGE__->has_many( - "import_auths", +__PACKAGE__->might_have( + "import_auth", "Koha::Schema::Result::ImportAuth", { "foreign.import_record_id" => "self.import_record_id" }, { cascade_copy => 0, cascade_delete => 0 }, @@ -262,8 +262,8 @@ __PACKAGE__->has_many( ); -# Created by DBIx::Class::Schema::Loader v0.07049 @ 2022-10-17 11:17:50 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:bGhtHdQ7/pXWWezcgz/4EA +# Created by DBIx::Class::Schema::Loader v0.07049 @ 2022-12-12 12:33:40 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:OEpfDFkYGGgTowPIly5bYQ sub koha_object_class { 'Koha::Import::Record'; diff --git a/installer/data/mysql/atomicupdate/bug_32437.pl b/installer/data/mysql/atomicupdate/bug_32437.pl new file mode 100755 index 0000000000..6ff91391b9 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_32437.pl @@ -0,0 +1,19 @@ +use Modern::Perl; + +return { + bug_number => "32437", + description => "Add primary key to import_auths tables", + up => sub { + my ($args) = @_; + my ($dbh, $out) = @$args{qw(dbh out)}; + if( !primary_key_exists('import_auths') ){ + $dbh->do(q{ALTER TABLE import_auths ADD PRIMARY KEY (import_record_id);}); + say $out "Added PRIMARY KEY ON import_record_id to import_authd table"; + } elsif( !primary_key_exists('import_auths','import_record_id') ){ + say $out "Found an existing PRIMARY KEY on import_auths table"; + say $out "You must delete this key and replace it with a key on import_record_id"; + } else { + say $out "PRIMARY KEY import_record_id on import_auths already exists"; + } + }, +}; diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index d831006cd5..a4d3be7e5f 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -3277,6 +3277,7 @@ CREATE TABLE `import_auths` ( `control_number` varchar(25) DEFAULT NULL, `authorized_heading` varchar(128) DEFAULT NULL, `original_source` varchar(25) DEFAULT NULL, + PRIMARY KEY (`import_record_id`), KEY `import_auths_ibfk_1` (`import_record_id`), KEY `matched_authid` (`matched_authid`), CONSTRAINT `import_auths_ibfk_1` FOREIGN KEY (`import_record_id`) REFERENCES `import_records` (`import_record_id`) ON DELETE CASCADE ON UPDATE CASCADE diff --git a/t/db_dependent/Koha/Import/Record/Auths.t b/t/db_dependent/Koha/Import/Record/Auths.t new file mode 100755 index 0000000000..d992a41735 --- /dev/null +++ b/t/db_dependent/Koha/Import/Record/Auths.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::Record::Auths; +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_record_auths = Koha::Import::Record::Auths->search->count; + +my $record_auth_1 = $builder->build({ source => 'ImportAuth' }); +my $record_auth_2 = $builder->build({ source => 'ImportAuth' }); + +is( Koha::Import::Record::Auths->search->count, $nb_of_record_auths + 2, 'The 2 record auths should have been added' ); + +my $retrieved_record_auth_1 = Koha::Import::Record::Auths->search({ import_record_id => $record_auth_1->{import_record_id}})->next; +is_deeply( $retrieved_record_auth_1->unblessed, $record_auth_1, 'Find a record auth by import record id should return the correct record auth' ); + +$retrieved_record_auth_1->delete; +is( Koha::Import::Record::Auths->search->count, $nb_of_record_auths + 1, 'Delete should have deleted the record auth' ); + +$schema->storage->txn_rollback; diff --git a/t/db_dependent/Koha/Import/Record/Biblios.t b/t/db_dependent/Koha/Import/Record/Biblios.t new file mode 100755 index 0000000000..cfd0f71b9d --- /dev/null +++ b/t/db_dependent/Koha/Import/Record/Biblios.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::Record::Biblios; +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_record_biblios = Koha::Import::Record::Biblios->search->count; + +my $record_biblio_1 = $builder->build({ source => 'ImportBiblio' }); +my $record_biblio_2 = $builder->build({ source => 'ImportBiblio' }); + +is( Koha::Import::Record::Biblios->search->count, $nb_of_record_biblios + 2, 'The 2 record biblios should have been added' ); + +my $retrieved_record_biblio_1 = Koha::Import::Record::Biblios->search({ import_record_id => $record_biblio_1->{import_record_id}})->next; +is_deeply( $retrieved_record_biblio_1->unblessed, $record_biblio_1, 'Find a record biblio by import record id should return the correct record biblio' ); + +$retrieved_record_biblio_1->delete; +is( Koha::Import::Record::Biblios->search->count, $nb_of_record_biblios + 1, 'Delete should have deleted the record biblio' ); + +$schema->storage->txn_rollback; -- 2.30.2