From 205ab8f50ba3274e6382b114036b9bc0c943c918 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Tue, 20 Aug 2019 16:15:17 +0100 Subject: [PATCH] Bug 14570: DBRev 19.06.00.022 Signed-off-by: Martin Renvoize --- Koha.pm | 2 +- Koha/Schema/Result/BorrowerRelationship.pm | 20 +++++- .../data/mysql/atomicupdate/bug_14570.perl | 69 ------------------- installer/data/mysql/updatedatabase.pl | 68 ++++++++++++++++++ 4 files changed, 87 insertions(+), 72 deletions(-) delete mode 100644 installer/data/mysql/atomicupdate/bug_14570.perl diff --git a/Koha.pm b/Koha.pm index 0d0b727a4b..9a63efa185 100644 --- a/Koha.pm +++ b/Koha.pm @@ -29,7 +29,7 @@ use vars qw{ $VERSION }; # - #4 : the developer version. The 4th number is the database subversion. # used by developers when the database changes. updatedatabase take care of the changes itself # and is automatically called by Auth.pm when needed. -$VERSION = "19.06.00.021"; +$VERSION = "19.06.00.022"; sub version { return $VERSION; diff --git a/Koha/Schema/Result/BorrowerRelationship.pm b/Koha/Schema/Result/BorrowerRelationship.pm index 88cef4f294..376e3a314d 100644 --- a/Koha/Schema/Result/BorrowerRelationship.pm +++ b/Koha/Schema/Result/BorrowerRelationship.pm @@ -72,6 +72,22 @@ __PACKAGE__->add_columns( __PACKAGE__->set_primary_key("id"); +=head1 UNIQUE CONSTRAINTS + +=head2 C + +=over 4 + +=item * L + +=item * L + +=back + +=cut + +__PACKAGE__->add_unique_constraint("guarantor_guarantee_idx", ["guarantor_id", "guarantee_id"]); + =head1 RELATIONS =head2 guarantee @@ -110,8 +126,8 @@ __PACKAGE__->belongs_to( ); -# Created by DBIx::Class::Schema::Loader v0.07046 @ 2018-07-18 10:52:59 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:R8RThgcrct40Zq0UMW3TWQ +# Created by DBIx::Class::Schema::Loader v0.07046 @ 2019-08-20 15:14:37 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:ZymvWAn9Nzfuh1lExUIhIg # You can replace this text with custom code or comments, and it will be preserved on regeneration diff --git a/installer/data/mysql/atomicupdate/bug_14570.perl b/installer/data/mysql/atomicupdate/bug_14570.perl deleted file mode 100644 index e3f9a6465c..0000000000 --- a/installer/data/mysql/atomicupdate/bug_14570.perl +++ /dev/null @@ -1,69 +0,0 @@ -$DBversion = 'XXX'; # will be replaced by the RM -if( CheckVersion( $DBversion ) ) { - # you can use $dbh here like: - # $dbh->do( "ALTER TABLE biblio ADD COLUMN badtaste int" ); - unless (TableExists('borrower_relationships')){ - $dbh->do(q{ - CREATE TABLE `borrower_relationships` ( - id INT(11) NOT NULL AUTO_INCREMENT, - guarantor_id INT(11) NOT NULL, - guarantee_id INT(11) NOT NULL, - relationship VARCHAR(100) NOT NULL, - PRIMARY KEY (id), - UNIQUE KEY `guarantor_guarantee_idx` ( `guarantor_id`, `guarantee_id` ), - CONSTRAINT r_guarantor FOREIGN KEY ( guarantor_id ) REFERENCES borrowers ( borrowernumber ) ON UPDATE CASCADE ON DELETE CASCADE, - CONSTRAINT r_guarantee FOREIGN KEY ( guarantee_id ) REFERENCES borrowers ( borrowernumber ) ON UPDATE CASCADE ON DELETE CASCADE - ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; - }); - - $dbh->do(q{ - UPDATE borrowers - LEFT JOIN borrowers guarantor ON ( borrowers.guarantorid = guarantor.borrowernumber ) - SET borrowers.guarantorid = NULL WHERE guarantor.borrowernumber IS NULL; - }); - - # Bad data handling: guarantorid IS NOT NULL AND relationship IS NULL - $dbh->do(q{ - UPDATE borrowers - SET relationship = '_bad_data' - WHERE guarantorid IS NOT NULL AND - relationship IS NULL - }); - - $dbh->do(q{ - INSERT INTO borrower_relationships ( guarantor_id, guarantee_id, relationship ) - SELECT guarantorid, borrowernumber, relationship FROM borrowers WHERE guarantorid IS NOT NULL; - }); - - # Clean migrated guarantor data - $dbh->do(q{ - UPDATE borrowers - SET contactname=NULL, - contactfirstname=NULL, - relationship=NULL - WHERE guarantorid IS NOT NULL - }); - } - - if( column_exists( 'borrowers', 'guarantorid' ) ) { - $dbh->do(q{ - ALTER TABLE borrowers DROP guarantorid; - }); - } - - if( column_exists( 'deletedborrowers', 'guarantorid' ) ) { - $dbh->do(q{ - ALTER TABLE deletedborrowers DROP guarantorid; - }); - } - - if( column_exists( 'borrower_modifications', 'guarantorid' ) ) { - $dbh->do(q{ - ALTER TABLE borrower_modifications DROP guarantorid; - }); - } - - # Always end with this (adjust the bug info) - SetVersion( $DBversion ); - print "Upgrade to $DBversion done (Bug 14570 - Make it possible to add multiple guarantors to a record)\n"; -} diff --git a/installer/data/mysql/updatedatabase.pl b/installer/data/mysql/updatedatabase.pl index 2679a0417d..341cdcb65f 100755 --- a/installer/data/mysql/updatedatabase.pl +++ b/installer/data/mysql/updatedatabase.pl @@ -19284,6 +19284,74 @@ if( CheckVersion( $DBversion ) ) { print "Upgrade to $DBversion done (Bug 23309 - Can't add new subfields to bibliographic frameworks in strict mode)\n"; } +$DBversion = '19.06.00.022'; +if ( CheckVersion($DBversion) ) { + + unless ( TableExists('borrower_relationships') ) { + $dbh->do(q{ + CREATE TABLE `borrower_relationships` ( + id INT(11) NOT NULL AUTO_INCREMENT, + guarantor_id INT(11) NOT NULL, + guarantee_id INT(11) NOT NULL, + relationship VARCHAR(100) NOT NULL, + PRIMARY KEY (id), + UNIQUE KEY `guarantor_guarantee_idx` ( `guarantor_id`, `guarantee_id` ), + CONSTRAINT r_guarantor FOREIGN KEY ( guarantor_id ) REFERENCES borrowers ( borrowernumber ) ON UPDATE CASCADE ON DELETE CASCADE, + CONSTRAINT r_guarantee FOREIGN KEY ( guarantee_id ) REFERENCES borrowers ( borrowernumber ) ON UPDATE CASCADE ON DELETE CASCADE + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + }); + + $dbh->do(q{ + UPDATE borrowers + LEFT JOIN borrowers guarantor ON ( borrowers.guarantorid = guarantor.borrowernumber ) + SET borrowers.guarantorid = NULL WHERE guarantor.borrowernumber IS NULL; + }); + + # Bad data handling: guarantorid IS NOT NULL AND relationship IS NULL + $dbh->do(q{ + UPDATE borrowers + SET relationship = '_bad_data' + WHERE guarantorid IS NOT NULL AND + relationship IS NULL + }); + + $dbh->do(q{ + INSERT INTO borrower_relationships ( guarantor_id, guarantee_id, relationship ) + SELECT guarantorid, borrowernumber, relationship FROM borrowers WHERE guarantorid IS NOT NULL; + }); + + # Clean migrated guarantor data + $dbh->do(q{ + UPDATE borrowers + SET contactname=NULL, + contactfirstname=NULL, + relationship=NULL + WHERE guarantorid IS NOT NULL + }); + } + + if ( column_exists( 'borrowers', 'guarantorid' ) ) { + $dbh->do(q{ + ALTER TABLE borrowers DROP guarantorid; + }); + } + + if ( column_exists( 'deletedborrowers', 'guarantorid' ) ) { + $dbh->do(q{ + ALTER TABLE deletedborrowers DROP guarantorid; + }); + } + + if ( column_exists( 'borrower_modifications', 'guarantorid' ) ) { + $dbh->do(q{ + ALTER TABLE borrower_modifications DROP guarantorid; + }); + } + + SetVersion($DBversion); + print "Upgrade to $DBversion done (Bug 14570 - Make it possible to add multiple guarantors to a record)\n"; +} + # SEE bug 13068 # if there is anything in the atomicupdate, read and execute it. my $update_dir = C4::Context->config('intranetdir') . '/installer/data/mysql/atomicupdate/'; -- 2.39.5