From 0b1a1261ffaba5d47665c3e839bc7a9577723fd4 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Fri, 26 May 2023 15:42:08 +0000 Subject: [PATCH] Bug 33847: Database update replaces undefined rules with defaults rather than the value that would be used Bug 29012 introduces a database update that sets the default values for rules that are required but undefined. This functionally changes the results of the circulation rules. Instead, this update should find value that is being used for that rule combo and use that as the rule value, only using the default in the case that the derived rule doesn't exist or has a null value. Test Plan: 1) Check out Koha 22.05.05 2) Create a default all/all/all rule, 3 other rules. Ensure they all have Loan period set to 7, with one of the non-default rules having a Loan period of 14. 3) Delete all but one of the non-default rules with the following query: Delete from circulation_rules where rule_name = 'issuelength' and ( rule_value != 14 and not ( branchcode is null and categorycode is null and itemtype is null ) ) limit 2; 4) Check out 254f721320 5) Run updatedatabase.pl and restart 6) Note the rules were recreated with the value 0 7) Repeat steps 1-4 8) Apply this patch 9) If you're using the same database, set the version to 22.0600023 and restart 10) Run updatedatabase.pl 11) Note the rules were recreated, but the value is the derived value from the all/all/all rule! Signed-off-by: Kevin Carnes Signed-off-by: Marcel de Rooy Signed-off-by: Emily Lamancusa Bug 33847: Rewrite to use SQL Signed-off-by: Emily Lamancusa Signed-off-by: Marcel de Rooy [EDIT] Squashed, and added reference to new bug too Signed-off-by: Katrin Fischer (cherry picked from commit f6f2d1ae41ef83629cb25c684d261f98238fe051) Signed-off-by: Fridolin Somers (cherry picked from commit 8b04a3f642576a5ad3a06c8ef22b42cada75726e) Signed-off-by: Lucas Gass --- installer/data/mysql/db_revs/220600024.pl | 75 ++++++++++++++--------- 1 file changed, 45 insertions(+), 30 deletions(-) diff --git a/installer/data/mysql/db_revs/220600024.pl b/installer/data/mysql/db_revs/220600024.pl index 50c01046c9..d45a061e10 100755 --- a/installer/data/mysql/db_revs/220600024.pl +++ b/installer/data/mysql/db_revs/220600024.pl @@ -1,41 +1,56 @@ use Modern::Perl; return { - bug_number => "29012", + bug_number => "29012/33847", # dbrev fixed on report 33847 description => "Some rules are not saved when left blank while editing a 'rule' line in smart-rules.pl", - up => sub { + up => sub { my ($args) = @_; - my ($dbh, $out) = @$args{qw(dbh out)}; + my ( $dbh, $out ) = @$args{qw(dbh out)}; my %default_rule_values = ( issuelength => 0, - hardduedate => '', - unseen_renewals_allowed => '', + hardduedate => q{}, + unseen_renewals_allowed => q{}, rentaldiscount => 0, - decreaseloanholds => '', + decreaseloanholds => q{}, ); - while (my ($rule_name, $rule_value) = each (%default_rule_values)) { - $dbh->do(q{ - INSERT IGNORE INTO circulation_rules (branchcode, categorycode, itemtype, rule_name, rule_value) - SELECT branchcode, categorycode, itemtype, ?, ? FROM circulation_rules cr - WHERE EXISTS ( - SELECT * FROM circulation_rules cr2 - WHERE - cr2.rule_name="suspension_chargeperiod" - AND ( (cr2.branchcode=cr.branchcode) OR ( ISNULL(cr2.branchcode) AND ISNULL(cr.branchcode) ) ) - AND ( (cr2.categorycode=cr.categorycode) OR ( ISNULL(cr2.categorycode) AND ISNULL(cr.categorycode) ) ) - AND ( (cr2.itemtype=cr.itemtype) OR ( ISNULL(cr2.itemtype) AND ISNULL(cr.itemtype) ) ) - ) - AND NOT EXISTS ( - SELECT * FROM circulation_rules cr2 - WHERE - cr2.rule_name=? - AND ( (cr2.branchcode=cr.branchcode) OR ( ISNULL(cr2.branchcode) AND ISNULL(cr.branchcode) ) ) - AND ( (cr2.categorycode=cr.categorycode) OR ( ISNULL(cr2.categorycode) AND ISNULL(cr.categorycode) ) ) - AND ( (cr2.itemtype=cr.itemtype) OR ( ISNULL(cr2.itemtype) AND ISNULL(cr.itemtype) ) ) - ) - GROUP BY branchcode, categorycode, itemtype - }, undef, $rule_name, $rule_value, $rule_name); + + my $sth = $dbh->prepare( + q{ + SELECT codes.branchcode, codes.categorycode, codes.itemtype + FROM circulation_rules codes + WHERE codes.rule_name = 'fine' + AND NOT EXISTS + (SELECT NULL FROM circulation_rules cr + WHERE cr.rule_name = ? + AND cr.branchcode <=> codes.branchcode + AND cr.categorycode <=> codes.categorycode + AND cr.itemtype <=> codes.itemtype) + } + ); + my $insert_sth = $dbh->prepare( + q{ + INSERT IGNORE INTO circulation_rules (branchcode, categorycode, itemtype, rule_name, rule_value) + SELECT codes.branchcode, codes.categorycode, codes.itemtype, ?, IFNULL(effective.rule_value, ?) + FROM circulation_rules codes + LEFT JOIN circulation_rules effective + ON effective.rule_name = ? + AND (effective.branchcode <=> codes.branchcode OR effective.branchcode IS NULL) + AND (effective.categorycode <=> codes.categorycode OR effective.categorycode IS NULL) + AND (effective.itemtype <=> codes.itemtype OR effective.itemtype IS NULL) + WHERE codes.branchcode <=> ? AND codes.categorycode <=> ? AND codes.itemtype <=> ? AND codes.rule_name = 'fine' + ORDER BY effective.branchcode DESC, effective.categorycode DESC, effective.itemtype DESC + LIMIT 1 } - say $out "Add default values for blank circulation rules that weren't saved to the database"; + ); + my ( $branchcode, $categorycode, $itemtype ); + while ( my ( $rule_name, $rule_value ) = each(%default_rule_values) ) { + $sth->execute($rule_name); + $sth->bind_columns( \( $branchcode, $categorycode, $itemtype ) ); + while ( $sth->fetch ) { + $insert_sth->execute( $rule_name, $rule_value, $rule_name, $branchcode, $categorycode, $itemtype ); + } + } + + say $out "Set derived values for blank circulation rules that weren't saved to the database"; }, -} +}; -- 2.39.5