From 986d551ac49488216f51773172bf6a2c7d041e40 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Fri, 22 Feb 2019 14:53:41 +0000 Subject: [PATCH] Bug 20639: DBRev 18.12.00.016 Signed-off-by: Nick Clemens --- Koha.pm | 2 +- Koha/Schema/Result/AuthorisedValue.pm | 19 ++++++++- Koha/Schema/Result/Illrequest.pm | 33 ++++++++++++++- ...d_new_illrequests_status_alias_column.perl | 41 ------------------ installer/data/mysql/updatedatabase.pl | 42 +++++++++++++++++++ 5 files changed, 91 insertions(+), 46 deletions(-) delete mode 100644 installer/data/mysql/atomicupdate/bug_20581-add_new_illrequests_status_alias_column.perl diff --git a/Koha.pm b/Koha.pm index f59abaa0d9..6cf205a748 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 = "18.12.00.015"; +$VERSION = "18.12.00.016"; sub version { return $VERSION; diff --git a/Koha/Schema/Result/AuthorisedValue.pm b/Koha/Schema/Result/AuthorisedValue.pm index 7b978eb3b8..1a65d388c9 100644 --- a/Koha/Schema/Result/AuthorisedValue.pm +++ b/Koha/Schema/Result/AuthorisedValue.pm @@ -129,9 +129,24 @@ __PACKAGE__->belongs_to( { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" }, ); +=head2 illrequests -# Created by DBIx::Class::Schema::Loader v0.07042 @ 2017-04-26 16:13:07 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:YwvGOd/jzk71ekWfO56xrw +Type: has_many + +Related object: L + +=cut + +__PACKAGE__->has_many( + "illrequests", + "Koha::Schema::Result::Illrequest", + { "foreign.status_alias" => "self.authorised_value" }, + { cascade_copy => 0, cascade_delete => 0 }, +); + + +# Created by DBIx::Class::Schema::Loader v0.07046 @ 2019-02-22 14:32:48 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:hDlebhEn+f+thqwBo/LOqQ # You can replace this text with custom code or comments, and it will be preserved on regeneration diff --git a/Koha/Schema/Result/Illrequest.pm b/Koha/Schema/Result/Illrequest.pm index cc56ee16d5..4260a8d2df 100644 --- a/Koha/Schema/Result/Illrequest.pm +++ b/Koha/Schema/Result/Illrequest.pm @@ -54,6 +54,13 @@ __PACKAGE__->table("illrequests"); is_nullable: 1 size: 50 +=head2 status_alias + + data_type: 'varchar' + is_foreign_key: 1 + is_nullable: 1 + size: 80 + =head2 placed data_type: 'date' @@ -143,6 +150,8 @@ __PACKAGE__->add_columns( { data_type => "varchar", is_foreign_key => 1, is_nullable => 0, size => 50 }, "status", { data_type => "varchar", is_nullable => 1, size => 50 }, + "status_alias", + { data_type => "varchar", is_foreign_key => 1, is_nullable => 1, size => 80 }, "placed", { data_type => "date", datetime_undef_if_invalid => 1, is_nullable => 1 }, "replied", @@ -253,9 +262,29 @@ __PACKAGE__->has_many( { cascade_copy => 0, cascade_delete => 0 }, ); +=head2 status_alias + +Type: belongs_to + +Related object: L + +=cut + +__PACKAGE__->belongs_to( + "status_alias", + "Koha::Schema::Result::AuthorisedValue", + { authorised_value => "status_alias" }, + { + is_deferrable => 1, + join_type => "LEFT", + on_delete => "SET NULL", + on_update => "CASCADE", + }, +); + -# Created by DBIx::Class::Schema::Loader v0.07042 @ 2018-10-27 13:16:48 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:rv6QyhxEah2r/rDcz3aOOw +# Created by DBIx::Class::Schema::Loader v0.07046 @ 2019-02-22 14:32:49 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:I6fY8XRfEmRxSzOeVT9Krw # 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_20581-add_new_illrequests_status_alias_column.perl b/installer/data/mysql/atomicupdate/bug_20581-add_new_illrequests_status_alias_column.perl deleted file mode 100644 index 001eb089aa..0000000000 --- a/installer/data/mysql/atomicupdate/bug_20581-add_new_illrequests_status_alias_column.perl +++ /dev/null @@ -1,41 +0,0 @@ -$DBversion = 'XXX'; # will be replaced by the RM -if( CheckVersion( $DBversion ) ) { - - if ( !column_exists( 'illrequests', 'status_alias' ) ) { - # Fresh upgrade, just add the column and constraint - $dbh->do( "ALTER TABLE illrequests ADD COLUMN status_alias varchar(80) DEFAULT NULL AFTER status" ); - } else { - # Migrate all existing foreign keys from referencing authorised_values.id - # to referencing authorised_values.authorised_value - # First remove the foreign key constraint and index - if ( foreign_key_exists( 'illrequests', 'illrequests_safk' ) ) { - $dbh->do( "ALTER TABLE illrequests DROP FOREIGN KEY illrequests_safk"); - } - if ( index_exists( 'illrequests', 'illrequests_safk' ) ) { - $dbh->do( "DROP INDEX illrequests_safk ON illrequests" ); - } - # Now change the illrequests.status_alias column definition from int to varchar - $dbh->do( "ALTER TABLE illrequests MODIFY COLUMN status_alias varchar(80)" ); - # Now replace all references to authorised_values.id with their - # corresponding authorised_values.authorised_value - my $sth = $dbh->prepare( "SELECT illrequest_id, status_alias FROM illrequests WHERE status_alias IS NOT NULL" ); - $sth->execute(); - while (my @row = $sth->fetchrow_array()) { - my $r_id = $row[0]; - my $av_id = $row[1]; - # Get the authorised value's authorised_value value - my ($av_val) = $dbh->selectrow_array( "SELECT authorised_value FROM authorised_values WHERE id = ?", {}, $av_id ); - # Now update illrequests.status_alias - if ($av_val) { - $dbh->do( "UPDATE illrequests SET status_alias = ? WHERE illrequest_id = ?", {}, ($av_val, $r_id) ); - } - } - } - if ( !foreign_key_exists( 'illrequests', 'illrequests_safk' ) ) { - $dbh->do( "ALTER TABLE illrequests ADD CONSTRAINT illrequests_safk FOREIGN KEY (status_alias) REFERENCES authorised_values(authorised_value) ON UPDATE CASCADE ON DELETE SET NULL" ); - } - $dbh->do( "INSERT IGNORE INTO authorised_value_categories SET category_name = 'ILLSTATUS'"); - - SetVersion( $DBversion ); - print "Upgrade to $DBversion done (Bug 20581 - Allow manual selection of custom ILL request statuses)\n"; -} diff --git a/installer/data/mysql/updatedatabase.pl b/installer/data/mysql/updatedatabase.pl index 564b6cf4eb..b50980ef02 100755 --- a/installer/data/mysql/updatedatabase.pl +++ b/installer/data/mysql/updatedatabase.pl @@ -17358,6 +17358,48 @@ if( CheckVersion( $DBversion ) ) { print "Upgrade to $DBversion done (Bug 3820 - Update patron modification logs)\n"; } +$DBversion = '18.12.00.016'; +if( CheckVersion( $DBversion ) ) { + + if ( !column_exists( 'illrequests', 'status_alias' ) ) { + # Fresh upgrade, just add the column and constraint + $dbh->do( "ALTER TABLE illrequests ADD COLUMN status_alias varchar(80) DEFAULT NULL AFTER status" ); + } else { + # Migrate all existing foreign keys from referencing authorised_values.id + # to referencing authorised_values.authorised_value + # First remove the foreign key constraint and index + if ( foreign_key_exists( 'illrequests', 'illrequests_safk' ) ) { + $dbh->do( "ALTER TABLE illrequests DROP FOREIGN KEY illrequests_safk"); + } + if ( index_exists( 'illrequests', 'illrequests_safk' ) ) { + $dbh->do( "DROP INDEX illrequests_safk ON illrequests" ); + } + # Now change the illrequests.status_alias column definition from int to varchar + $dbh->do( "ALTER TABLE illrequests MODIFY COLUMN status_alias varchar(80)" ); + # Now replace all references to authorised_values.id with their + # corresponding authorised_values.authorised_value + my $sth = $dbh->prepare( "SELECT illrequest_id, status_alias FROM illrequests WHERE status_alias IS NOT NULL" ); + $sth->execute(); + while (my @row = $sth->fetchrow_array()) { + my $r_id = $row[0]; + my $av_id = $row[1]; + # Get the authorised value's authorised_value value + my ($av_val) = $dbh->selectrow_array( "SELECT authorised_value FROM authorised_values WHERE id = ?", {}, $av_id ); + # Now update illrequests.status_alias + if ($av_val) { + $dbh->do( "UPDATE illrequests SET status_alias = ? WHERE illrequest_id = ?", {}, ($av_val, $r_id) ); + } + } + } + if ( !foreign_key_exists( 'illrequests', 'illrequests_safk' ) ) { + $dbh->do( "ALTER TABLE illrequests ADD CONSTRAINT illrequests_safk FOREIGN KEY (status_alias) REFERENCES authorised_values(authorised_value) ON UPDATE CASCADE ON DELETE SET NULL" ); + } + $dbh->do( "INSERT IGNORE INTO authorised_value_categories SET category_name = 'ILLSTATUS'"); + + SetVersion( $DBversion ); + print "Upgrade to $DBversion done (Bug 20581 - Allow manual selection of custom ILL request statuses)\n"; +} + # SEE bug 13068 # if there is anything in the atomicupdate, read and execute it. -- 2.39.5