From 0e9d45e7b4cb2e6a6d6b9559671b5d91c0e6fab4 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Fri, 17 Apr 2020 10:17:32 +0100 Subject: [PATCH] Bug 21901: DBRev 19.12.00.076 Signed-off-by: Martin Renvoize --- Koha.pm | 2 +- Koha/Schema/Result/Biblio.pm | 49 ++++++- Koha/Schema/Result/Serial.pm | 48 +++++-- Koha/Schema/Result/Subscription.pm | 53 ++++++- Koha/Schema/Result/Subscriptionhistory.pm | 44 +++++- .../data/mysql/atomicupdate/bug_21901.perl | 123 ----------------- installer/data/mysql/updatedatabase.pl | 129 ++++++++++++++++++ 7 files changed, 302 insertions(+), 146 deletions(-) delete mode 100644 installer/data/mysql/atomicupdate/bug_21901.perl diff --git a/Koha.pm b/Koha.pm index fbf8b4093d..95a46f1750 100644 --- a/Koha.pm +++ b/Koha.pm @@ -29,7 +29,7 @@ use vars qw{ $VERSION }; # - #4 : the developer version. The 4th number is the database subversion. # used by developers when the database changes. updatedatabase take care of the changes itself # and is automatically called by Auth.pm when needed. -$VERSION = "19.12.00.075"; +$VERSION = "19.12.00.076"; sub version { return $VERSION; diff --git a/Koha/Schema/Result/Biblio.pm b/Koha/Schema/Result/Biblio.pm index 7c66b1dc03..806faf87ad 100644 --- a/Koha/Schema/Result/Biblio.pm +++ b/Koha/Schema/Result/Biblio.pm @@ -345,6 +345,51 @@ __PACKAGE__->has_many( { cascade_copy => 0, cascade_delete => 0 }, ); +=head2 serials + +Type: has_many + +Related object: L + +=cut + +__PACKAGE__->has_many( + "serials", + "Koha::Schema::Result::Serial", + { "foreign.biblionumber" => "self.biblionumber" }, + { cascade_copy => 0, cascade_delete => 0 }, +); + +=head2 subscriptionhistories + +Type: has_many + +Related object: L + +=cut + +__PACKAGE__->has_many( + "subscriptionhistories", + "Koha::Schema::Result::Subscriptionhistory", + { "foreign.biblionumber" => "self.biblionumber" }, + { cascade_copy => 0, cascade_delete => 0 }, +); + +=head2 subscriptions + +Type: has_many + +Related object: L + +=cut + +__PACKAGE__->has_many( + "subscriptions", + "Koha::Schema::Result::Subscription", + { "foreign.biblionumber" => "self.biblionumber" }, + { cascade_copy => 0, cascade_delete => 0 }, +); + =head2 suggestions Type: has_many @@ -406,8 +451,8 @@ __PACKAGE__->has_many( ); -# Created by DBIx::Class::Schema::Loader v0.07046 @ 2019-10-01 07:08:47 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:otCex8qzJmZyc+JXpKNdpQ +# Created by DBIx::Class::Schema::Loader v0.07046 @ 2020-04-17 09:15:51 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:p2SIq565zPyE3ZUkSuXyBA __PACKAGE__->has_one( diff --git a/Koha/Schema/Result/Serial.pm b/Koha/Schema/Result/Serial.pm index 58aed2c012..e3bcadbf6f 100644 --- a/Koha/Schema/Result/Serial.pm +++ b/Koha/Schema/Result/Serial.pm @@ -31,17 +31,15 @@ __PACKAGE__->table("serial"); =head2 biblionumber - data_type: 'varchar' - default_value: (empty string) + data_type: 'integer' + is_foreign_key: 1 is_nullable: 0 - size: 100 =head2 subscriptionid - data_type: 'varchar' - default_value: (empty string) + data_type: 'integer' + is_foreign_key: 1 is_nullable: 0 - size: 100 =head2 serialseq @@ -120,9 +118,9 @@ __PACKAGE__->add_columns( "serialid", { data_type => "integer", is_auto_increment => 1, is_nullable => 0 }, "biblionumber", - { data_type => "varchar", default_value => "", is_nullable => 0, size => 100 }, + { data_type => "integer", is_foreign_key => 1, is_nullable => 0 }, "subscriptionid", - { data_type => "varchar", default_value => "", is_nullable => 0, size => 100 }, + { data_type => "integer", is_foreign_key => 1, is_nullable => 0 }, "serialseq", { data_type => "varchar", default_value => "", is_nullable => 0, size => 100 }, "serialseq_x", @@ -163,6 +161,21 @@ __PACKAGE__->set_primary_key("serialid"); =head1 RELATIONS +=head2 biblionumber + +Type: belongs_to + +Related object: L + +=cut + +__PACKAGE__->belongs_to( + "biblionumber", + "Koha::Schema::Result::Biblio", + { biblionumber => "biblionumber" }, + { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" }, +); + =head2 serialitems Type: has_many @@ -178,9 +191,24 @@ __PACKAGE__->has_many( { cascade_copy => 0, cascade_delete => 0 }, ); +=head2 subscriptionid + +Type: belongs_to + +Related object: L + +=cut + +__PACKAGE__->belongs_to( + "subscriptionid", + "Koha::Schema::Result::Subscription", + { subscriptionid => "subscriptionid" }, + { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" }, +); + -# Created by DBIx::Class::Schema::Loader v0.07042 @ 2018-02-16 17:54:54 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:sRygXoIOnqpdk0lqVMcBdA +# Created by DBIx::Class::Schema::Loader v0.07046 @ 2020-04-17 09:15:51 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:ktCCJzkgRUFpYu857WQONA # You can replace this text with custom code or comments, and it will be preserved on regeneration diff --git a/Koha/Schema/Result/Subscription.pm b/Koha/Schema/Result/Subscription.pm index 735cd05655..a668b81fed 100644 --- a/Koha/Schema/Result/Subscription.pm +++ b/Koha/Schema/Result/Subscription.pm @@ -26,7 +26,7 @@ __PACKAGE__->table("subscription"); =head2 biblionumber data_type: 'integer' - default_value: 0 + is_foreign_key: 1 is_nullable: 0 =head2 subscriptionid @@ -280,7 +280,7 @@ __PACKAGE__->table("subscription"); __PACKAGE__->add_columns( "biblionumber", - { data_type => "integer", default_value => 0, is_nullable => 0 }, + { data_type => "integer", is_foreign_key => 1, is_nullable => 0 }, "subscriptionid", { data_type => "integer", is_auto_increment => 1, is_nullable => 0 }, "librarian", @@ -396,6 +396,21 @@ __PACKAGE__->has_many( { cascade_copy => 0, cascade_delete => 0 }, ); +=head2 biblionumber + +Type: belongs_to + +Related object: L + +=cut + +__PACKAGE__->belongs_to( + "biblionumber", + "Koha::Schema::Result::Biblio", + { biblionumber => "biblionumber" }, + { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" }, +); + =head2 numberpattern Type: belongs_to @@ -436,6 +451,36 @@ __PACKAGE__->belongs_to( }, ); +=head2 serials + +Type: has_many + +Related object: L + +=cut + +__PACKAGE__->has_many( + "serials", + "Koha::Schema::Result::Serial", + { "foreign.subscriptionid" => "self.subscriptionid" }, + { cascade_copy => 0, cascade_delete => 0 }, +); + +=head2 subscriptionhistory + +Type: might_have + +Related object: L + +=cut + +__PACKAGE__->might_have( + "subscriptionhistory", + "Koha::Schema::Result::Subscriptionhistory", + { "foreign.subscriptionid" => "self.subscriptionid" }, + { cascade_copy => 0, cascade_delete => 0 }, +); + =head2 subscriptionroutinglists Type: has_many @@ -452,8 +497,8 @@ __PACKAGE__->has_many( ); -# Created by DBIx::Class::Schema::Loader v0.07046 @ 2020-04-15 11:35:38 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:5zW2XKZNcvytO8svTJ4ijQ +# Created by DBIx::Class::Schema::Loader v0.07046 @ 2020-04-17 09:15:51 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:Bw7XzPSvv9fjc9F6+uIBUw __PACKAGE__->has_many( "additional_field_values", diff --git a/Koha/Schema/Result/Subscriptionhistory.pm b/Koha/Schema/Result/Subscriptionhistory.pm index a7d44fca3a..c39953a16b 100644 --- a/Koha/Schema/Result/Subscriptionhistory.pm +++ b/Koha/Schema/Result/Subscriptionhistory.pm @@ -26,13 +26,13 @@ __PACKAGE__->table("subscriptionhistory"); =head2 biblionumber data_type: 'integer' - default_value: 0 + is_foreign_key: 1 is_nullable: 0 =head2 subscriptionid data_type: 'integer' - default_value: 0 + is_foreign_key: 1 is_nullable: 0 =head2 histstartdate @@ -71,9 +71,9 @@ __PACKAGE__->table("subscriptionhistory"); __PACKAGE__->add_columns( "biblionumber", - { data_type => "integer", default_value => 0, is_nullable => 0 }, + { data_type => "integer", is_foreign_key => 1, is_nullable => 0 }, "subscriptionid", - { data_type => "integer", default_value => 0, is_nullable => 0 }, + { data_type => "integer", is_foreign_key => 1, is_nullable => 0 }, "histstartdate", { data_type => "date", datetime_undef_if_invalid => 1, is_nullable => 1 }, "histenddate", @@ -100,9 +100,41 @@ __PACKAGE__->add_columns( __PACKAGE__->set_primary_key("subscriptionid"); +=head1 RELATIONS -# Created by DBIx::Class::Schema::Loader v0.07046 @ 2019-06-17 07:24:39 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:SK33dc7lx/rDGX3tiR84ww +=head2 biblionumber + +Type: belongs_to + +Related object: L + +=cut + +__PACKAGE__->belongs_to( + "biblionumber", + "Koha::Schema::Result::Biblio", + { biblionumber => "biblionumber" }, + { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" }, +); + +=head2 subscriptionid + +Type: belongs_to + +Related object: L + +=cut + +__PACKAGE__->belongs_to( + "subscriptionid", + "Koha::Schema::Result::Subscription", + { subscriptionid => "subscriptionid" }, + { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" }, +); + + +# Created by DBIx::Class::Schema::Loader v0.07046 @ 2020-04-17 09:15:51 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:bcJbffy74eI1r+e4pImAwQ sub koha_object_class { 'Koha::Subscription::History'; diff --git a/installer/data/mysql/atomicupdate/bug_21901.perl b/installer/data/mysql/atomicupdate/bug_21901.perl deleted file mode 100644 index fb11d77bdd..0000000000 --- a/installer/data/mysql/atomicupdate/bug_21901.perl +++ /dev/null @@ -1,123 +0,0 @@ -$DBversion = 'XXX'; # will be replaced by the RM -if( CheckVersion( $DBversion ) ) { - - $dbh->do(q| - UPDATE - serial - SET - planneddate = NULL - WHERE - planneddate = '0000-00-00' - |); - - $dbh->do(q| - UPDATE - serial - SET - publisheddate = NULL - WHERE - publisheddate = '0000-00-00' - |); - - $dbh->do(q| - UPDATE - serial - SET - claimdate = NULL - WHERE - claimdate = '0000-00-00' - |); - - $dbh->do(q| - ALTER TABLE serial - MODIFY COLUMN biblionumber INT(11) NOT NULL - |); - - unless ( foreign_key_exists( 'serial', 'serial_ibfk_1' ) ) { - my $serials = $dbh->selectall_arrayref(q| - SELECT serialid FROM serial WHERE biblionumber NOT IN (SELECT biblionumber FROM biblio) - |, { Slice => {} }); - if ( @$serials ) { - warn q|WARNING - The following serials are deleted, they were not attached to an existing bibliographic record (serialid): | . join ", ", map { $_->{serialid} } @$serials; - $dbh->do(q| - DELETE FROM serial WHERE biblionumber NOT IN (SELECT biblionumber FROM biblio) - |); - } - $dbh->do(q| - ALTER TABLE serial - ADD CONSTRAINT serial_ibfk_1 FOREIGN KEY (biblionumber) REFERENCES biblio (biblionumber) ON DELETE CASCADE ON UPDATE CASCADE - |); - } - - $dbh->do(q| - ALTER TABLE serial - MODIFY COLUMN subscriptionid INT(11) NOT NULL - |); - - unless ( foreign_key_exists( 'serial', 'serial_ibfk_2' ) ) { - my $serials = $dbh->selectall_arrayref(q| - SELECT serialid FROM serial WHERE subscriptionid NOT IN (SELECT subscriptionid FROM subscription) - |, { Slice => {} }); - if ( @$serials ) { - warn q|WARNING - The following serials are deleted, they were not attached to an existing subscription (serialid): | . join ", ", map { $_->{serialid} } @$serials; - $dbh->do(q| - DELETE FROM serial WHERE subscriptionid NOT IN (SELECT subscriptionid FROM subscription) - |); - } - $dbh->do(q| - ALTER TABLE serial - ADD CONSTRAINT serial_ibfk_2 FOREIGN KEY (subscriptionid) REFERENCES subscription (subscriptionid) ON DELETE CASCADE ON UPDATE CASCADE - |); - } - - $dbh->do(q| - ALTER TABLE subscriptionhistory - MODIFY COLUMN biblionumber int(11) NOT NULL, - MODIFY COLUMN subscriptionid int(11) NOT NULL - |); - - unless ( foreign_key_exists( 'subscriptionhistory', 'subscription_history_ibfk_1' ) ) { - $dbh->do(q| - DELETE FROM subscriptionhistory WHERE biblionumber NOT IN (SELECT biblionumber FROM biblio) - |); - $dbh->do(q| - ALTER TABLE subscriptionhistory - ADD CONSTRAINT subscription_history_ibfk_1 FOREIGN KEY (biblionumber) REFERENCES biblio (biblionumber) ON DELETE CASCADE ON UPDATE CASCADE - |); - } - - unless ( foreign_key_exists( 'subscriptionhistory', 'subscription_history_ibfk_2' ) ) { - $dbh->do(q| - DELETE FROM subscriptionhistory WHERE subscriptionid NOT IN (SELECT subscriptionid FROM subscription) - |); - $dbh->do(q| - ALTER TABLE subscriptionhistory - ADD CONSTRAINT subscription_history_ibfk_2 FOREIGN KEY (subscriptionid) REFERENCES subscription (subscriptionid) ON DELETE CASCADE ON UPDATE CASCADE - |); - } - - $dbh->do(q| - ALTER TABLE subscription - MODIFY COLUMN biblionumber int(11) NOT NULL - |); - - unless ( foreign_key_exists( 'subscription', 'subscription_ibfk_3' ) ) { - my $subscriptions = $dbh->selectall_arrayref(q| - SELECT subscriptionid FROM subscription WHERE biblionumber NOT IN (SELECT biblionumber FROM biblio) - |, { Slice => {} }); - if ( @$subscriptions ) { - warn q|WARNING - The following subscriptions are deleted, they were not attached to an existing bibliographic record (subscriptionid): | . join ", ", map { $_->{subscriptionid} } @$subscriptions; - - $dbh->do(q| - DELETE FROM subscription WHERE biblionumber NOT IN (SELECT biblionumber FROM biblio) - |); - } - $dbh->do(q| - ALTER TABLE subscription - ADD CONSTRAINT subscription_ibfk_3 FOREIGN KEY (biblionumber) REFERENCES biblio (biblionumber) ON DELETE CASCADE ON UPDATE CASCADE - |); - } - - SetVersion( $DBversion ); - print "Upgrade to $DBversion done (Bug 21901 - Add foreign key constraints on serial)\n"; -} diff --git a/installer/data/mysql/updatedatabase.pl b/installer/data/mysql/updatedatabase.pl index 0f9c5c85b0..1b67a58edb 100755 --- a/installer/data/mysql/updatedatabase.pl +++ b/installer/data/mysql/updatedatabase.pl @@ -21629,6 +21629,135 @@ if( CheckVersion( $DBversion ) ) { NewVersion( $DBversion, 25086, "Set changed_fields column of borrower_modifications as nullable"); } +$DBversion = '19.12.00.076'; +if( CheckVersion( $DBversion ) ) { + my @warnings; + + $dbh->do(q| + UPDATE + serial + SET + planneddate = NULL + WHERE + planneddate = '0000-00-00' + |); + + $dbh->do(q| + UPDATE + serial + SET + publisheddate = NULL + WHERE + publisheddate = '0000-00-00' + |); + + $dbh->do(q| + UPDATE + serial + SET + claimdate = NULL + WHERE + claimdate = '0000-00-00' + |); + + $dbh->do(q| + ALTER TABLE serial + MODIFY COLUMN biblionumber INT(11) NOT NULL + |); + + unless ( foreign_key_exists( 'serial', 'serial_ibfk_1' ) ) { + my $serials = $dbh->selectall_arrayref(q| + SELECT serialid FROM serial WHERE biblionumber NOT IN (SELECT biblionumber FROM biblio) + |, { Slice => {} }); + if ( @$serials ) { + push @warnings, q|WARNING - The following serials are deleted, they were not attached to an existing bibliographic record (serialid): | . join ", ", map { $_->{serialid} } @$serials; + $dbh->do(q| + DELETE FROM serial WHERE biblionumber NOT IN (SELECT biblionumber FROM biblio) + |); + } + $dbh->do(q| + ALTER TABLE serial + ADD CONSTRAINT serial_ibfk_1 FOREIGN KEY (biblionumber) REFERENCES biblio (biblionumber) ON DELETE CASCADE ON UPDATE CASCADE + |); + } + + $dbh->do(q| + ALTER TABLE serial + MODIFY COLUMN subscriptionid INT(11) NOT NULL + |); + + unless ( foreign_key_exists( 'serial', 'serial_ibfk_2' ) ) { + my $serials = $dbh->selectall_arrayref(q| + SELECT serialid FROM serial WHERE subscriptionid NOT IN (SELECT subscriptionid FROM subscription) + |, { Slice => {} }); + if ( @$serials ) { + push @warnings, q|WARNING - The following serials are deleted, they were not attached to an existing subscription (serialid): | . join ", ", map { $_->{serialid} } @$serials; + $dbh->do(q| + DELETE FROM serial WHERE subscriptionid NOT IN (SELECT subscriptionid FROM subscription) + |); + } + $dbh->do(q| + ALTER TABLE serial + ADD CONSTRAINT serial_ibfk_2 FOREIGN KEY (subscriptionid) REFERENCES subscription (subscriptionid) ON DELETE CASCADE ON UPDATE CASCADE + |); + } + + $dbh->do(q| + ALTER TABLE subscriptionhistory + MODIFY COLUMN biblionumber int(11) NOT NULL, + MODIFY COLUMN subscriptionid int(11) NOT NULL + |); + + unless ( foreign_key_exists( 'subscriptionhistory', 'subscription_history_ibfk_1' ) ) { + $dbh->do(q| + DELETE FROM subscriptionhistory WHERE biblionumber NOT IN (SELECT biblionumber FROM biblio) + |); + $dbh->do(q| + ALTER TABLE subscriptionhistory + ADD CONSTRAINT subscription_history_ibfk_1 FOREIGN KEY (biblionumber) REFERENCES biblio (biblionumber) ON DELETE CASCADE ON UPDATE CASCADE + |); + } + + unless ( foreign_key_exists( 'subscriptionhistory', 'subscription_history_ibfk_2' ) ) { + $dbh->do(q| + DELETE FROM subscriptionhistory WHERE subscriptionid NOT IN (SELECT subscriptionid FROM subscription) + |); + $dbh->do(q| + ALTER TABLE subscriptionhistory + ADD CONSTRAINT subscription_history_ibfk_2 FOREIGN KEY (subscriptionid) REFERENCES subscription (subscriptionid) ON DELETE CASCADE ON UPDATE CASCADE + |); + } + + $dbh->do(q| + ALTER TABLE subscription + MODIFY COLUMN biblionumber int(11) NOT NULL + |); + + unless ( foreign_key_exists( 'subscription', 'subscription_ibfk_3' ) ) { + my $subscriptions = $dbh->selectall_arrayref(q| + SELECT subscriptionid FROM subscription WHERE biblionumber NOT IN (SELECT biblionumber FROM biblio) + |, { Slice => {} }); + if ( @$subscriptions ) { + push @warnings, q|WARNING - The following subscriptions are deleted, they were not attached to an existing bibliographic record (subscriptionid): | . join ", ", map { $_->{subscriptionid} } @$subscriptions; + + $dbh->do(q| + DELETE FROM subscription WHERE biblionumber NOT IN (SELECT biblionumber FROM biblio) + |); + } + $dbh->do(q| + ALTER TABLE subscription + ADD CONSTRAINT subscription_ibfk_3 FOREIGN KEY (biblionumber) REFERENCES biblio (biblionumber) ON DELETE CASCADE ON UPDATE CASCADE + |); + } + + for my $warning (@warnings) { + warn $warning; + } + + my $description = [ "Add foreign key constraints on serial", @warnings ]; + NewVersion( $DBversion, 21901, $description); +} + # SEE bug 13068 # if there is anything in the atomicupdate, read and execute it. my $update_dir = C4::Context->config('intranetdir') . '/installer/data/mysql/atomicupdate/'; -- 2.39.5