Bug 18925: (QA follow-up) Make DB update idempotent
[koha.git] / installer / data / mysql / atomicupdate / bug_18925.perl
1 $DBversion = 'XXX';  # will be replaced by the RM
2 if( CheckVersion( $DBversion ) ) {
3     if ( TableExists( 'branch_borrower_circ_rules' ) ) {
4         if ( column_exists( 'branch_borrower_circ_rules', 'maxissueqty' ) ) {
5             $dbh->do("
6                 INSERT INTO circulation_rules ( categorycode, branchcode, itemtype, rule_name, rule_value )
7                 SELECT categorycode, branchcode, NULL, 'patron_maxissueqty', COALESCE( maxissueqty, '' )
8                 FROM branch_borrower_circ_rules
9             ");
10             $dbh->do("
11                 INSERT INTO circulation_rules ( categorycode, branchcode, itemtype, rule_name, rule_value )
12                 SELECT categorycode, branchcode, NULL, 'patron_maxonsiteissueqty', COALESCE( maxonsiteissueqty, '' )
13                 FROM branch_borrower_circ_rules
14             ");
15             $dbh->do("DROP TABLE branch_borrower_circ_rules");
16         }
17     }
18
19     if ( TableExists( 'default_borrower_circ_rules' ) ) {
20         if ( column_exists( 'default_borrower_circ_rules', 'maxissueqty' ) ) {
21             $dbh->do("
22                 INSERT INTO circulation_rules ( categorycode, branchcode, itemtype, rule_name, rule_value )
23                 SELECT categorycode, NULL, NULL, 'patron_maxissueqty', COALESCE( maxissueqty, '' )
24                 FROM default_borrower_circ_rules
25             ");
26             $dbh->do("
27                 INSERT INTO circulation_rules ( categorycode, branchcode, itemtype, rule_name, rule_value )
28                 SELECT categorycode, NULL, NULL, 'patron_maxonsiteissueqty', COALESCE( maxonsiteissueqty, '' )
29                 FROM default_borrower_circ_rules
30             ");
31             $dbh->do("DROP TABLE default_borrower_circ_rules");
32         }
33     }
34
35     if ( column_exists( 'default_circ_rules', 'maxissueqty' ) ) {
36         $dbh->do("
37             INSERT INTO circulation_rules ( categorycode, branchcode, itemtype, rule_name, rule_value )
38             SELECT NULL, NULL, NULL, 'patron_maxissueqty', COALESCE( maxissueqty, '' )
39             FROM default_circ_rules
40         ");
41         $dbh->do("
42             INSERT INTO circulation_rules ( categorycode, branchcode, itemtype, rule_name, rule_value )
43             SELECT NULL, NULL, NULL, 'patron_maxonsiteissueqty', COALESCE( maxonsiteissueqty, '' )
44             FROM default_circ_rules
45         ");
46         $dbh->do("ALTER TABLE default_circ_rules DROP COLUMN maxissueqty, DROP COLUMN maxonsiteissueqty");
47     }
48
49     if ( column_exists( 'default_branch_circ_rules', 'maxissueqty' ) ) {
50         $dbh->do("
51             INSERT INTO circulation_rules ( categorycode, branchcode, itemtype, rule_name, rule_value )
52             SELECT NULL, branchcode, NULL, 'patron_maxissueqty', COALESCE( maxissueqty, '' )
53             FROM default_branch_circ_rules
54         ");
55         $dbh->do("
56             INSERT INTO circulation_rules ( categorycode, branchcode, itemtype, rule_name, rule_value )
57             SELECT NULL, NULL, NULL, 'patron_maxonsiteissueqty', COALESCE( maxonsiteissueqty, '' )
58             FROM default_branch_circ_rules
59         ");
60         $dbh->do("ALTER TABLE default_branch_circ_rules DROP COLUMN maxissueqty, DROP COLUMN maxonsiteissueqty");
61     }
62
63     SetVersion( $DBversion );
64     print "Upgrade to $DBversion done (Bug 18925 - Move maxissueqty and maxonsiteissueqty to circulation_rules)\n";
65 }