Bug 32030: Create erm_user_roles after erm_licenses
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Tue, 8 Nov 2022 07:33:38 +0000 (08:33 +0100)
committerTomas Cohen Arazi <tomascohen@theke.io>
Tue, 8 Nov 2022 12:44:53 +0000 (09:44 -0300)
Or the update will fail with the following error

ERROR - {UNKNOWN}: DBI Exception: DBD::mysql::db do failed: Can't create table `koha_kohadev`.`erm_user_roles` (errno: 150 "Foreign key constraint is incorrectly formed") at /kohadevbox/koha/C4/Installer.pm line 739

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
installer/data/mysql/atomicupdate/erm.pl

index 5f4475c..99ba899 100755 (executable)
@@ -69,32 +69,6 @@ return {
                 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
             });
         }
-
-        unless ( TableExists('erm_user_roles') ) {
-            $dbh->do(q{
-                CREATE TABLE `erm_user_roles` (
-                    `agreement_id` INT(11) NULL COMMENT 'link to the agreement',
-                    `license_id` INT(11) NULL COMMENT 'link to the license',
-                    `user_id` INT(11) NOT NULL COMMENT 'link to the user',
-                    `role` VARCHAR(80) NOT NULL COMMENT 'role of the user',
-                    CONSTRAINT `erm_user_roles_ibfk_1` FOREIGN KEY (`agreement_id`) REFERENCES `erm_agreements` (`agreement_id`) ON DELETE CASCADE ON UPDATE CASCADE,
-                    CONSTRAINT `erm_user_roles_ibfk_2` FOREIGN KEY (`license_id`) REFERENCES `erm_licenses` (`license_id`) ON DELETE CASCADE ON UPDATE CASCADE,
-                    CONSTRAINT `erm_user_roles_ibfk_3` FOREIGN KEY (`user_id`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE CASCADE ON UPDATE CASCADE
-                ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-            });
-        }
-        $dbh->do(q{
-            INSERT IGNORE INTO authorised_value_categories (category_name, is_system)
-            VALUES
-                ('ERM_USER_ROLES', 1)
-        });
-        $dbh->do(q{
-            INSERT IGNORE INTO authorised_values (category, authorised_value, lib)
-            VALUES
-                ('ERM_USER_ROLES', 'librarian', 'ERM librarian'),
-                ('ERM_USER_ROLES', 'subject_specialist', 'Subject specialist')
-        });
-
         unless ( TableExists('erm_licenses') ) {
             $dbh->do(q{
                 CREATE TABLE `erm_licenses` (
@@ -156,6 +130,31 @@ return {
                 ('ERM_AGREEMENT_LICENSE_LOCATION', 'cupboard', 'Cupboard');
         });
 
+        unless ( TableExists('erm_user_roles') ) {
+            $dbh->do(q{
+                CREATE TABLE `erm_user_roles` (
+                    `agreement_id` INT(11) NULL COMMENT 'link to the agreement',
+                    `license_id` INT(11) NULL COMMENT 'link to the license',
+                    `user_id` INT(11) NOT NULL COMMENT 'link to the user',
+                    `role` VARCHAR(80) NOT NULL COMMENT 'role of the user',
+                    CONSTRAINT `erm_user_roles_ibfk_1` FOREIGN KEY (`agreement_id`) REFERENCES `erm_agreements` (`agreement_id`) ON DELETE CASCADE ON UPDATE CASCADE,
+                    CONSTRAINT `erm_user_roles_ibfk_2` FOREIGN KEY (`license_id`) REFERENCES `erm_licenses` (`license_id`) ON DELETE CASCADE ON UPDATE CASCADE,
+                    CONSTRAINT `erm_user_roles_ibfk_3` FOREIGN KEY (`user_id`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE CASCADE ON UPDATE CASCADE
+                ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
+            });
+        }
+        $dbh->do(q{
+            INSERT IGNORE INTO authorised_value_categories (category_name, is_system)
+            VALUES
+                ('ERM_USER_ROLES', 1)
+        });
+        $dbh->do(q{
+            INSERT IGNORE INTO authorised_values (category, authorised_value, lib)
+            VALUES
+                ('ERM_USER_ROLES', 'librarian', 'ERM librarian'),
+                ('ERM_USER_ROLES', 'subject_specialist', 'Subject specialist')
+        });
+
         unless ( TableExists('erm_agreement_relationships') ) {
             $dbh->do(q{
                 CREATE TABLE `erm_agreement_relationships` (