Bug 21901: (QA follow-up) Add a few NOT NULL constraints in dbrev
[koha-ffzg.git] / installer / data / mysql / atomicupdate / bug_21901.perl
1 $DBversion = 'XXX'; # will be replaced by the RM
2 if( CheckVersion( $DBversion ) ) {
3
4     $dbh->do(q|
5         UPDATE
6           serial
7         SET
8           planneddate = NULL
9         WHERE
10           planneddate = '0000-00-00'
11     |);
12
13     $dbh->do(q|
14         UPDATE
15           serial
16         SET
17           publisheddate = NULL
18         WHERE
19           publisheddate = '0000-00-00'
20     |);
21
22     $dbh->do(q|
23         UPDATE
24           serial
25         SET
26           claimdate = NULL
27         WHERE
28           claimdate = '0000-00-00'
29     |);
30
31     $dbh->do(q|
32         ALTER TABLE serial
33         MODIFY COLUMN biblionumber INT(11) NOT NULL
34     |);
35
36     unless ( foreign_key_exists( 'serial', 'serial_ibfk_1' ) ) {
37         my $serials = $dbh->selectall_arrayref(q|
38             SELECT serialid FROM serial WHERE biblionumber NOT IN (SELECT biblionumber FROM biblio)
39         |, { Slice => {} });
40         if ( @$serials ) {
41             warn q|WARNING - The following serials are deleted, they were not attached to an existing bibliographic record (serialid): | . join ", ", map { $_->{serialid} } @$serials;
42             $dbh->do(q|
43                 DELETE FROM serial WHERE biblionumber NOT IN (SELECT biblionumber FROM biblio)
44             |);
45         }
46         $dbh->do(q|
47             ALTER TABLE serial
48             ADD CONSTRAINT serial_ibfk_1 FOREIGN KEY (biblionumber) REFERENCES biblio (biblionumber) ON DELETE CASCADE ON UPDATE CASCADE
49         |);
50     }
51
52     $dbh->do(q|
53         ALTER TABLE serial
54         MODIFY COLUMN subscriptionid INT(11) NOT NULL
55     |);
56
57     unless ( foreign_key_exists( 'serial', 'serial_ibfk_2' ) ) {
58         my $serials = $dbh->selectall_arrayref(q|
59             SELECT serialid FROM serial WHERE subscriptionid NOT IN (SELECT subscriptionid FROM subscription)
60         |, { Slice => {} });
61         if ( @$serials ) {
62             warn q|WARNING - The following serials are deleted, they were not attached to an existing subscription (serialid): | . join ", ", map { $_->{serialid} } @$serials;
63             $dbh->do(q|
64                 DELETE FROM serial WHERE subscriptionid NOT IN (SELECT subscriptionid FROM subscription)
65             |);
66         }
67         $dbh->do(q|
68             ALTER TABLE serial
69             ADD CONSTRAINT serial_ibfk_2 FOREIGN KEY (subscriptionid) REFERENCES subscription (subscriptionid) ON DELETE CASCADE ON UPDATE CASCADE
70         |);
71     }
72
73     $dbh->do(q|
74         ALTER TABLE subscriptionhistory
75         MODIFY COLUMN biblionumber int(11) NOT NULL,
76         MODIFY COLUMN subscriptionid int(11) NOT NULL
77     |);
78
79     unless ( foreign_key_exists( 'subscriptionhistory', 'subscription_history_ibfk_1' ) ) {
80         $dbh->do(q|
81             DELETE FROM subscriptionhistory WHERE biblionumber NOT IN (SELECT biblionumber FROM biblio)
82         |);
83         $dbh->do(q|
84             ALTER TABLE subscriptionhistory
85             ADD CONSTRAINT subscription_history_ibfk_1 FOREIGN KEY (biblionumber) REFERENCES biblio (biblionumber) ON DELETE CASCADE ON UPDATE CASCADE
86         |);
87     }
88
89     unless ( foreign_key_exists( 'subscriptionhistory', 'subscription_history_ibfk_2' ) ) {
90         $dbh->do(q|
91             DELETE FROM subscriptionhistory WHERE subscriptionid NOT IN (SELECT subscriptionid FROM subscription)
92         |);
93         $dbh->do(q|
94             ALTER TABLE subscriptionhistory
95             ADD CONSTRAINT subscription_history_ibfk_2 FOREIGN KEY (subscriptionid) REFERENCES subscription (subscriptionid) ON DELETE CASCADE ON UPDATE CASCADE
96         |);
97     }
98
99     $dbh->do(q|
100         ALTER TABLE subscription
101         MODIFY COLUMN biblionumber int(11) NOT NULL
102     |);
103
104     unless ( foreign_key_exists( 'subscription', 'subscription_ibfk_3' ) ) {
105         my $subscriptions = $dbh->selectall_arrayref(q|
106             SELECT subscriptionid FROM subscription WHERE biblionumber NOT IN (SELECT biblionumber FROM biblio)
107         |, { Slice => {} });
108         if ( @$subscriptions ) {
109             warn q|WARNING - The following subscriptions are deleted, they were not attached to an existing bibliographic record (subscriptionid): | . join ", ", map { $_->{subscriptionid} } @$subscriptions;
110
111             $dbh->do(q|
112                 DELETE FROM subscription WHERE biblionumber NOT IN (SELECT biblionumber FROM biblio)
113             |);
114         }
115         $dbh->do(q|
116             ALTER TABLE subscription
117             ADD CONSTRAINT subscription_ibfk_3 FOREIGN KEY (biblionumber) REFERENCES biblio (biblionumber) ON DELETE CASCADE ON UPDATE CASCADE
118         |);
119     }
120
121     SetVersion( $DBversion );
122     print "Upgrade to $DBversion done (Bug 21901 - Add foreign key constraints on serial)\n";
123 }