Bug 22368: Make sure the tests will always pass
[koha.git] / installer / data / mysql / atomicupdate / bug_22368.perl
1 $DBversion = 'XXX';    # will be replaced by the RM
2 if ( CheckVersion($DBversion) ) {
3
4     # Add constraint for suggestedby
5     unless ( foreign_key_exists( 'suggestions', 'suggestions_ibfk_suggestedby' ) )
6     {
7         $dbh->do(
8 "ALTER TABLE suggestions CHANGE COLUMN suggestedby suggestedby INT(11) NULL DEFAULT NULL;"
9         );
10         $dbh->do(
11 "UPDATE suggestions LEFT JOIN borrowers ON (suggestions.suggestedby = borrowers.borrowernumber) SET suggestedby = null WHERE borrowernumber IS null"
12         );
13         $dbh->do(
14 "ALTER TABLE suggestions ADD CONSTRAINT `suggestions_ibfk_suggestedby` FOREIGN KEY (`suggestedby`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE SET NULL ON UPDATE CASCADE"
15         );
16     }
17
18     # Add constraint for managedby
19     unless ( foreign_key_exists( 'suggestions', 'suggestions_ibfk_managedby' ) )
20     {
21         $dbh->do(
22 "UPDATE suggestions LEFT JOIN borrowers ON (suggestions.managedby = borrowers.borrowernumber) SET managedby = null WHERE borrowernumber IS NULL"
23         );
24         $dbh->do(
25 "ALTER TABLE suggestions ADD CONSTRAINT `suggestions_ibfk_managedby` FOREIGN KEY (`managedby`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE SET NULL ON UPDATE CASCADE"
26         );
27     }
28
29     # Add constraint for acceptedby
30     unless (
31         foreign_key_exists( 'suggestions', 'suggestions_ibfk_acceptedby' ) )
32     {
33         $dbh->do(
34 "UPDATE suggestions LEFT JOIN borrowers ON (suggestions.acceptedby = borrowers.borrowernumber) SET acceptedby = null WHERE borrowernumber IS NULL"
35         );
36         $dbh->do(
37 "ALTER TABLE suggestions ADD CONSTRAINT `suggestions_ibfk_acceptedby` FOREIGN KEY (`acceptedby`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE SET NULL ON UPDATE CASCADE"
38         );
39     }
40
41     # Add constraint for rejectedby
42     unless (
43         foreign_key_exists( 'suggestions', 'suggestions_ibfk_rejectedby' ) )
44     {
45         $dbh->do(
46 "UPDATE suggestions LEFT JOIN borrowers ON (suggestions.rejectedby = borrowers.borrowernumber) SET rejectedby = null WHERE borrowernumber IS null"
47         );
48         $dbh->do(
49 "ALTER TABLE suggestions ADD CONSTRAINT `suggestions_ibfk_rejectedby` FOREIGN KEY (`rejectedby`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE SET NULL ON UPDATE CASCADE"
50         );
51     }
52
53     # Add constraint for biblionumber
54     unless (
55         foreign_key_exists( 'suggestions', 'suggestions_ibfk_biblionumber' ) )
56     {
57         $dbh->do(
58 "UPDATE suggestions s LEFT JOIN biblio b ON (s.biblionumber = b.biblionumber) SET s.biblionumber = null WHERE b.biblionumber IS null"
59         );
60         $dbh->do(
61 "ALTER TABLE suggestions ADD CONSTRAINT `suggestions_ibfk_biblionumber` FOREIGN KEY (`biblionumber`) REFERENCES `biblio` (`biblionumber`) ON DELETE SET NULL ON UPDATE CASCADE"
62         );
63     }
64
65     # Add constraint for branchcode
66     unless (
67         foreign_key_exists( 'suggestions', 'suggestions_ibfk_branchcode' ) )
68     {
69         $dbh->do(
70 "UPDATE suggestions s LEFT JOIN branches b ON (s.branchcode = b.branchcode) SET s.branchcode = null WHERE b.branchcode IS null"
71         );
72         $dbh->do(
73 "ALTER TABLE suggestions ADD CONSTRAINT `suggestions_ibfk_branchcode` FOREIGN KEY (`branchcode`) REFERENCES `branches` (`branchcode`) ON DELETE SET NULL ON UPDATE CASCADE"
74         );
75     }
76
77     SetVersion($DBversion);
78     print
79 "Upgrade to $DBversion done (Bug 22368 - Add missing constraints to suggestions)\n";
80 }