Bug 18887: Make DB update idempotent
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Wed, 3 Oct 2018 14:04:45 +0000 (11:04 -0300)
committerNick Clemens <nick@bywatersolutions.com>
Wed, 3 Oct 2018 17:58:18 +0000 (17:58 +0000)
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
installer/data/mysql/atomicupdate/bug_18887.perl

index 97eb1ef..3d38b57 100644 (file)
@@ -21,23 +21,28 @@ if( CheckVersion( $DBversion ) ) {
         });
     }
 
-    $dbh->do(q{
-        INSERT INTO circulation_rules ( branchcode, categorycode, itemtype, rule_name, rule_value )
-        SELECT branchcode, categorycode, NULL, 'max_holds', COALESCE( max_holds, '' ) FROM branch_borrower_circ_rules
-    });
-
-    $dbh->do(q{
-        INSERT INTO circulation_rules ( branchcode, categorycode, itemtype, rule_name, rule_value )
-        SELECT NULL, categorycode, NULL, 'max_holds', COALESCE( max_holds, '' ) FROM default_borrower_circ_rules
-    });
-
-    $dbh->do(q{
-        ALTER TABLE branch_borrower_circ_rules DROP COLUMN max_holds
-    });
-
-    $dbh->do(q{
-        ALTER TABLE default_borrower_circ_rules DROP COLUMN max_holds
-    });
+
+    if (column_exists('branch_borrower_circ_rules', 'max_holds') ){
+        $dbh->do(q{
+            INSERT IGNORE INTO circulation_rules ( branchcode, categorycode, itemtype, rule_name, rule_value )
+            SELECT branchcode, categorycode, NULL, 'max_holds', COALESCE( max_holds, '' ) FROM branch_borrower_circ_rules
+        });
+
+        $dbh->do(q{
+            ALTER TABLE branch_borrower_circ_rules DROP COLUMN max_holds
+        });
+    }
+
+    if (column_exists('default_borrower_circ_rules', 'max_holds') ){
+        $dbh->do(q{
+            INSERT IGNORE INTO circulation_rules ( branchcode, categorycode, itemtype, rule_name, rule_value )
+            SELECT NULL, categorycode, NULL, 'max_holds', COALESCE( max_holds, '' ) FROM default_borrower_circ_rules
+        });
+
+        $dbh->do(q{
+            ALTER TABLE default_borrower_circ_rules DROP COLUMN max_holds
+        });
+    }
 
     SetVersion( $DBversion );
     print "Upgrade to $DBversion done (Bug 18887 - Introduce new table 'circulation_rules', use for 'max_holds' rules)\n";