Bug 30449: Add missing FK constraint on borrower_attribute_types
[koha.git] / installer / data / mysql / atomicupdate / bug_30449.pl
1 use Modern::Perl;
2
3 return {
4     bug_number => 30449,
5     description => "Check borrower_attribute_types FK constraint",
6     up => sub {
7         my ($args) = @_;
8         my ($dbh, $out) = @$args{qw(dbh out)};
9
10         if( foreign_key_exists('borrower_attribute_types', 'category_code_fk') ) {
11             $dbh->do( q|ALTER TABLE borrower_attribute_types DROP CONSTRAINT category_code_fk| );
12             if( index_exists('borrower_attribute_types', 'category_code_fk') ) {
13                 $dbh->do( q|ALTER TABLE borrower_attribute_types DROP INDEX category_code_fk| );
14             }
15         }
16         if( !foreign_key_exists('borrower_attribute_types', 'borrower_attribute_types_ibfk_1') ) {
17             if( !index_exists('borrower_attribute_types', 'category_code') ) {
18                 $dbh->do( q|ALTER TABLE borrower_attribute_types ADD INDEX category_code (category_code)| );
19             }
20             $dbh->do( q|ALTER TABLE borrower_attribute_types ADD CONSTRAINT borrower_attribute_types_ibfk_1 FOREIGN KEY (`category_code`) REFERENCES `categories` (`categorycode`)| );
21         }
22     },
23 };