Koha/installer/data/mysql/db_revs/211200016.pl
Tomas Cohen Arazi d3e6c3c709
Bug 30912: Fix db rev 21.12.00.016
This is a mix of Nick's patch and Jonathan's comment on it. I tested it
with KTD using MySQL 8 and it works correctly.

To test:
1. Launch KTD with bells and whistles:
   $ docker compose -f docker-compose.yml \
                    -f docker-compose.mysql8.0.yml \
                    up -d
2. Inside of it, do:
   $ koha-mysql kohadev
   > update systempreferences set value="21.1200015" where variable="version";
   > \q
   $ restart_all
   $ updatedatabase
=> SUCCESS: All good :-D
3. Run:
   $ koha-mysql kohadev
   > update systempreferences set value="21.1200015" where variable="version";
   > ALTER TABLE user_permissions DROP PRIMARY KEY;
   > \q
   $ updatedatabase
=> FAIL: You get:
Upgrade to 21.12.00.016  [12:47:09]: Bug 30060 - Update user_permissions to add primary key and remove null option from code column
ERROR - {UNKNOWN}: DBI Exception: DBD::mysql::db do failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF EXISTS `PRIMARY`' at line 1 at /kohadevbox/koha/C4/Installer.pm line 739
4. Apply this patch
5. Run:
   $ updatedatabase
=> SUCCESS: Update goes well
6. Sign off :-D

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
2022-06-13 10:54:55 -03:00

41 lines
1.9 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')
){
if ( primary_key_exists('user_permissions') ) {
$dbh->do(q{
ALTER TABLE user_permissions DROP INDEX `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";
}
},
};