From c290ae7e0edf3f30241adb08ad222a6aa7ecb10c Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Tue, 19 Oct 2021 11:28:40 +0000 Subject: [PATCH] Bug 28153: (follow-up) Ensure update is idempotent Signed-off-by: Owen Leonard Signed-off-by: Jonathan Druart --- installer/data/mysql/db_revs/210600035.pl | 75 +++++++++++++---------- 1 file changed, 42 insertions(+), 33 deletions(-) diff --git a/installer/data/mysql/db_revs/210600035.pl b/installer/data/mysql/db_revs/210600035.pl index 2f7c1192a6..d641e82ef8 100755 --- a/installer/data/mysql/db_revs/210600035.pl +++ b/installer/data/mysql/db_revs/210600035.pl @@ -6,39 +6,48 @@ return { up => sub { my ($args) = @_; my ($dbh, $out) = @$args{qw(dbh out)}; - $dbh->do(q{ - INSERT IGNORE INTO `message_attributes` - (message_attribute_id, message_name, `takes_days`) - VALUES (10, 'Hold_Reminder', 0) + my $attribute = $dbh->do(q{ + SELECT message_attribute_id + FROM message_attributes + WHERE message_name = "Hold_Reminder" }); - say $out "Message attribute added"; - $dbh->do(q{ - INSERT IGNORE INTO `message_transports` - (`message_attribute_id`, `message_transport_type`, `is_digest`, `letter_module`, `letter_code`) - VALUES (10, 'email', 0, 'circulation', 'HOLD_REMINDER'), - (10, 'sms', 0, 'circulation', 'HOLD_REMINDER'), - (10, 'phone', 0, 'circulation', 'HOLD_REMINDER'), - (10, 'itiva', 0, 'circulation', 'HOLD_REMINDER') - }); - say $out "HOLD_REMINDER added to message_transports"; - $dbh->do(q{ - INSERT INTO borrower_message_preferences - ( borrowernumber, categorycode, message_attribute_id, days_in_advance, wants_digest ) - SELECT borrowernumber, categorycode, 10, days_in_advance, wants_digest - FROM borrower_message_preferences WHERE message_attribute_id = 4 - }); - say $out "Hold_Filled message preference copied to Hold_Reminder"; - $dbh->do(q{ - INSERT INTO borrower_message_transport_preferences - ( borrower_message_preference_id, message_transport_type ) - SELECT b1.borrower_message_preference_id, message_transport_type - FROM borrower_message_preferences b1 - JOIN borrower_message_preferences b2 ON - b1.message_attribute_id = 10 AND b2.message_attribute_id = 4 AND - b1.borrowernumber=b2.borrowernumber - JOIN borrower_message_transport_preferences bt ON - b2.borrower_message_preference_id = bt.borrower_message_preference_id - }); - say $out "Hold_Filled message transport preferences copied to Hold_Reminder"; + unless( $attribute == 1 ){ + $dbh->do(q{ + INSERT IGNORE INTO `message_attributes` + (message_attribute_id, message_name, `takes_days`) + VALUES (10, 'Hold_Reminder', 0) + }); + say $out "Message attribute added"; + $dbh->do(q{ + INSERT IGNORE INTO `message_transports` + (`message_attribute_id`, `message_transport_type`, `is_digest`, `letter_module`, `letter_code`) + VALUES (10, 'email', 0, 'circulation', 'HOLD_REMINDER'), + (10, 'sms', 0, 'circulation', 'HOLD_REMINDER'), + (10, 'phone', 0, 'circulation', 'HOLD_REMINDER'), + (10, 'itiva', 0, 'circulation', 'HOLD_REMINDER') + }); + say $out "HOLD_REMINDER added to message_transports"; + $dbh->do(q{ + INSERT IGNORE INTO borrower_message_preferences + ( borrowernumber, categorycode, message_attribute_id, days_in_advance, wants_digest ) + SELECT borrowernumber, categorycode, 10, days_in_advance, wants_digest + FROM borrower_message_preferences WHERE message_attribute_id = 4 + }); + say $out "Hold_Filled message preference copied to Hold_Reminder"; + $dbh->do(q{ + INSERT IGNORE INTO borrower_message_transport_preferences + ( borrower_message_preference_id, message_transport_type ) + SELECT b1.borrower_message_preference_id, message_transport_type + FROM borrower_message_preferences b1 + JOIN borrower_message_preferences b2 ON + b1.message_attribute_id = 10 AND b2.message_attribute_id = 4 AND + b1.borrowernumber=b2.borrowernumber + JOIN borrower_message_transport_preferences bt ON + b2.borrower_message_preference_id = bt.borrower_message_preference_id + }); + say $out "Hold_Filled message transport preferences copied to Hold_Reminder"; + } else { + say $out "Hold_Reminder message attribute exists, skipping update"; + } }, } -- 2.39.5