Bug 32437: Add Objects for ImportAuths
authorNick Clemens <nick@bywatersolutions.com>
Mon, 12 Dec 2022 13:59:56 +0000 (13:59 +0000)
committerTomas Cohen Arazi <tomascohen@theke.io>
Fri, 31 Mar 2023 11:13:25 +0000 (13:13 +0200)
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 <andrewfh@dubcolib.org>
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Koha/Import/Record/Auth.pm [new file with mode: 0644]
Koha/Import/Record/Auths.pm [new file with mode: 0644]
installer/data/mysql/atomicupdate/bug_32437.pl [new file with mode: 0755]
installer/data/mysql/kohastructure.sql
t/db_dependent/Koha/Import/Record/Auths.t [new file with mode: 0755]
t/db_dependent/Koha/Import/Record/Biblios.t [new file with mode: 0755]

diff --git a/Koha/Import/Record/Auth.pm b/Koha/Import/Record/Auth.pm
new file mode 100644 (file)
index 0000000..beaab30
--- /dev/null
@@ -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 <http://www.gnu.org/licenses>.
+
+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 (file)
index 0000000..2a8f1b2
--- /dev/null
@@ -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 <http://www.gnu.org/licenses>.
+
+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/installer/data/mysql/atomicupdate/bug_32437.pl b/installer/data/mysql/atomicupdate/bug_32437.pl
new file mode 100755 (executable)
index 0000000..6ff9139
--- /dev/null
@@ -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";
+        }
+    },
+};
index a3faa37..52e8ad2 100644 (file)
@@ -3285,6 +3285,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 (executable)
index 0000000..d992a41
--- /dev/null
@@ -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 <http://www.gnu.org/licenses>.
+
+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 (executable)
index 0000000..cfd0f71
--- /dev/null
@@ -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 <http://www.gnu.org/licenses>.
+
+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;