Koha/installer/data/mysql/db_revs/211200016.pl
Jonathan Druart 563836cce1 Bug 30276: (bug 30060 follow-up) Fix 21.12.00.016
ERROR 1832 (HY000): Cannot change column 'code': used in a foreign key constraint 'user_permissions_ibfk_2'

Test plan:
Checkout a commit prior to 30060 (c023f63abc)
reset_all
checkout master+this patch
updatedatabase

Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
2022-03-28 16:00:25 +02:00

39 lines
1.8 KiB
Perl
Executable file

use Modern::Perl;
return {
bug_number => "30060",
description => "Update user_permissions to add primary key and remove null option from code column",
up => sub {
my ($args) = @_;
my ($dbh, $out) = @$args{qw(dbh out)};
unless(
primary_key_exists( 'user_permissions', 'borrowernumber') &&
primary_key_exists( 'user_permissions', 'module_bit' ) &&
primary_key_exists( 'user_permissions', 'code')
){
$dbh->do(q{
ALTER TABLE user_permissions DROP INDEX IF EXISTS `PRIMARY`
});
say $out "Dropped any previously created primary key";
$dbh->do(q{ALTER TABLE user_permissions ADD COLUMN temp SERIAL PRIMARY KEY});
$dbh->do(q{DELETE t1 FROM user_permissions t1 INNER JOIN user_permissions t2 WHERE t1.temp < t2.temp AND t1.borrowernumber = t2.borrowernumber AND t1.module_bit = t2.module_bit AND t1.code = t2.code});
$dbh->do(q{ALTER TABLE user_permissions DROP COLUMN temp});
say $out "Removed any duplicate rows";
if ( foreign_key_exists('user_permissions', 'user_permissions_ibfk_2') ) {
$dbh->do(q{
ALTER TABLE user_permissions DROP FOREIGN KEY user_permissions_ibfk_2
});
}
$dbh->do(q{
ALTER TABLE user_permissions ADD CONSTRAINT PK_borrowernumber_module_code PRIMARY KEY (borrowernumber,module_bit,code)
});
$dbh->do(q{
ALTER TABLE user_permissions
ADD CONSTRAINT `user_permissions_ibfk_2` FOREIGN KEY (`module_bit`, `code`) REFERENCES `permissions` (`module_bit`, `code`) ON DELETE CASCADE ON UPDATE CASCADE;
});
say $out "Added a primary key on user_permissions on borrowernumber, module_bit, code";
}
},
};