Bug 21901: DBRev 19.12.00.076
authorMartin Renvoize <martin.renvoize@ptfs-europe.com>
Fri, 17 Apr 2020 09:17:32 +0000 (10:17 +0100)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Fri, 17 Apr 2020 09:17:32 +0000 (10:17 +0100)
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Koha.pm
Koha/Schema/Result/Biblio.pm
Koha/Schema/Result/Serial.pm
Koha/Schema/Result/Subscription.pm
Koha/Schema/Result/Subscriptionhistory.pm
installer/data/mysql/atomicupdate/bug_21901.perl [deleted file]
installer/data/mysql/updatedatabase.pl

diff --git a/Koha.pm b/Koha.pm
index fbf8b40..95a46f1 100644 (file)
--- 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;
index 7c66b1d..806faf8 100644 (file)
@@ -345,6 +345,51 @@ __PACKAGE__->has_many(
   { cascade_copy => 0, cascade_delete => 0 },
 );
 
+=head2 serials
+
+Type: has_many
+
+Related object: L<Koha::Schema::Result::Serial>
+
+=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<Koha::Schema::Result::Subscriptionhistory>
+
+=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<Koha::Schema::Result::Subscription>
+
+=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(
index 58aed2c..e3bcadb 100644 (file)
@@ -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<Koha::Schema::Result::Biblio>
+
+=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<Koha::Schema::Result::Subscription>
+
+=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
index 735cd05..a668b81 100644 (file)
@@ -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<Koha::Schema::Result::Biblio>
+
+=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<Koha::Schema::Result::Serial>
+
+=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<Koha::Schema::Result::Subscriptionhistory>
+
+=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",
index a7d44fc..c39953a 100644 (file)
@@ -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<Koha::Schema::Result::Biblio>
+
+=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<Koha::Schema::Result::Subscription>
+
+=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 (file)
index fb11d77..0000000
+++ /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";
-}
index 0f9c5c8..1b67a58 100755 (executable)
@@ -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/';